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.
//
// 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
})
}

View file

@ -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 {

View file

@ -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)

View file

@ -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}}

View file

@ -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)

View file

@ -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)
}

View file

@ -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
}

View file

@ -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:

View file

@ -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