Calendar API.

This commit is contained in:
Nuno Cruces 2022-03-28 20:22:39 +01:00
parent d239bcb824
commit 6494eb386b
9 changed files with 68 additions and 23 deletions

View file

@ -6,10 +6,10 @@ import (
// Calendar displays the calendar dialog. // Calendar displays the calendar dialog.
// //
// Returns zero on cancel.
//
// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// Icon, Date. // Icon, DefaultDate.
//
// May return: ErrCanceled.
func Calendar(text string, options ...Option) (time.Time, error) { func Calendar(text string, options ...Option) (time.Time, error) {
return calendar(text, applyOptions(options)) 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. // DefaultDate returns an Option to set the date.
func DefaultDate(year int, month time.Month, day int) Option { func DefaultDate(year int, month time.Month, day int) Option {
return funcOption(func(o *options) { return funcOption(func(o *options) {
o.year, o.month, o.day = year, int(month), day o.year, o.month, o.day = &year, month, day
}) })
} }

View file

@ -9,7 +9,18 @@ import (
func calendar(text string, opts options) (time.Time, error) { func calendar(text string, opts options) (time.Time, error) {
var date zenutil.Date 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" date.Format = "yyyy-MM-dd"
if opts.title != nil { if opts.title != nil {

View file

@ -15,14 +15,14 @@ func calendar(text string, opts options) (time.Time, error) {
args = appendButtons(args, opts) args = appendButtons(args, opts)
args = appendWidthHeight(args, opts) args = appendWidthHeight(args, opts)
args = appendIcon(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)) args = append(args, "--day", strconv.Itoa(opts.day))
} }
if opts.month != 0 { if opts.year != nil {
args = append(args, "--month", strconv.Itoa(opts.month)) args = append(args, "--year", strconv.Itoa(*opts.year))
}
if opts.year != 0 {
args = append(args, "--year", strconv.Itoa(opts.year))
} }
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)

View file

@ -34,10 +34,16 @@ date.setFrameSize(date.fittingSize)
var alert=$.NSAlert.alloc.init var alert=$.NSAlert.alloc.init
alert.setAccessoryView(date) alert.setAccessoryView(date)
alert.setMessageText({{json .Text}}) alert.setMessageText({{json .Text}})
{{- if .Info}}alert.setInformativeText({{json .Info}}){{- end}} alert.addButtonWithTitle({{json .OK}})
{{- range .Buttons}}alert.addButtonWithTitle([{{json .}}]){{end}} alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent='\033'
{{- if .Info}}
alert.setInformativeText({{json .Info}})
{{- end}}
{{- if .Extra}}
alert.addButtonWithTitle({{json .Extra}})
{{- end}}
var res=alert.runModal var res=alert.runModal
switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Buttons}}[2]) 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.dateFormat={{json .Format}} fmt.dateFormat={{json .Format}}

View file

@ -4,6 +4,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -22,6 +23,7 @@ func main() {
} }
var str strings.Builder var str strings.Builder
funcs := template.FuncMap{"json": json.Marshal}
for _, file := range files { for _, file := range files {
name := file.Name() name := file.Name()
@ -29,6 +31,10 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
_, err = template.New(file.Name()).Funcs(funcs).Parse(string(data))
if err != nil {
log.Fatal(err)
}
data, err = minify(data) data, err = minify(data)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -15,13 +15,19 @@ date.setFrameSize(date.fittingSize)
var alert = $.NSAlert.alloc.init var alert = $.NSAlert.alloc.init
alert.setAccessoryView(date) alert.setAccessoryView(date)
alert.setMessageText({{json .Text}}) alert.setMessageText({{json .Text}})
{{- if .Info}}alert.setInformativeText({{json .Info}}){{- end}} alert.addButtonWithTitle({{json .OK}})
{{- range .Buttons}}alert.addButtonWithTitle([{{json .}}]){{end}} alert.addButtonWithTitle({{json .Cancel}}).keyEquivalent = '\033'
{{- if .Info}}
alert.setInformativeText({{json .Info}})
{{- end}}
{{- if .Extra}}
alert.addButtonWithTitle({{json .Extra}})
{{- end}}
var res = alert.runModal var res = alert.runModal
switch (res) { switch (res) {
case $.NSAlertThirdButtonReturn: case $.NSAlertThirdButtonReturn:
$.puts({{json .Buttons}}[2]) $.puts({{json .Extra}})
case $.NSAlertSecondButtonReturn: case $.NSAlertSecondButtonReturn:
$.exit(1) $.exit(1)
} }

View file

@ -190,6 +190,7 @@ type File struct {
Options FileOptions Options FileOptions
} }
// FileOptions is internal.
type FileOptions struct { type FileOptions struct {
Prompt *string `json:"withPrompt,omitempty"` Prompt *string `json:"withPrompt,omitempty"`
Type []string `json:"ofType,omitempty"` Type []string `json:"ofType,omitempty"`
@ -219,9 +220,11 @@ type Progress struct {
// Date is internal. // Date is internal.
type Date struct { type Date struct {
Date int64 Date int64
Text string Text string
Info string Info string
Format string Format string
Buttons []string OK string
Cancel string
Extra *string
} }

View file

@ -48,6 +48,16 @@ func getButtons(dialog, okcancel bool, opts options) (btns zenutil.DialogButtons
return 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 { func (i DialogIcon) String() string {
switch i { switch i {
case ErrorIcon: case ErrorIcon:

View file

@ -13,6 +13,7 @@ package zenity
import ( import (
"context" "context"
"image/color" "image/color"
"time"
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
@ -54,7 +55,9 @@ type options struct {
defaultItems []string defaultItems []string
// Calendar options // Calendar options
year, month, day int month time.Month
day int
year *int
// File selection options // File selection options
directory bool directory bool