zenity/file_darwin.go

88 lines
1.7 KiB
Go
Raw Normal View History

2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 05:29:31 -05:00
import (
"strings"
2020-01-05 07:37:45 -05:00
"github.com/ncruces/zenity/internal/osa"
2019-12-11 05:29:31 -05:00
)
2020-01-04 22:21:39 -05:00
func SelectFile(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-05 07:37:45 -05:00
out, err := osa.Run("file", osa.File{
2020-01-02 20:44:23 -05:00
Operation: "chooseFile",
Prompt: opts.title,
Location: opts.filename,
Type: appleFilters(opts.filters),
2019-12-11 05:29:31 -05:00
})
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
2020-01-04 22:21:39 -05:00
func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options)
2020-01-05 07:37:45 -05:00
out, err := osa.Run("file", osa.File{
2020-01-02 20:44:23 -05:00
Operation: "chooseFile",
Multiple: true,
Prompt: opts.title,
Location: opts.filename,
Type: appleFilters(opts.filters),
2019-12-11 05:29:31 -05:00
})
if err != nil {
return nil, err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
2019-12-11 19:22:28 -05:00
if len(out) == 0 {
return nil, nil
}
2019-12-11 05:29:31 -05:00
return strings.Split(string(out), "\x00"), nil
}
2020-01-04 22:21:39 -05:00
func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-05 07:37:45 -05:00
out, err := osa.Run("file", osa.File{
2020-01-02 20:44:23 -05:00
Operation: "chooseFileName",
Prompt: opts.title,
Location: opts.filename,
2019-12-11 05:29:31 -05:00
})
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
2020-01-04 22:21:39 -05:00
func SelectDirectory(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-05 07:37:45 -05:00
out, err := osa.Run("file", osa.File{
2020-01-02 20:44:23 -05:00
Operation: "chooseFolder",
Prompt: opts.title,
Location: opts.filename,
2019-12-11 05:29:31 -05:00
})
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
2019-12-11 06:29:32 -05:00
func appleFilters(filters []FileFilter) []string {
2019-12-11 05:29:31 -05:00
var filter []string
for _, f := range filters {
for _, e := range f.Exts {
filter = append(filter, strings.TrimPrefix(e, "."))
}
}
return filter
}