Entry (macOS), see #3.

This commit is contained in:
Nuno Cruces 2021-03-05 02:01:59 +00:00
parent 93e49a6b03
commit b2df443d8c
9 changed files with 139 additions and 27 deletions

View file

@ -24,6 +24,7 @@ const (
var ( var (
// Application Options // Application Options
notification bool notification bool
entryDlg bool
errorDlg bool errorDlg bool
infoDlg bool infoDlg bool
warningDlg bool warningDlg bool
@ -35,9 +36,13 @@ var (
title string title string
width uint width uint
height uint height uint
text string
// Entry options
entryText string
hideText bool
// Message options // Message options
text string
icon string icon string
okLabel string okLabel string
cancelLabel string cancelLabel string
@ -89,6 +94,9 @@ func main() {
case notification: case notification:
errResult(zenity.Notify(text, opts...)) errResult(zenity.Notify(text, opts...))
case entryDlg:
strResult(zenity.Entry(text, opts...))
case errorDlg: case errorDlg:
okResult(zenity.Error(text, opts...)) okResult(zenity.Error(text, opts...))
case infoDlg: case infoDlg:
@ -118,6 +126,7 @@ func main() {
func setupFlags() { func setupFlags() {
// Application Options // Application Options
flag.BoolVar(&notification, "notification", false, "Display notification") flag.BoolVar(&notification, "notification", false, "Display notification")
flag.BoolVar(&entryDlg, "entry", false, "Display text entry dialog")
flag.BoolVar(&errorDlg, "error", false, "Display error dialog") flag.BoolVar(&errorDlg, "error", false, "Display error dialog")
flag.BoolVar(&infoDlg, "info", false, "Display info dialog") flag.BoolVar(&infoDlg, "info", false, "Display info dialog")
flag.BoolVar(&warningDlg, "warning", false, "Display warning dialog") flag.BoolVar(&warningDlg, "warning", false, "Display warning dialog")
@ -130,9 +139,13 @@ func setupFlags() {
flag.StringVar(&icon, "window-icon", "", "Set the window `icon` (error, info, question, warning)") flag.StringVar(&icon, "window-icon", "", "Set the window `icon` (error, info, question, warning)")
flag.UintVar(&width, "width", 0, "Set the `width`") flag.UintVar(&width, "width", 0, "Set the `width`")
flag.UintVar(&height, "height", 0, "Set the `height`") flag.UintVar(&height, "height", 0, "Set the `height`")
flag.StringVar(&text, "text", "", "Set the dialog `text`")
// Entry options
flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`")
flag.BoolVar(&hideText, "hide-text", false, "Hide the entry text")
// Message options // Message options
flag.StringVar(&text, "text", "", "Set the dialog `text`")
flag.StringVar(&icon, "icon-name", "", "Set the dialog `icon` (dialog-error, dialog-information, dialog-question, dialog-warning)") flag.StringVar(&icon, "icon-name", "", "Set the dialog `icon` (dialog-error, dialog-information, dialog-question, dialog-warning)")
flag.StringVar(&okLabel, "ok-label", "", "Set the label of the OK button") flag.StringVar(&okLabel, "ok-label", "", "Set the label of the OK button")
flag.StringVar(&cancelLabel, "cancel-label", "", "Set the label of the Cancel button") flag.StringVar(&cancelLabel, "cancel-label", "", "Set the label of the Cancel button")
@ -179,6 +192,9 @@ func validateFlags() {
if notification { if notification {
n++ n++
} }
if entryDlg {
n++
}
if errorDlg { if errorDlg {
n++ n++
} }
@ -213,6 +229,11 @@ func loadFlags() []zenity.Option {
} }
} }
switch { switch {
case entryDlg:
setDefault(&title, "Add a new entry")
setDefault(&text, "Enter new text:")
setDefault(&okLabel, "OK")
setDefault(&cancelLabel, "Cancel")
case errorDlg: case errorDlg:
setDefault(&title, "Error") setDefault(&title, "Error")
setDefault(&icon, "dialog-error") setDefault(&icon, "dialog-error")
@ -246,6 +267,12 @@ func loadFlags() []zenity.Option {
opts = append(opts, zenity.Width(width)) opts = append(opts, zenity.Width(width))
opts = append(opts, zenity.Height(height)) opts = append(opts, zenity.Height(height))
// Entry options
opts = append(opts, zenity.EntryText(entryText))
if hideText {
opts = append(opts, zenity.HideText())
}
// Message options // Message options
var ico zenity.DialogIcon var ico zenity.DialogIcon

20
entry.go Normal file
View file

@ -0,0 +1,20 @@
package zenity
// Entry displays the text entry dialog.
//
// Returns nil on cancel.
//
// Valid options: Title, Text, EntryText, HideText.
func Entry(text string, options ...Option) (string, error) {
return entry(text, applyOptions(options))
}
// EntryText returns an Option to set the entry text.
func EntryText(text string) Option {
return funcOption(func(o *options) { o.entryText = text })
}
// HideText returns an Option to hide the entry text.
func HideText() Option {
return funcOption(func(o *options) { o.hideText = true })
}

58
entry_darwin.go Normal file
View file

@ -0,0 +1,58 @@
package zenity
import (
"os/exec"
"github.com/ncruces/zenity/internal/zenutil"
)
func entry(text string, opts options) (string, error) {
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)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return "", ErrExtraButton
}
return "", nil
}
if err != nil {
return "", err
}
return string(out[:len(out)-1]), err
}

View file

@ -17,14 +17,7 @@ app.activate()
var res=app.chooseColor({defaultColor:{{json .}}}) var res=app.chooseColor({defaultColor:{{json .}}})
{"rgb("+res.map(x=>Math.round(x*255))+")"} {"rgb("+res.map(x=>Math.round(x*255))+")"}
{{- end}} {{- end}}
{{define "file" -}} {{define "dialog" -}}
var app=Application.currentApplication()
app.includeStandardAdditions=true
app.activate()
var res=app.{{.Operation}}({{json .Options}})
if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()}
{{- end}}
{{define "msg" -}}
var app=Application.currentApplication() var app=Application.currentApplication()
app.includeStandardAdditions=true app.includeStandardAdditions=true
app.activate() app.activate()
@ -34,7 +27,14 @@ var res=app.{{.Operation}}({{json .Text}},{{json .Options}})
if(res.gaveUp){$.exit(5)} if(res.gaveUp){$.exit(5)}
if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned) if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned)
$.exit(1)} $.exit(1)}
void 0 res.textReturned
{{- end}}
{{define "file" -}}
var app=Application.currentApplication()
app.includeStandardAdditions=true
app.activate()
var res=app.{{.Operation}}({{json .Options}})
if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()}
{{- end}} {{- end}}
{{define "notify" -}} {{define "notify" -}}
var app=Application.currentApplication() var app=Application.currentApplication()

View file

@ -13,3 +13,4 @@ if (res.buttonReturned === {{json .Extra}}) {
$.puts(res.buttonReturned) $.puts(res.buttonReturned)
$.exit(1) $.exit(1)
} }
res.textReturned

View file

@ -64,18 +64,20 @@ type FileOptions struct {
Invisibles bool `json:"invisibles,omitempty"` Invisibles bool `json:"invisibles,omitempty"`
} }
// Msg is internal. // Dialog is internal.
type Msg struct { type Dialog struct {
Operation string Operation string
Text string Text string
Extra *string Extra *string
Options MsgOptions Options DialogOptions
} }
// MsgOptions is internal. // DialogOptions is internal.
type MsgOptions struct { type DialogOptions struct {
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
As string `json:"as,omitempty"` As string `json:"as,omitempty"`
Answer *string `json:"defaultAnswer,omitempty"`
Hidden bool `json:"hiddenAnswer,omitempty"`
Title *string `json:"withTitle,omitempty"` Title *string `json:"withTitle,omitempty"`
Icon string `json:"withIcon,omitempty"` Icon string `json:"withIcon,omitempty"`
Buttons []string `json:"buttons,omitempty"` Buttons []string `json:"buttons,omitempty"`

View file

@ -7,7 +7,7 @@ import (
) )
func message(kind messageKind, text string, opts options) (bool, error) { func message(kind messageKind, text string, opts options) (bool, error) {
var data zenutil.Msg var data zenutil.Dialog
data.Text = text data.Text = text
data.Options.Timeout = zenutil.Timeout data.Options.Timeout = zenutil.Timeout
@ -99,12 +99,12 @@ func message(kind messageKind, text string, opts options) (bool, error) {
} }
} }
out, err := zenutil.Run(opts.ctx, "msg", data) out, err := zenutil.Run(opts.ctx, "dialog", data)
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return false, ErrExtraButton
}
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return false, ErrExtraButton
}
return false, nil return false, nil
} }
if err != nil { if err != nil {

View file

@ -65,11 +65,11 @@ func message(kind messageKind, text string, opts options) (bool, error) {
} }
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return false, ErrExtraButton
}
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return false, ErrExtraButton
}
return false, nil return false, nil
} }
if err != nil { if err != nil {

View file

@ -48,6 +48,10 @@ type options struct {
ellipsize bool ellipsize bool
defaultCancel bool defaultCancel bool
// Entry options
entryText string
hideText bool
// Context for timeout // Context for timeout
ctx context.Context ctx context.Context
} }