zenity/entry_darwin.go

67 lines
1.6 KiB
Go
Raw Normal View History

2021-03-04 21:01:59 -05:00
package zenity
import (
2021-03-05 21:29:00 -05:00
"bytes"
2021-03-04 21:01:59 -05:00
"os/exec"
"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-04 21:01:59 -05:00
var data zenutil.Dialog
data.Text = text
data.Operation = "displayDialog"
data.Options.Title = opts.title
data.Options.Answer = &opts.entryText
data.Options.Hidden = opts.hideText
data.Options.Timeout = zenutil.Timeout
switch opts.icon {
case ErrorIcon:
data.Options.Icon = "stop"
case WarningIcon:
data.Options.Icon = "caution"
case InfoIcon, QuestionIcon:
data.Options.Icon = "note"
}
if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil {
if opts.okLabel == nil {
opts.okLabel = stringPtr("OK")
}
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
}
if opts.extraButton == nil {
data.Options.Buttons = []string{*opts.cancelLabel, *opts.okLabel}
data.Options.Default = 2
data.Options.Cancel = 1
} else {
data.Options.Buttons = []string{*opts.extraButton, *opts.cancelLabel, *opts.okLabel}
data.Options.Default = 3
data.Options.Cancel = 2
}
data.Extra = opts.extraButton
}
out, err := zenutil.Run(opts.ctx, "dialog", data)
2021-03-05 21:29:00 -05:00
out = bytes.TrimSuffix(out, []byte{'\n'})
2021-03-04 21:01:59 -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-04 21:01:59 -05:00
}
2021-03-05 21:07:00 -05:00
return "", false, nil
2021-03-04 21:01:59 -05:00
}
if err != nil {
2021-03-05 21:07:00 -05:00
return "", false, err
2021-03-04 21:01:59 -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) {
opts.hideText = true
2021-03-29 14:07:44 -04:00
pass, ok, err := entry("Password:", opts)
2021-03-05 21:07:00 -05:00
return "", pass, ok, err
2021-03-04 21:01:59 -05:00
}