forked from gen2brain/dlgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_linux.go
74 lines (60 loc) · 1.54 KB
/
file_linux.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
73
74
// +build linux,!windows,!darwin,!js
package dlgs
import (
"os/exec"
"strings"
"syscall"
)
// File displays a file dialog, returning the selected file/directory and a bool for success.
func File(title, filter string, directory bool) (string, bool, error) {
cmd, err := cmdPath()
if err != nil {
return "", false, err
}
dir := ""
if directory {
dir = "--directory"
}
fileFilter := ""
if filter != "" {
fileFilter = "--file-filter=" + filter
}
o, err := exec.Command(cmd, "--file-selection", "--title", title, fileFilter, dir).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return "", ws.ExitStatus() == 0, nil
}
}
ret := true
out := strings.TrimSpace(string(o))
if out == "" {
ret = false
}
return out, ret, err
}
// FileMulti displays a file dialog, returning the selected files and a bool for success.
func FileMulti(title, filter string) ([]string, bool, error) {
cmd, err := cmdPath()
if err != nil {
return []string{}, false, err
}
sep := "|"
fileFilter := ""
if filter != "" {
fileFilter = "--file-filter=" + filter
}
o, err := exec.Command(cmd, "--file-selection", "--multiple", "--separator", sep, "--title", title, fileFilter).Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return []string{}, ws.ExitStatus() == 0, nil
}
}
ret := true
out := strings.TrimSpace(string(o))
if out == "" {
ret = false
}
return strings.Split(out, sep), ret, err
}