forked from ncruces/zenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_unix.go
72 lines (60 loc) · 1.61 KB
/
file_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//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.WriteByte('|')
}
for i, p := range f.Patterns {
if i != 0 {
buf.WriteByte(' ')
}
buf.WriteString(p)
}
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
}