From 6494eb386b6bcc99d14e4876d2ba9efe602e6dfa Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 28 Mar 2022 20:22:39 +0100 Subject: [PATCH] Calendar API. --- date.go | 8 ++++---- date_darwin.go | 13 ++++++++++++- date_unix.go | 12 ++++++------ internal/zenutil/osa_generated.go | 12 +++++++++--- internal/zenutil/osa_generator.go | 6 ++++++ internal/zenutil/osascripts/date.gojs | 12 +++++++++--- internal/zenutil/run_darwin.go | 13 ++++++++----- util_darwin.go | 10 ++++++++++ zenity.go | 5 ++++- 9 files changed, 68 insertions(+), 23 deletions(-) diff --git a/date.go b/date.go index 4aedf9d..2925a63 100644 --- a/date.go +++ b/date.go @@ -6,10 +6,10 @@ import ( // Calendar displays the calendar dialog. // -// Returns zero on cancel. -// // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, -// Icon, Date. +// Icon, DefaultDate. +// +// May return: ErrCanceled. func Calendar(text string, options ...Option) (time.Time, error) { return calendar(text, applyOptions(options)) } @@ -17,6 +17,6 @@ func Calendar(text string, options ...Option) (time.Time, error) { // DefaultDate returns an Option to set the date. func DefaultDate(year int, month time.Month, day int) Option { return funcOption(func(o *options) { - o.year, o.month, o.day = year, int(month), day + o.year, o.month, o.day = &year, month, day }) } diff --git a/date_darwin.go b/date_darwin.go index bc05c73..9734a10 100644 --- a/date_darwin.go +++ b/date_darwin.go @@ -9,7 +9,18 @@ import ( func calendar(text string, opts options) (time.Time, error) { var date zenutil.Date - date.Date = time.Now().Unix() + year, month, day := time.Now().Date() + if time.January <= opts.month && opts.month <= time.December { + month = opts.month + } + if 1 <= opts.day && opts.day <= 31 { + day = opts.day + } + if opts.year != nil { + year = *opts.year + } + date.Date = time.Date(year, month, day, 0, 0, 0, 0, time.UTC).Unix() + date.OK, date.Cancel, date.Extra = getAlertButtons(opts) date.Format = "yyyy-MM-dd" if opts.title != nil { diff --git a/date_unix.go b/date_unix.go index 9fcb7ac..491ce3a 100644 --- a/date_unix.go +++ b/date_unix.go @@ -15,14 +15,14 @@ func calendar(text string, opts options) (time.Time, error) { args = appendButtons(args, opts) args = appendWidthHeight(args, opts) args = appendIcon(args, opts) - if opts.day != 0 { + if time.January <= opts.month && opts.month <= time.December { + args = append(args, "--month", strconv.Itoa(int(opts.month))) + } + if 1 <= opts.day && opts.day <= 31 { args = append(args, "--day", strconv.Itoa(opts.day)) } - if opts.month != 0 { - args = append(args, "--month", strconv.Itoa(opts.month)) - } - if opts.year != 0 { - args = append(args, "--year", strconv.Itoa(opts.year)) + if opts.year != nil { + args = append(args, "--year", strconv.Itoa(*opts.year)) } out, err := zenutil.Run(opts.ctx, args) diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index c780ffb..a36961d 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -34,10 +34,16 @@ date.setFrameSize(date.fittingSize) var alert=$.NSAlert.alloc.init alert.setAccessoryView(date) alert.setMessageText({{json .Text}}) -{{- if .Info}}alert.setInformativeText({{json .Info}}){{- end}} -{{- range .Buttons}}alert.addButtonWithTitle([{{json .}}]){{end}} +alert.addButtonWithTitle({{json .OK}}) +alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent='\033' +{{- if .Info}} +alert.setInformativeText({{json .Info}}) +{{- end}} +{{- if .Extra}} +alert.addButtonWithTitle({{json .Extra}}) +{{- end}} var res=alert.runModal -switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Buttons}}[2]) +switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Extra}}) case $.NSAlertSecondButtonReturn:$.exit(1)} var fmt=$.NSDateFormatter.alloc.init fmt.dateFormat={{json .Format}} diff --git a/internal/zenutil/osa_generator.go b/internal/zenutil/osa_generator.go index 7312c71..b41ebc7 100644 --- a/internal/zenutil/osa_generator.go +++ b/internal/zenutil/osa_generator.go @@ -4,6 +4,7 @@ package main import ( "bytes" + "encoding/json" "log" "os" "path/filepath" @@ -22,6 +23,7 @@ func main() { } var str strings.Builder + funcs := template.FuncMap{"json": json.Marshal} for _, file := range files { name := file.Name() @@ -29,6 +31,10 @@ func main() { if err != nil { log.Fatal(err) } + _, err = template.New(file.Name()).Funcs(funcs).Parse(string(data)) + if err != nil { + log.Fatal(err) + } data, err = minify(data) if err != nil { log.Fatal(err) diff --git a/internal/zenutil/osascripts/date.gojs b/internal/zenutil/osascripts/date.gojs index a4f7125..76daed4 100644 --- a/internal/zenutil/osascripts/date.gojs +++ b/internal/zenutil/osascripts/date.gojs @@ -15,13 +15,19 @@ date.setFrameSize(date.fittingSize) var alert = $.NSAlert.alloc.init alert.setAccessoryView(date) alert.setMessageText({{json .Text}}) -{{- if .Info}}alert.setInformativeText({{json .Info}}){{- end}} -{{- range .Buttons}}alert.addButtonWithTitle([{{json .}}]){{end}} +alert.addButtonWithTitle({{json .OK}}) +alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033' +{{- if .Info}} + alert.setInformativeText({{json .Info}}) +{{- end}} +{{- if .Extra}} + alert.addButtonWithTitle({{json .Extra}}) +{{- end}} var res = alert.runModal switch (res) { case $.NSAlertThirdButtonReturn: - $.puts({{json .Buttons}}[2]) + $.puts({{json .Extra}}) case $.NSAlertSecondButtonReturn: $.exit(1) } diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index 663c0ec..c375cdd 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -190,6 +190,7 @@ type File struct { Options FileOptions } +// FileOptions is internal. type FileOptions struct { Prompt *string `json:"withPrompt,omitempty"` Type []string `json:"ofType,omitempty"` @@ -219,9 +220,11 @@ type Progress struct { // Date is internal. type Date struct { - Date int64 - Text string - Info string - Format string - Buttons []string + Date int64 + Text string + Info string + Format string + OK string + Cancel string + Extra *string } diff --git a/util_darwin.go b/util_darwin.go index b05c1c6..f0318d2 100644 --- a/util_darwin.go +++ b/util_darwin.go @@ -48,6 +48,16 @@ func getButtons(dialog, okcancel bool, opts options) (btns zenutil.DialogButtons return } +func getAlertButtons(opts options) (ok, cancel string, extra *string) { + if opts.okLabel == nil { + opts.okLabel = stringPtr("OK") + } + if opts.cancelLabel == nil { + opts.cancelLabel = stringPtr("Cancel") + } + return *opts.okLabel, *opts.cancelLabel, opts.extraButton +} + func (i DialogIcon) String() string { switch i { case ErrorIcon: diff --git a/zenity.go b/zenity.go index 1e33d34..3ffb744 100644 --- a/zenity.go +++ b/zenity.go @@ -13,6 +13,7 @@ package zenity import ( "context" "image/color" + "time" "github.com/ncruces/zenity/internal/zenutil" ) @@ -54,7 +55,9 @@ type options struct { defaultItems []string // Calendar options - year, month, day int + month time.Month + day int + year *int // File selection options directory bool