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

View file

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

View file

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

View file

@ -52,7 +52,7 @@ var res=alert.runModal
switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}}) switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn:$.exit(1)} case $.NSAlertSecondButtonReturn:$.exit(1)}
var fmt=$.NSDateFormatter.alloc.init var fmt=$.NSDateFormatter.alloc.init
fmt.locale=$.NSLocale.localeWithLocaleIdentifier("en_US_POSIX") fmt.locale=$.NSLocale.localeWithLocaleIdentifier('en_US_POSIX')
fmt.dateFormat={{json .Format}} fmt.dateFormat={{json .Format}}
fmt.stringFromDate(date.dateValue) fmt.stringFromDate(date.dateValue)
{{- end}} {{- end}}
@ -70,7 +70,7 @@ try{var res=app.{{.Operation}}({{json .Text}},opts)}catch(e){if(e.errorNumber===
$.dprintf(2,e) $.dprintf(2,e)
$.exit(-1)} $.exit(-1)}
if(res.gaveUp){$.exit(5)} if(res.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned) if(res.buttonReturned==={{json .Extra}}){$.puts({{json .Extra}})
$.exit(1)} $.exit(1)}
res.textReturned res.textReturned
{{- end}} {{- end}}
@ -118,4 +118,31 @@ if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1)
continue} continue}
var i=parseInt(s) var i=parseInt(s)
if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}} 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}}`)) {{- end}}`))

View file

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

View file

@ -22,7 +22,7 @@ if (res.gaveUp) {
$.exit(5) $.exit(5)
} }
if (res.buttonReturned === {{json .Extra}}) { if (res.buttonReturned === {{json .Extra}}) {
$.puts(res.buttonReturned) $.puts({{json .Extra}})
$.exit(1) $.exit(1)
} }
res.textReturned 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"` 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. // DialogButtons is internal.
type DialogButtons struct { type DialogButtons struct {
Buttons []string 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. // List is internal.
type List struct { type List struct {
Items []string Items []string

View file

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

View file

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

View file

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

View file

@ -2,9 +2,7 @@
package zenity package zenity
import ( import "github.com/ncruces/zenity/internal/zenutil"
"github.com/ncruces/zenity/internal/zenutil"
)
func progress(opts options) (ProgressDialog, error) { func progress(opts options) (ProgressDialog, error) {
args := []string{"--progress"} 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 { if skip, err := skip(err); skip {
t.Skip("skipping:", err) t.Skip("skipping:", err)
} }
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { if runtime.GOOS == "windows" {
if errors.Is(err, zenity.ErrUnsupported) { if errors.Is(err, zenity.ErrUnsupported) {
t.Skip("was not unsupported:", err) t.Skip("was not unsupported:", err)
} else { } else {

View file

@ -2,12 +2,7 @@
package zenity package zenity
import ( import "github.com/ncruces/zenity/internal/zenutil"
"os/exec"
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
func password(opts options) (string, string, error) { func password(opts options) (string, string, error) {
args := []string{"--password"} args := []string{"--password"}
@ -18,14 +13,5 @@ func password(opts options) (string, string, error) {
} }
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
str, err := strResult(opts, out, err) return pwdResult("|", 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
} }

View file

@ -1,5 +1,3 @@
//go:build windows || darwin
package zenity package zenity
import "fmt" 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 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
}