From 3fe55c184b5b8f0ad8578ae8db0dad13dfa31c6d Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 9 Jan 2020 11:17:39 +0000 Subject: [PATCH] WIP: zenity command (linux). --- file_unix.go | 23 +++++++++++--------- internal/cmd/{cmd.go => cmd_darwin.go} | 2 ++ internal/cmd/cmd_unix.go | 5 ++++- internal/cmd/cmd_windows.go | 3 +++ internal/osa/osa_darwin.go | 4 ++-- internal/zen/zen_unix.go | 30 ++++++++++++++++++++++++++ msg_unix.go | 14 ++++++++---- 7 files changed, 64 insertions(+), 17 deletions(-) rename internal/cmd/{cmd.go => cmd_darwin.go} (68%) create mode 100644 internal/zen/zen_unix.go diff --git a/file_unix.go b/file_unix.go index f72fbde..224fc24 100644 --- a/file_unix.go +++ b/file_unix.go @@ -5,6 +5,9 @@ package zenity import ( "os/exec" "strings" + + "github.com/ncruces/zenity/internal/cmd" + "github.com/ncruces/zenity/internal/zen" ) func SelectFile(options ...Option) (string, error) { @@ -18,8 +21,8 @@ func SelectFile(options ...Option) (string, error) { args = append(args, "--filename", opts.filename) } args = append(args, zenityFilters(opts.filters)...) - cmd := exec.Command("zenity", args...) - out, err := cmd.Output() + + out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return "", nil } @@ -35,7 +38,7 @@ func SelectFile(options ...Option) (string, error) { func SelectFileMutiple(options ...Option) ([]string, error) { opts := optsParse(options) - args := []string{"--file-selection", "--multiple", "--separator=\x1e"} + args := []string{"--file-selection", "--multiple", "--separator", cmd.Separator} if opts.title != "" { args = append(args, "--title", opts.title) } @@ -43,8 +46,8 @@ func SelectFileMutiple(options ...Option) ([]string, error) { args = append(args, "--filename", opts.filename) } args = append(args, zenityFilters(opts.filters)...) - cmd := exec.Command("zenity", args...) - out, err := cmd.Output() + + out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return nil, nil } @@ -54,7 +57,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { if len(out) > 0 { out = out[:len(out)-1] } - return strings.Split(string(out), "\x1e"), nil + return strings.Split(string(out), cmd.Separator), nil } func SelectFileSave(options ...Option) (string, error) { @@ -71,8 +74,8 @@ func SelectFileSave(options ...Option) (string, error) { args = append(args, "--confirm-overwrite") } args = append(args, zenityFilters(opts.filters)...) - cmd := exec.Command("zenity", args...) - out, err := cmd.Output() + + out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return "", nil } @@ -95,8 +98,8 @@ func SelectDirectory(options ...Option) (string, error) { if opts.filename != "" { args = append(args, "--filename", opts.filename) } - cmd := exec.Command("zenity", args...) - out, err := cmd.Output() + + out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return "", nil } diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd_darwin.go similarity index 68% rename from internal/cmd/cmd.go rename to internal/cmd/cmd_darwin.go index 4e9c44e..7d8d806 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd_darwin.go @@ -1,4 +1,6 @@ package cmd +const LineBreak = "\n" + var Command bool var Separator = "\x00" diff --git a/internal/cmd/cmd_unix.go b/internal/cmd/cmd_unix.go index c3f757d..d905bd8 100644 --- a/internal/cmd/cmd_unix.go +++ b/internal/cmd/cmd_unix.go @@ -1,5 +1,8 @@ -// +build !windows +// +build !windows,!darwin package cmd const LineBreak = "\n" + +var Command bool +var Separator = "\x1e" diff --git a/internal/cmd/cmd_windows.go b/internal/cmd/cmd_windows.go index 94a2f78..e3235fc 100644 --- a/internal/cmd/cmd_windows.go +++ b/internal/cmd/cmd_windows.go @@ -1,3 +1,6 @@ package cmd const LineBreak = "\r\n" + +var Command bool +var Separator string diff --git a/internal/osa/osa_darwin.go b/internal/osa/osa_darwin.go index 54a9c82..f7a3439 100644 --- a/internal/osa/osa_darwin.go +++ b/internal/osa/osa_darwin.go @@ -21,10 +21,10 @@ func Run(script string, data interface{}) ([]byte, error) { res = res[len("")] if cmd.Command { - cmd, err := exec.LookPath("osascript") + path, err := exec.LookPath("osascript") if err == nil { os.Stderr.Close() - syscall.Exec(cmd, []string{"osascript", "-l", "JavaScript", "-e", res}, nil) + syscall.Exec(path, []string{"osascript", "-l", "JavaScript", "-e", res}, nil) } } diff --git a/internal/zen/zen_unix.go b/internal/zen/zen_unix.go new file mode 100644 index 0000000..99e2468 --- /dev/null +++ b/internal/zen/zen_unix.go @@ -0,0 +1,30 @@ +// +build !windows,!darwin + +package zen + +import ( + "os" + "os/exec" + "syscall" + + "github.com/ncruces/zenity/internal/cmd" +) + +var tool, path string + +func init() { + for _, tool = range [3]string{"matedialog", "qarma", "zenity"} { + path, _ = exec.LookPath(tool) + if path != "" { + return + } + } + tool = "zenity" +} + +func Run(args []string) ([]byte, error) { + if cmd.Command && path != "" { + syscall.Exec(path, append([]string{tool}, args...), os.Environ()) + } + return exec.Command(tool, args...).Output() +} diff --git a/msg_unix.go b/msg_unix.go index 00ae017..29d1d6e 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -2,7 +2,11 @@ package zenity -import "os/exec" +import ( + "os/exec" + + "github.com/ncruces/zenity/internal/zen" +) func Error(text string, options ...Option) (bool, error) { return message("--error", text, options) @@ -23,7 +27,10 @@ func Warning(text string, options ...Option) (bool, error) { func message(arg, text string, options []Option) (bool, error) { opts := optsParse(options) - args := []string{arg, "--text", text, "--no-markup"} + args := []string{arg} + if text != "" { + args = append(args, "--text", text, "--no-markup") + } if opts.title != "" { args = append(args, "--title", opts.title) } @@ -56,8 +63,7 @@ func message(arg, text string, options []Option) (bool, error) { args = append(args, "--icon-name=dialog-warning") } - cmd := exec.Command("zenity", args...) - out, err := cmd.Output() + out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if len(out) > 0 && string(out[:len(out)-1]) == opts.extra { return false, ErrExtraButton