From 79ade2cd7e18394ad9c6a50cf0f5e2a5848d74cd Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 5 Mar 2021 13:56:16 +0000 Subject: [PATCH] Entry (unix), see #3. --- entry_unix.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ msg_unix.go | 5 +---- notify_unix.go | 6 +----- 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 entry_unix.go diff --git a/entry_unix.go b/entry_unix.go new file mode 100644 index 0000000..f6f3d09 --- /dev/null +++ b/entry_unix.go @@ -0,0 +1,55 @@ +// +build !windows,!darwin + +package zenity + +import ( + "os/exec" + "strconv" + + "github.com/ncruces/zenity/internal/zenutil" +) + +func entry(text string, opts options) (string, error) { + args := []string{"--entry", "--text", text} + if opts.title != nil { + args = append(args, "--title", *opts.title) + } + if opts.width > 0 { + args = append(args, "--width", strconv.FormatUint(uint64(opts.width), 10)) + } + if opts.height > 0 { + args = append(args, "--height", strconv.FormatUint(uint64(opts.height), 10)) + } + if opts.okLabel != nil { + args = append(args, "--ok-label", *opts.okLabel) + } + if opts.cancelLabel != nil { + args = append(args, "--cancel-label", *opts.cancelLabel) + } + if opts.extraButton != nil { + args = append(args, "--extra-button", *opts.extraButton) + } + switch opts.icon { + case ErrorIcon: + args = append(args, "--window-icon=error") + case WarningIcon: + args = append(args, "--window-icon=warning") + case InfoIcon: + args = append(args, "--window-icon=info") + case QuestionIcon: + args = append(args, "--window-icon=question") + } + + out, err := zenutil.Run(opts.ctx, args) + if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { + if len(out) > 0 && opts.extraButton != nil && + string(out[:len(out)-1]) == *opts.extraButton { + return "", ErrExtraButton + } + return "", nil + } + if err != nil { + return "", err + } + return string(out[:len(out)-1]), err +} diff --git a/msg_unix.go b/msg_unix.go index bc9d3dc..7d8ed2a 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -10,7 +10,7 @@ import ( ) func message(kind messageKind, text string, opts options) (bool, error) { - var args []string + args := []string{"--text", text, "--no-markup"} switch kind { case questionKind: args = append(args, "--question") @@ -21,9 +21,6 @@ func message(kind messageKind, text string, opts options) (bool, error) { case errorKind: args = append(args, "--error") } - if text != "" { - args = append(args, "--text", text, "--no-markup") - } if opts.title != nil { args = append(args, "--title", *opts.title) } diff --git a/notify_unix.go b/notify_unix.go index 9630f8e..1f54121 100644 --- a/notify_unix.go +++ b/notify_unix.go @@ -7,11 +7,7 @@ import ( ) func notify(text string, opts options) error { - args := []string{"--notification"} - - if text != "" { - args = append(args, "--text", text, "--no-markup") - } + args := []string{"--notification", "--text", text} if opts.title != nil { args = append(args, "--title", *opts.title) }