Entry (unix), see #3.

This commit is contained in:
Nuno Cruces 2021-03-05 13:56:16 +00:00
parent b2df443d8c
commit 79ade2cd7e
3 changed files with 57 additions and 9 deletions

55
entry_unix.go Normal file
View file

@ -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
}

View file

@ -10,7 +10,7 @@ import (
) )
func message(kind messageKind, text string, opts options) (bool, error) { func message(kind messageKind, text string, opts options) (bool, error) {
var args []string args := []string{"--text", text, "--no-markup"}
switch kind { switch kind {
case questionKind: case questionKind:
args = append(args, "--question") args = append(args, "--question")
@ -21,9 +21,6 @@ func message(kind messageKind, text string, opts options) (bool, error) {
case errorKind: case errorKind:
args = append(args, "--error") args = append(args, "--error")
} }
if text != "" {
args = append(args, "--text", text, "--no-markup")
}
if opts.title != nil { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }

View file

@ -7,11 +7,7 @@ import (
) )
func notify(text string, opts options) error { func notify(text string, opts options) error {
args := []string{"--notification"} args := []string{"--notification", "--text", text}
if text != "" {
args = append(args, "--text", text, "--no-markup")
}
if opts.title != nil { if opts.title != nil {
args = append(args, "--title", *opts.title) args = append(args, "--title", *opts.title)
} }