zenity/file_darwin.go

103 lines
2.3 KiB
Go
Raw Permalink Normal View History

2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 05:29:31 -05:00
import (
2021-03-05 21:29:00 -05:00
"bytes"
2020-01-06 07:01:51 -05:00
"os/exec"
2019-12-11 05:29:31 -05:00
"strings"
2020-01-05 07:37:45 -05:00
2020-01-19 06:57:05 -05:00
"github.com/ncruces/zenity/internal/zenutil"
2019-12-11 05:29:31 -05:00
)
2021-03-04 07:42:30 -05:00
func selectFile(opts options) (string, error) {
2021-02-19 12:46:47 -05:00
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _ = splitDirAndName(opts.filename)
2020-01-09 20:46:53 -05:00
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
2021-02-19 12:46:47 -05:00
data.Options.Type = initFilters(opts.fileFilters)
2020-01-09 20:46:53 -05:00
}
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, "file", data)
2020-01-06 07:01:51 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil
}
2019-12-11 05:29:31 -05:00
if err != nil {
return "", err
}
2021-03-05 21:29:00 -05:00
return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
2019-12-11 05:29:31 -05:00
}
2021-03-04 07:42:30 -05:00
func selectFileMutiple(opts options) ([]string, error) {
2021-02-19 12:46:47 -05:00
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _ = splitDirAndName(opts.filename)
data.Options.Multiple = true
data.Separator = zenutil.Separator
2020-01-09 20:46:53 -05:00
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
2021-02-19 12:46:47 -05:00
data.Options.Type = initFilters(opts.fileFilters)
2020-01-09 20:46:53 -05:00
}
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, "file", data)
2020-01-06 07:01:51 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return nil, nil
}
2019-12-11 05:29:31 -05:00
if err != nil {
return nil, err
}
2021-03-05 21:29:00 -05:00
out = bytes.TrimSuffix(out, []byte{'\n'})
2019-12-11 19:22:28 -05:00
if len(out) == 0 {
return nil, nil
}
2020-01-19 06:57:05 -05:00
return strings.Split(string(out), zenutil.Separator), nil
2019-12-11 05:29:31 -05:00
}
2021-03-04 07:42:30 -05:00
func selectFileSave(opts options) (string, error) {
2021-02-19 12:46:47 -05:00
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, data.Options.Name = splitDirAndName(opts.filename)
2020-01-12 18:09:11 -05:00
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFileName"
2019-12-11 05:29:31 -05:00
}
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, "file", data)
2020-01-06 07:01:51 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil
}
2019-12-11 05:29:31 -05:00
if err != nil {
return "", err
}
2021-03-05 21:29:00 -05:00
return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
2019-12-11 05:29:31 -05:00
}
2020-01-12 18:09:11 -05:00
func initFilters(filters []FileFilter) []string {
2019-12-11 05:29:31 -05:00
var filter []string
for _, f := range filters {
2020-01-08 19:54:34 -05:00
for _, p := range f.Patterns {
2020-01-08 21:38:10 -05:00
star := strings.LastIndexByte(p, '*')
if star >= 0 {
dot := strings.LastIndexByte(p, '.')
if star > dot {
return nil
}
filter = append(filter, p[dot+1:])
} else {
filter = append(filter, p)
}
2019-12-11 05:29:31 -05:00
}
}
return filter
}