zenity/entry_unix.go

100 lines
2.5 KiB
Go
Raw Normal View History

2021-03-05 08:56:16 -05:00
// +build !windows,!darwin
package zenity
import (
2021-03-05 21:29:00 -05:00
"bytes"
2021-03-05 08:56:16 -05:00
"os/exec"
"strconv"
2021-03-05 21:07:00 -05:00
"strings"
2021-03-05 08:56:16 -05:00
"github.com/ncruces/zenity/internal/zenutil"
)
2021-03-05 21:07:00 -05:00
func entry(text string, opts options) (string, bool, error) {
2021-03-05 10:14:30 -05:00
args := []string{"--entry", "--text", text, "--entry-text", opts.entryText}
2021-03-05 08:56:16 -05:00
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)
}
2021-03-05 10:14:30 -05:00
if opts.hideText {
args = append(args, "--hide-text")
}
2021-03-05 08:56:16 -05:00
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)
2021-03-05 21:29:00 -05:00
out = bytes.TrimSuffix(out, []byte{'\n'})
2021-03-05 08:56:16 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2021-03-05 21:29:00 -05:00
if opts.extraButton != nil &&
*opts.extraButton == string(out) {
2021-03-05 21:07:00 -05:00
return "", false, ErrExtraButton
2021-03-05 08:56:16 -05:00
}
2021-03-05 21:07:00 -05:00
return "", false, nil
2021-03-05 08:56:16 -05:00
}
if err != nil {
2021-03-05 21:07:00 -05:00
return "", false, err
2021-03-05 08:56:16 -05:00
}
2021-03-05 21:29:00 -05:00
return string(out), true, nil
2021-03-05 21:07:00 -05:00
}
func password(opts options) (string, string, bool, error) {
args := []string{"--password"}
if opts.title != nil {
args = append(args, "--title", *opts.title)
}
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)
}
if opts.username {
args = append(args, "--username")
}
out, err := zenutil.Run(opts.ctx, args)
2021-03-05 21:29:00 -05:00
out = bytes.TrimSuffix(out, []byte{'\n'})
2021-03-05 21:07:00 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2021-03-05 21:29:00 -05:00
if opts.extraButton != nil &&
*opts.extraButton == string(out) {
2021-03-05 21:07:00 -05:00
return "", "", false, ErrExtraButton
}
return "", "", false, nil
}
if err != nil {
return "", "", false, err
}
if opts.username {
2021-03-05 21:29:00 -05:00
if split := strings.SplitN(string(out), "|", 2); len(split) == 2 {
2021-03-05 21:07:00 -05:00
return split[0], split[1], true, nil
}
}
2021-03-05 21:29:00 -05:00
return "", string(out), true, nil
2021-03-05 08:56:16 -05:00
}