zenity/file_darwin.go

117 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 (
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-08 21:38:10 -05:00
"github.com/ncruces/zenity/internal/cmd"
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-09 20:46:53 -05:00
data := osa.File{
2020-01-12 18:09:11 -05:00
Prompt: opts.title,
Invisibles: opts.hidden,
2020-01-09 20:46:53 -05:00
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
2020-01-12 18:09:11 -05:00
data.Type = initFilters(opts.filters)
2020-01-09 20:46:53 -05:00
}
data.Location, _ = splitDirAndName(opts.filename)
out, err := osa.Run("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
}
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-09 20:46:53 -05:00
data := osa.File{
2020-01-12 18:09:11 -05:00
Prompt: opts.title,
Invisibles: opts.hidden,
Multiple: true,
Separator: cmd.Separator,
2020-01-09 20:46:53 -05:00
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFile"
2020-01-12 18:09:11 -05:00
data.Type = initFilters(opts.filters)
2020-01-09 20:46:53 -05:00
}
data.Location, _ = splitDirAndName(opts.filename)
out, err := osa.Run("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
}
if len(out) > 0 {
out = out[:len(out)-1]
}
2019-12-11 19:22:28 -05:00
if len(out) == 0 {
return nil, nil
}
2020-01-08 21:38:10 -05:00
return strings.Split(string(out), cmd.Separator), nil
2019-12-11 05:29:31 -05:00
}
2020-01-04 22:21:39 -05:00
func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-09 20:46:53 -05:00
data := osa.File{
2020-01-12 18:09:11 -05:00
Prompt: opts.title,
}
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFileName"
data.Type = initFilters(opts.filters)
2019-12-11 05:29:31 -05:00
}
2020-01-09 20:46:53 -05:00
data.Location, data.Name = splitDirAndName(opts.filename)
2019-12-11 05:29:31 -05:00
2020-01-09 20:46:53 -05:00
out, err := osa.Run("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
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
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
}