This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
file_linux.go
82 lines (68 loc) · 2.26 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
75
76
77
78
79
80
81
82
// +build linux,!windows,!darwin,!js
package dlgs
import (
"os/exec"
"strings"
"syscall"
)
// File displays a file dialog, returning the selected file or directory, a bool for success, and an
// error if it was unable to display the dialog. Filter is a string that determines
// which extensions should be displayed for the dialog. Separate multiple file
// extensions by spaces and use "*.extension" format for cross-platform compatibility, e.g. "*.png *.jpg".
// A blank string for the filter will display all file types.
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 != "" && !directory {
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 that allows for selecting multiple files. It returns the selected
// files, a bool for success, and an error if it was unable to display the dialog. Filter is a string
// that determines which files should be available for selection in the dialog. Separate multiple file
// extensions by spaces and use "*.extension" format for cross-platform compatibility, e.g. "*.png *.jpg".
// A blank string for the filter will display all file types.
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
}