zenity/file_unix.go

70 lines
1.6 KiB
Go
Raw Normal View History

2022-03-24 10:37:37 -04:00
//go:build !windows && !darwin
2020-01-08 13:12:29 -05:00
2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 06:29:32 -05:00
import (
"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"}
2022-06-02 07:24:24 -04:00
args = appendGeneral(args, opts)
2021-04-08 11:43:14 -04:00
args = appendFileArgs(args, opts)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-04-29 11:05:28 -04:00
return strResult(opts, out, err)
2019-12-11 06:29:32 -05:00
}
2022-05-19 09:09:21 -04:00
func selectFileMultiple(opts options) ([]string, error) {
2020-01-19 06:57:05 -05:00
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
2022-06-02 07:24:24 -04:00
args = appendGeneral(args, opts)
2021-04-08 11:43:14 -04:00
args = appendFileArgs(args, opts)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-04-08 11:43:14 -04:00
return lstResult(opts, out, err)
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"}
2022-06-02 07:24:24 -04:00
args = appendGeneral(args, opts)
2021-04-08 11:43:14 -04:00
args = appendFileArgs(args, opts)
2020-01-09 06:17:39 -05:00
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-04-29 11:05:28 -04:00
return strResult(opts, out, err)
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
}
2021-04-08 11:43:14 -04:00
func appendFileArgs(args []string, opts options) []string {
if opts.directory {
args = append(args, "--directory")
}
if opts.filename != "" {
args = append(args, "--filename", opts.filename)
}
if opts.confirmOverwrite {
args = append(args, "--confirm-overwrite")
}
args = append(args, initFilters(opts.fileFilters)...)
return args
}