diff --git a/entry.go b/entry.go index 259fb82..4a96d80 100644 --- a/entry.go +++ b/entry.go @@ -10,15 +10,6 @@ func Entry(text string, options ...Option) (string, bool, error) { return entry(text, applyOptions(options)) } -// Password displays the password dialog. -// -// Returns false on cancel, or ErrExtraButton. -// -// Valid options: Title, OKLabel, CancelLabel, ExtraButton, Icon, Username. -func Password(options ...Option) (usr string, pw string, ok bool, err error) { - return password(applyOptions(options)) -} - // EntryText returns an Option to set the entry text. func EntryText(text string) Option { return funcOption(func(o *options) { o.entryText = text }) @@ -28,8 +19,3 @@ func EntryText(text string) Option { func HideText() Option { return funcOption(func(o *options) { o.hideText = true }) } - -// Username returns an Option to display the username (Unix only). -func Username() Option { - return funcOption(func(o *options) { o.username = true }) -} diff --git a/entry_darwin.go b/entry_darwin.go index 5f0da90..6de4e61 100644 --- a/entry_darwin.go +++ b/entry_darwin.go @@ -18,9 +18,3 @@ func entry(text string, opts options) (string, bool, error) { out, err := zenutil.Run(opts.ctx, "dialog", data) return strResult(opts, out, err) } - -func password(opts options) (string, string, bool, error) { - opts.hideText = true - str, ok, err := entry("Password:", opts) - return "", str, ok, err -} diff --git a/entry_test.go b/entry_test.go index 698d312..ee3cbc2 100644 --- a/entry_test.go +++ b/entry_test.go @@ -16,11 +16,6 @@ func ExampleEntry() { // Output: } -func ExamplePassword() { - zenity.Password(zenity.Title("Type your password")) - // Output: -} - func TestEntryTimeout(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) @@ -41,24 +36,3 @@ func TestEntryCancel(t *testing.T) { t.Error("was not canceled:", err) } } - -func TestPasswordTimeout(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) - - _, _, _, err := zenity.Password(zenity.Context(ctx)) - if !os.IsTimeout(err) { - t.Error("did not timeout:", err) - } - - cancel() -} - -func TestPasswordCancel(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - cancel() - - _, _, _, err := zenity.Password(zenity.Context(ctx)) - if !errors.Is(err, context.Canceled) { - t.Error("was not canceled:", err) - } -} diff --git a/entry_unix.go b/entry_unix.go index dca67de..a688033 100644 --- a/entry_unix.go +++ b/entry_unix.go @@ -3,8 +3,6 @@ package zenity import ( - "strings" - "github.com/ncruces/zenity/internal/zenutil" ) @@ -24,21 +22,3 @@ func entry(text string, opts options) (string, bool, error) { out, err := zenutil.Run(opts.ctx, args) return strResult(opts, out, err) } - -func password(opts options) (string, string, bool, error) { - args := []string{"--password"} - args = appendTitle(args, opts) - args = appendButtons(args, opts) - if opts.username { - args = append(args, "--username") - } - - out, err := zenutil.Run(opts.ctx, args) - str, ok, err := strResult(opts, out, err) - if ok && opts.username { - if split := strings.SplitN(string(out), "|", 2); len(split) == 2 { - return split[0], split[1], true, nil - } - } - return "", str, ok, err -} diff --git a/entry_windows.go b/entry_windows.go index b81607c..009a7b7 100644 --- a/entry_windows.go +++ b/entry_windows.go @@ -156,9 +156,3 @@ func entry(text string, opts options) (out string, ok bool, err error) { } return out, ok, err } - -func password(opts options) (string, string, bool, error) { - opts.hideText = true - str, ok, err := entry("Password:", opts) - return "", str, ok, err -} diff --git a/pwd.go b/pwd.go new file mode 100644 index 0000000..685c129 --- /dev/null +++ b/pwd.go @@ -0,0 +1,15 @@ +package zenity + +// Password displays the password dialog. +// +// Returns false on cancel, or ErrExtraButton. +// +// Valid options: Title, OKLabel, CancelLabel, ExtraButton, Icon, Username. +func Password(options ...Option) (usr string, pw string, ok bool, err error) { + return password(applyOptions(options)) +} + +// Username returns an Option to display the username (Unix only). +func Username() Option { + return funcOption(func(o *options) { o.username = true }) +} diff --git a/pwd_stub.go b/pwd_stub.go new file mode 100644 index 0000000..63bc24a --- /dev/null +++ b/pwd_stub.go @@ -0,0 +1,9 @@ +// +build windows darwin + +package zenity + +func password(opts options) (string, string, bool, error) { + opts.hideText = true + str, ok, err := entry("Password:", opts) + return "", str, ok, err +} diff --git a/pwd_test.go b/pwd_test.go new file mode 100644 index 0000000..7b6df89 --- /dev/null +++ b/pwd_test.go @@ -0,0 +1,37 @@ +package zenity_test + +import ( + "context" + "errors" + "os" + "testing" + "time" + + "github.com/ncruces/zenity" +) + +func ExamplePassword() { + zenity.Password(zenity.Title("Type your password")) + // Output: +} + +func TestPasswordTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, _, _, err := zenity.Password(zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout:", err) + } + + cancel() +} + +func TestPasswordCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, _, _, err := zenity.Password(zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("was not canceled:", err) + } +} diff --git a/pwd_unix.go b/pwd_unix.go new file mode 100644 index 0000000..dc19245 --- /dev/null +++ b/pwd_unix.go @@ -0,0 +1,27 @@ +// +build !windows,!darwin + +package zenity + +import ( + "strings" + + "github.com/ncruces/zenity/internal/zenutil" +) + +func password(opts options) (string, string, bool, error) { + args := []string{"--password"} + args = appendTitle(args, opts) + args = appendButtons(args, opts) + if opts.username { + args = append(args, "--username") + } + + out, err := zenutil.Run(opts.ctx, args) + str, ok, err := strResult(opts, out, err) + if ok && opts.username { + if split := strings.SplitN(string(out), "|", 2); len(split) == 2 { + return split[0], split[1], true, nil + } + } + return "", str, ok, err +}