Username (macOS).

This commit is contained in:
Nuno Cruces 2022-05-11 17:49:15 +01:00
parent 8277866bb0
commit 0abd40cb11
18 changed files with 185 additions and 41 deletions

View File

@ -61,6 +61,9 @@ var (
entryText string
hideText bool
// Password options
username bool
// List options
columns int
allowEmpty bool
@ -219,6 +222,9 @@ func setupFlags() {
flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`")
flag.BoolVar(&hideText, "hide-text", false, "Hide the entry text")
// Password options
flag.BoolVar(&username, "username", false, "Display the username option")
// List options
flag.Func("column", "Set the column `header`", addColumn)
flag.Bool("hide-header", true, "Hide the column headers")
@ -435,6 +441,12 @@ func loadFlags() []zenity.Option {
opts = append(opts, zenity.HideText())
}
// Password options
if username {
opts = append(opts, zenity.Username())
}
// List options
if !allowEmpty {

View File

@ -1,8 +1,6 @@
package zenity
import (
"time"
)
import "time"
// Calendar displays the calendar dialog.
//

View File

@ -1,8 +1,6 @@
package zenity
import (
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func selectFile(opts options) (string, error) {
var data zenutil.File

View File

@ -52,7 +52,7 @@ var res=alert.runModal
switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn:$.exit(1)}
var fmt=$.NSDateFormatter.alloc.init
fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX")
fmt.locale=$.NSLocale.localeWithLocaleIdentifier('en_US_POSIX')
fmt.dateFormat={{json .Format}}
fmt.stringFromDate(date.dateValue)
{{- end}}
@ -70,7 +70,7 @@ try{var res=app.{{.Operation}}({{json .Text}},opts)}catch(e){if(e.errorNumber===
$.dprintf(2,e)
$.exit(-1)}
if(res.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned)
if(res.buttonReturned==={{json .Extra}}){$.puts({{json .Extra}})
$.exit(1)}
res.textReturned
{{- end}}
@ -118,4 +118,31 @@ if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1)
continue}
var i=parseInt(s)
if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}}
{{- end}}
{{define "pwd" -}}
var app=Application.currentApplication()
app.includeStandardAdditions=true
app.activate()
ObjC.import('stdio')
ObjC.import('stdlib')
var opts={{json .Options}}
{{- if .IconPath}}
opts.withIcon=Path({{json .IconPath}})
{{- end}}
function dialog(text){try{var res=app.displayDialog(text,opts)}catch(e){if(e.errorNumber===-128)$.exit(1)
$.dprintf(2,e)
$.exit(-1)}
if(res.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts({{json .Extra}})
$.exit(1)}
return res.textReturned}
var start=Date.now()
opts.defaultAnswer=''
var username=dialog('Username:')
{{- if .Options.Timeout}}
opts.givingUpAfter-=(Date.now()-start)/1000|0
{{- end}}
opts.hiddenAnswer=true
var password=dialog('Password:')
username+{{json .Separator}}+password
{{- end}}`))

View File

@ -35,6 +35,6 @@ case $.NSAlertSecondButtonReturn:
}
var fmt = $.NSDateFormatter.alloc.init
fmt.locale = $.NSLocale.localeWithLocaleIdentifier("en_US_POSIX")
fmt.locale = $.NSLocale.localeWithLocaleIdentifier('en_US_POSIX')
fmt.dateFormat = {{json .Format}}
fmt.stringFromDate(date.dateValue)

View File

@ -22,7 +22,7 @@ if (res.gaveUp) {
$.exit(5)
}
if (res.buttonReturned === {{json .Extra}}) {
$.puts(res.buttonReturned)
$.puts({{json .Extra}})
$.exit(1)
}
res.textReturned

View File

@ -0,0 +1,43 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
ObjC.import('stdio')
ObjC.import('stdlib')
var opts = {{json .Options}}
{{- if .IconPath}}
opts.withIcon = Path({{json .IconPath}})
{{- end}}
function dialog(text) {
try {
var res = app.displayDialog(text, opts)
} catch (e) {
if (e.errorNumber === -128) $.exit(1)
$.dprintf(2, e)
$.exit(-1)
}
if (res.gaveUp) {
$.exit(5)
}
if (res.buttonReturned === {{json .Extra}}) {
$.puts({{json .Extra}})
$.exit(1)
}
return res.textReturned
}
var start = Date.now()
opts.defaultAnswer = ''
var username = dialog('Username:')
{{- if .Options.Timeout}}
opts.givingUpAfter -= (Date.now() - start) / 1000 |0
{{- end}}
opts.hiddenAnswer = true
var password = dialog('Password:')
username + {{json .Separator}} + password

View File

@ -147,6 +147,24 @@ type DialogOptions struct {
Timeout int `json:"givingUpAfter,omitempty"`
}
// Password is internal.
type Password struct {
Separator string
Extra *string
Options PasswordOptions
IconPath string
}
// PasswordOptions is internal.
type PasswordOptions struct {
Title *string `json:"withTitle,omitempty"`
Icon string `json:"withIcon,omitempty"`
Buttons []string `json:"buttons,omitempty"`
Cancel int `json:"cancelButton,omitempty"`
Default int `json:"defaultButton,omitempty"`
Timeout int `json:"givingUpAfter,omitempty"`
}
// DialogButtons is internal.
type DialogButtons struct {
Buttons []string
@ -166,6 +184,17 @@ func (d *Dialog) SetButtons(btns DialogButtons) {
}
}
// SetButtons is internal.
func (d *Password) SetButtons(btns DialogButtons) {
d.Options.Buttons = btns.Buttons
d.Options.Default = btns.Default
d.Options.Cancel = btns.Cancel
if btns.Extra > 0 {
name := btns.Buttons[btns.Extra-1]
d.Extra = &name
}
}
// List is internal.
type List struct {
Items []string

View File

@ -2,9 +2,7 @@
package zenity
import (
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func list(text string, items []string, opts options) (string, error) {
args := []string{"--list", "--column=", "--hide-header", "--text", text}

View File

@ -2,9 +2,7 @@
package zenity
import (
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func message(kind messageKind, text string, opts options) error {
args := []string{"--text", text, "--no-markup"}

View File

@ -2,9 +2,7 @@
package zenity
import (
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func notify(text string, opts options) error {
args := []string{"--notification", "--text", text}

View File

@ -2,9 +2,7 @@
package zenity
import (
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func progress(opts options) (ProgressDialog, error) {
args := []string{"--progress"}

33
pwd_darwin.go Normal file
View File

@ -0,0 +1,33 @@
package zenity
import (
"os"
"github.com/ncruces/zenity/internal/zenutil"
)
func password(opts options) (string, string, error) {
if !opts.username {
opts.hideText = true
str, err := entry("Password:", opts)
return "", str, err
}
var data zenutil.Password
data.Separator = zenutil.Separator
data.Options.Title = opts.title
data.Options.Timeout = zenutil.Timeout
if opts.customIcon != "" {
_, err := os.Stat(opts.customIcon)
if err != nil {
return "", "", err
}
data.IconPath = opts.customIcon
} else {
data.Options.Icon = opts.icon.String()
}
data.SetButtons(getButtons(true, true, opts))
out, err := zenutil.Run(opts.ctx, "pwd", data)
return pwdResult(zenutil.Separator, opts, out, err)
}

View File

@ -60,7 +60,7 @@ func TestPassword_username(t *testing.T) {
if skip, err := skip(err); skip {
t.Skip("skipping:", err)
}
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
if runtime.GOOS == "windows" {
if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err)
} else {

View File

@ -2,12 +2,7 @@
package zenity
import (
"os/exec"
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
import "github.com/ncruces/zenity/internal/zenutil"
func password(opts options) (string, string, error) {
args := []string{"--password"}
@ -18,14 +13,5 @@ func password(opts options) (string, string, error) {
}
out, err := zenutil.Run(opts.ctx, args)
str, err := strResult(opts, out, err)
if opts.username {
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 255 {
return "", "", ErrUnsupported
}
if split := strings.SplitN(str, "|", 2); err == nil && len(split) == 2 {
return split[0], split[1], nil
}
}
return "", str, err
return pwdResult("|", opts, out, err)
}

View File

@ -1,5 +1,3 @@
//go:build windows || darwin
package zenity
import "fmt"

14
scripts/password.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
ENTRY=`zenity --password --username`
case $? in
0)
echo "User Name: `echo $ENTRY | cut -d'|' -f1`"
echo "Password : `echo $ENTRY | cut -d'|' -f2`"
;;
1)
echo "Stop login.";;
-1)
echo "An unexpected error has occurred.";;
esac

View File

@ -82,3 +82,17 @@ func lstResult(opts options, out []byte, err error) ([]string, error) {
}
return strings.Split(str, zenutil.Separator), nil
}
func pwdResult(sep string, opts options, out []byte, err error) (string, string, error) {
str, err := strResult(opts, out, err)
if opts.username {
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 255 {
return "", "", ErrUnsupported
}
if split := strings.SplitN(str, sep, 2); err == nil && len(split) == 2 {
return split[0], split[1], nil
}
}
return "", str, err
}