Refactor.

This commit is contained in:
Nuno Cruces 2021-04-09 01:26:57 +01:00
parent 1ca281d701
commit 75aca1cc3c
9 changed files with 88 additions and 72 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

15
pwd.go Normal file
View file

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

9
pwd_stub.go Normal file
View file

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

37
pwd_test.go Normal file
View file

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

27
pwd_unix.go Normal file
View file

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