zenity/file_unix.go

73 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
2024-07-10 00:06:01 -04:00
"git.bigun.dev/evan/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
}
2022-12-14 22:14:04 -05:00
func initFilters(filters FileFilters) []string {
2019-12-11 06:29:32 -05:00
var res []string
2022-12-14 22:14:04 -05:00
filters.casefold()
2019-12-11 06:29:32 -05:00
for _, f := range filters {
var buf strings.Builder
buf.WriteString("--file-filter=")
if f.Name != "" {
buf.WriteString(f.Name)
2023-04-12 06:08:58 -04:00
buf.WriteByte('|')
2019-12-11 06:29:32 -05:00
}
2023-04-12 06:08:58 -04:00
for i, p := range f.Patterns {
if i != 0 {
buf.WriteByte(' ')
}
2020-01-08 19:54:34 -05:00
buf.WriteString(p)
2019-12-11 06:29:32 -05:00
}
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
}