zenity/file_unix.go

106 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-01-08 13:12:29 -05:00
// +build !windows,!darwin
2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 06:29:32 -05:00
import (
2021-03-05 21:29:00 -05:00
"bytes"
2019-12-11 06:29:32 -05:00
"os/exec"
"strings"
2020-01-09 06:17:39 -05:00
2020-01-19 06:57:05 -05:00
"github.com/ncruces/zenity/internal/zenutil"
2019-12-11 06:29:32 -05:00
)
2021-03-04 07:42:30 -05:00
func selectFile(opts options) (string, error) {
2019-12-11 06:29:32 -05:00
args := []string{"--file-selection"}
2021-03-04 07:35:27 -05:00
if opts.title != nil {
args = append(args, "--title", *opts.title)
2019-12-11 06:29:32 -05:00
}
2021-03-05 10:14:30 -05:00
if opts.directory {
args = append(args, "--directory")
}
2020-01-02 20:44:23 -05:00
if opts.filename != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--filename", opts.filename)
2019-12-11 06:29:32 -05:00
}
2020-01-24 07:52:45 -05:00
args = append(args, initFilters(opts.fileFilters)...)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-03-05 21:29:00 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2019-12-11 19:42:40 -05:00
return "", nil
}
2019-12-11 06:29:32 -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 06:29:32 -05:00
}
2021-03-04 07:42:30 -05:00
func selectFileMutiple(opts options) ([]string, error) {
2020-01-19 06:57:05 -05:00
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
2021-03-04 07:35:27 -05:00
if opts.title != nil {
args = append(args, "--title", *opts.title)
2019-12-11 06:29:32 -05:00
}
2021-03-05 10:14:30 -05:00
if opts.directory {
args = append(args, "--directory")
}
2020-01-02 20:44:23 -05:00
if opts.filename != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--filename", opts.filename)
2019-12-11 06:29:32 -05:00
}
2020-01-24 07:52:45 -05:00
args = append(args, initFilters(opts.fileFilters)...)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-03-05 21:29:00 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2019-12-11 19:42:40 -05:00
return nil, nil
}
2019-12-11 06:29:32 -05:00
if err != nil {
return nil, err
}
2021-03-05 21:29:00 -05:00
out = bytes.TrimSuffix(out, []byte{'\n'})
if len(out) == 0 {
return nil, nil
2019-12-11 06:29:32 -05:00
}
2020-01-19 06:57:05 -05:00
return strings.Split(string(out), zenutil.Separator), nil
2019-12-11 06:29:32 -05:00
}
2021-03-04 07:42:30 -05:00
func selectFileSave(opts options) (string, error) {
2019-12-11 06:29:32 -05:00
args := []string{"--file-selection", "--save"}
2021-03-04 07:35:27 -05:00
if opts.title != nil {
args = append(args, "--title", *opts.title)
2019-12-11 06:29:32 -05:00
}
2021-03-05 10:14:30 -05:00
if opts.directory {
args = append(args, "--directory")
2019-12-11 06:29:32 -05:00
}
2020-01-24 07:52:45 -05:00
if opts.confirmOverwrite {
2019-12-11 06:29:32 -05:00
args = append(args, "--confirm-overwrite")
}
2021-03-05 10:14:30 -05:00
if opts.filename != "" {
args = append(args, "--filename", opts.filename)
}
2020-01-24 07:52:45 -05:00
args = append(args, initFilters(opts.fileFilters)...)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-03-05 21:29:00 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2019-12-11 19:42:40 -05:00
return "", nil
}
2019-12-11 06:29:32 -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 06:29:32 -05:00
}
2020-01-12 18:09:11 -05:00
func initFilters(filters []FileFilter) []string {
2019-12-11 06:29:32 -05:00
var res []string
for _, f := range filters {
var buf strings.Builder
buf.WriteString("--file-filter=")
if f.Name != "" {
buf.WriteString(f.Name)
buf.WriteRune('|')
}
2020-01-08 19:54:34 -05:00
for _, p := range f.Patterns {
buf.WriteString(p)
2019-12-11 06:29:32 -05:00
buf.WriteRune(' ')
}
res = append(res, buf.String())
}
return res
}