zenity/file.go

97 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-01-09 12:05:43 -05:00
package zenity
import (
"os"
"path/filepath"
)
2020-01-23 06:44:28 -05:00
// SelectFile displays the file selection dialog.
//
// Returns an empty string on cancel.
//
// Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFile(options ...Option) (string, error) {
2020-01-26 11:04:49 -05:00
return selectFile(options)
2020-01-23 06:44:28 -05:00
}
// SelectFileMutiple displays the multiple file selection dialog.
//
// Returns a nil slice on cancel.
//
// Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFileMutiple(options ...Option) ([]string, error) {
2020-01-26 11:04:49 -05:00
return selectFileMutiple(options)
2020-01-23 06:44:28 -05:00
}
// SelectFileSave displays the save file selection dialog.
//
// Returns an empty string on cancel.
//
// Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden,
// FileFilter(s).
func SelectFileSave(options ...Option) (string, error) {
2020-01-26 11:04:49 -05:00
return selectFileSave(options)
2020-01-23 06:44:28 -05:00
}
// Filename returns an Option to set the filename.
//
// You can specify a file name, a directory path, or both.
// Specifying a file name, makes it the default selected file.
// Specifying a directory path, makes it the default dialog location.
func Filename(filename string) Option {
2020-01-24 07:52:45 -05:00
return funcOption(func(o *options) { o.filename = filename })
2020-01-23 06:44:28 -05:00
}
// Directory returns an Option to activate directory-only selection.
func Directory() Option {
2020-01-24 07:52:45 -05:00
return funcOption(func(o *options) { o.directory = true })
2020-01-23 06:44:28 -05:00
}
// ConfirmOverwrite returns an Option to confirm file selection if filename
// already exists.
func ConfirmOverwrite() Option {
2020-01-24 07:52:45 -05:00
return funcOption(func(o *options) { o.confirmOverwrite = true })
2020-01-23 06:44:28 -05:00
}
// ConfirmCreate returns an Option to confirm file selection if filename does
// not yet exist (Windows only).
func ConfirmCreate() Option {
2020-01-24 07:52:45 -05:00
return funcOption(func(o *options) { o.confirmCreate = true })
2020-01-23 06:44:28 -05:00
}
// ShowHidden returns an Option to show hidden files (Windows and macOS only).
func ShowHidden() Option {
2020-01-24 07:52:45 -05:00
return funcOption(func(o *options) { o.showHidden = true })
2020-01-23 06:44:28 -05:00
}
2020-01-24 07:52:45 -05:00
// FileFilter is an Option that sets a filename filter.
2020-01-23 06:44:28 -05:00
//
// macOS hides filename filters from the user,
// and only supports filtering by extension (or "type").
type FileFilter struct {
Name string // display string that describes the filter (optional)
Patterns []string // filter patterns for the display string
}
2020-01-24 07:52:45 -05:00
func (f FileFilter) apply(o *options) {
o.fileFilters = append(o.fileFilters, f)
2020-01-23 06:44:28 -05:00
}
2020-01-24 07:52:45 -05:00
// FileFilters is an Option that sets multiple filename filters.
2020-01-23 06:44:28 -05:00
type FileFilters []FileFilter
2020-01-24 07:52:45 -05:00
func (f FileFilters) apply(o *options) {
o.fileFilters = append(o.fileFilters, f...)
2020-01-23 06:44:28 -05:00
}
2020-01-09 12:05:43 -05:00
func splitDirAndName(path string) (dir, name string) {
2021-02-18 08:24:02 -05:00
if path != "" {
path = filepath.Clean(path)
fi, err := os.Stat(path)
if err == nil && fi.IsDir() {
return path, ""
}
2020-01-09 12:05:43 -05:00
}
return filepath.Split(path)
}