2019-12-29 20:00:50 -05:00
|
|
|
package zenity
|
2019-12-11 06:29:32 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-12-29 20:00:50 -05:00
|
|
|
func SelectFile(title, defaultPath string, filters []FileFilter) (string, error) {
|
2019-12-11 06:29:32 -05:00
|
|
|
args := []string{"--file-selection"}
|
|
|
|
if title != "" {
|
|
|
|
args = append(args, "--title="+title)
|
|
|
|
}
|
|
|
|
if defaultPath != "" {
|
|
|
|
args = append(args, "--filename="+defaultPath)
|
|
|
|
}
|
|
|
|
args = append(args, zenityFilters(filters)...)
|
|
|
|
cmd := exec.Command("zenity", args...)
|
|
|
|
out, err := cmd.Output()
|
2019-12-11 19:42:40 -05:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-12-11 06:29:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
|
|
out = out[:len(out)-1]
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:00:50 -05:00
|
|
|
func SelectFileMutiple(title, defaultPath string, filters []FileFilter) ([]string, error) {
|
2019-12-11 06:29:32 -05:00
|
|
|
args := []string{"--file-selection", "--multiple", "--separator=\x1e"}
|
|
|
|
if title != "" {
|
|
|
|
args = append(args, "--title="+title)
|
|
|
|
}
|
|
|
|
if defaultPath != "" {
|
|
|
|
args = append(args, "--filename="+defaultPath)
|
|
|
|
}
|
|
|
|
args = append(args, zenityFilters(filters)...)
|
|
|
|
cmd := exec.Command("zenity", args...)
|
|
|
|
out, err := cmd.Output()
|
2019-12-11 19:42:40 -05:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-11 06:29:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
|
|
out = out[:len(out)-1]
|
|
|
|
}
|
|
|
|
return strings.Split(string(out), "\x1e"), nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:00:50 -05:00
|
|
|
func SelectFileSave(title, defaultPath string, confirmOverwrite bool, filters []FileFilter) (string, error) {
|
2019-12-11 06:29:32 -05:00
|
|
|
args := []string{"--file-selection", "--save"}
|
|
|
|
if title != "" {
|
|
|
|
args = append(args, "--title="+title)
|
|
|
|
}
|
|
|
|
if defaultPath != "" {
|
|
|
|
args = append(args, "--filename="+defaultPath)
|
|
|
|
}
|
|
|
|
if confirmOverwrite {
|
|
|
|
args = append(args, "--confirm-overwrite")
|
|
|
|
}
|
|
|
|
args = append(args, zenityFilters(filters)...)
|
|
|
|
cmd := exec.Command("zenity", args...)
|
|
|
|
out, err := cmd.Output()
|
2019-12-11 19:42:40 -05:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-12-11 06:29:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
|
|
out = out[:len(out)-1]
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:00:50 -05:00
|
|
|
func SelectDirectory(title, defaultPath string) (string, error) {
|
2019-12-11 06:29:32 -05:00
|
|
|
args := []string{"--file-selection", "--directory"}
|
|
|
|
if title != "" {
|
|
|
|
args = append(args, "--title="+title)
|
|
|
|
}
|
|
|
|
if defaultPath != "" {
|
|
|
|
args = append(args, "--filename="+defaultPath)
|
|
|
|
}
|
|
|
|
cmd := exec.Command("zenity", args...)
|
|
|
|
out, err := cmd.Output()
|
2019-12-11 19:42:40 -05:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-12-11 06:29:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(out) > 0 {
|
|
|
|
out = out[:len(out)-1]
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func zenityFilters(filters []FileFilter) []string {
|
|
|
|
var res []string
|
|
|
|
for _, f := range filters {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf.WriteString("--file-filter=")
|
|
|
|
if f.Name != "" {
|
|
|
|
buf.WriteString(f.Name)
|
|
|
|
buf.WriteRune('|')
|
|
|
|
}
|
|
|
|
for _, e := range f.Exts {
|
|
|
|
buf.WriteRune('*')
|
|
|
|
buf.WriteString(e)
|
|
|
|
buf.WriteRune(' ')
|
|
|
|
}
|
|
|
|
res = append(res, buf.String())
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|