File dialogs with zenity.

This commit is contained in:
Nuno Cruces 2019-12-11 11:29:32 +00:00
parent 5dd85636fd
commit 0967af949a
4 changed files with 118 additions and 10 deletions

6
dialog/dialog.go Normal file
View file

@ -0,0 +1,6 @@
package dialog
type FileFilter struct {
Name string
Exts []string
}

View file

@ -14,7 +14,7 @@ func OpenFile(title, defaultPath string, filters []FileFilter) (string, error) {
Operation: "chooseFile", Operation: "chooseFile",
Title: title, Title: title,
DefaultPath: defaultPath, DefaultPath: defaultPath,
Filter: toFilter(filters), Filter: appleFilters(filters),
}) })
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
@ -33,7 +33,7 @@ func OpenFiles(title, defaultPath string, filters []FileFilter) ([]string, error
Multiple: true, Multiple: true,
Title: title, Title: title,
DefaultPath: defaultPath, DefaultPath: defaultPath,
Filter: toFilter(filters), Filter: appleFilters(filters),
}) })
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
@ -45,7 +45,7 @@ func OpenFiles(title, defaultPath string, filters []FileFilter) ([]string, error
return strings.Split(string(out), "\x00"), nil return strings.Split(string(out), "\x00"), nil
} }
func SaveFile(title, defaultPath string, filters []FileFilter) (string, error) { func SaveFile(title, defaultPath string, confirmOverwrite bool, filters []FileFilter) (string, error) {
cmd := exec.Command("osascript", "-l", "JavaScript") cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = scriptExpand(scriptData{ cmd.Stdin = scriptExpand(scriptData{
Operation: "chooseFileName", Operation: "chooseFileName",
@ -79,12 +79,7 @@ func PickFolder(title, defaultPath string) (string, error) {
return string(out), nil return string(out), nil
} }
type FileFilter struct { func appleFilters(filters []FileFilter) []string {
Name string
Exts []string
}
func toFilter(filters []FileFilter) []string {
var filter []string var filter []string
for _, f := range filters { for _, f := range filters {
for _, e := range f.Exts { for _, e := range f.Exts {

107
dialog/dialog_linux.go Normal file
View file

@ -0,0 +1,107 @@
package dialog
import (
"os/exec"
"strings"
)
func OpenFile(title, defaultPath string, filters []FileFilter) (string, error) {
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()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func OpenFiles(title, defaultPath string, filters []FileFilter) ([]string, error) {
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()
if err != nil {
return nil, err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return strings.Split(string(out), "\x1e"), nil
}
func SaveFile(title, defaultPath string, confirmOverwrite bool, filters []FileFilter) (string, error) {
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()
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func PickFolder(title, defaultPath string) (string, error) {
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()
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
}

View file

@ -31,7 +31,7 @@ func TestOpenFiles(t *testing.T) {
} }
func TestSaveFile(t *testing.T) { func TestSaveFile(t *testing.T) {
ret, err := SaveFile("", "", []FileFilter{ ret, err := SaveFile("", "", true, []FileFilter{
{"Go files", []string{".go"}}, {"Go files", []string{".go"}},
{"Web files", []string{".html", ".js", ".css"}}, {"Web files", []string{".html", ".js", ".css"}},
{"Image files", []string{".png", ".ico"}}, {"Image files", []string{".png", ".ico"}},