zenity/file.go

220 lines
5.5 KiB
Go
Raw Permalink Normal View History

2020-01-09 12:05:43 -05:00
package zenity
import (
"os"
"path/filepath"
2021-04-11 22:59:08 -04:00
"strings"
2020-01-09 12:05:43 -05:00
)
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) {
2021-03-04 07:42:30 -05:00
return selectFile(applyOptions(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) {
2021-03-04 07:42:30 -05:00
return selectFileMutiple(applyOptions(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) {
2021-03-04 07:42:30 -05:00
return selectFileSave(applyOptions(options))
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
}
2021-03-05 10:14:30 -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 {
return funcOption(func(o *options) { o.filename = filename })
}
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
}
2021-04-13 08:28:26 -04:00
// Windows' patterns are case insensitive, don't support character classes or escaping.
// First we remove character classes, then escaping. Patterns with literal wildcards are invalid (match nothing).
// The semicolon is a separator, so we replace it with the single character wildcard.
func (f FileFilters) simplify() {
for _, filter := range f {
for i, pattern := range filter.Patterns {
var escape, invalid bool
var buf strings.Builder
for _, r := range removeClasses(pattern) {
if !escape && r == '\\' {
escape = true
continue
}
if escape && (r == '*' || r == '?') {
invalid = true
break
}
if r == ';' {
r = '?'
}
buf.WriteRune(r)
escape = false
}
if invalid {
filter.Patterns[i] = ""
} else {
filter.Patterns[i] = buf.String()
}
}
}
}
// macOS filters by "type"; the case insensitive literal extension is a good proxy.
// So we extract the extension from each pattern, remove character classes, then escaping.
// If an extension contains a wildcard, any type is accepted.
func (f FileFilters) types() []string {
2021-04-11 22:59:08 -04:00
var res []string
for _, filter := range f {
for _, pattern := range filter.Patterns {
ext := pattern[strings.LastIndexByte(pattern, '.')+1:]
var escape bool
var buf strings.Builder
for _, r := range removeClasses(ext) {
switch {
case escape:
escape = false
case r == '\\':
escape = true
continue
case r == '*' || r == '?':
return nil
}
buf.WriteRune(r)
}
res = append(res, buf.String())
}
}
return res
}
2021-04-13 08:28:26 -04:00
// Remove character classes from pattern, assuming case insensitivity.
// Classes of one character (case insensitive) are replaced by the character.
// Others are replaced by the single character wildcard.
2021-04-11 22:59:08 -04:00
func removeClasses(pattern string) string {
var res strings.Builder
for {
i, j := findClass(pattern)
if i < 0 {
res.WriteString(pattern)
return res.String()
}
res.WriteString(pattern[:i])
var char string
var escape, many bool
for _, r := range pattern[i+1 : j-1] {
if escape {
escape = false
} else if r == '\\' {
escape = true
continue
}
if char == "" {
char = string(r)
} else if !strings.EqualFold(char, string(r)) {
many = true
break
}
}
if many {
res.WriteByte('?')
} else {
res.WriteByte('\\')
res.WriteString(char)
}
pattern = pattern[j:]
}
}
func findClass(pattern string) (start, end int) {
start = -1
escape := false
for i, b := range []byte(pattern) {
switch {
case escape:
escape = false
case b == '\\':
escape = true
case start < 0:
if b == '[' {
start = i
}
case 0 <= start && start < i-1:
if b == ']' {
return start, i + 1
}
}
}
return -1, -1
}
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)
}