zenity/file_unix.go
2022-12-15 03:14:04 +00:00

70 lines
1.6 KiB
Go

//go:build !windows && !darwin
package zenity
import (
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
func selectFile(opts options) (string, error) {
args := []string{"--file-selection"}
args = appendGeneral(args, opts)
args = appendFileArgs(args, opts)
out, err := zenutil.Run(opts.ctx, args)
return strResult(opts, out, err)
}
func selectFileMultiple(opts options) ([]string, error) {
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
args = appendGeneral(args, opts)
args = appendFileArgs(args, opts)
out, err := zenutil.Run(opts.ctx, args)
return lstResult(opts, out, err)
}
func selectFileSave(opts options) (string, error) {
args := []string{"--file-selection", "--save"}
args = appendGeneral(args, opts)
args = appendFileArgs(args, opts)
out, err := zenutil.Run(opts.ctx, args)
return strResult(opts, out, err)
}
func initFilters(filters FileFilters) []string {
var res []string
filters.casefold()
for _, f := range filters {
var buf strings.Builder
buf.WriteString("--file-filter=")
if f.Name != "" {
buf.WriteString(f.Name)
buf.WriteRune('|')
}
for _, p := range f.Patterns {
buf.WriteString(p)
buf.WriteRune(' ')
}
res = append(res, buf.String())
}
return res
}
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
}