From ff300988131d001634790bea3fa5219c42544d47 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 12 Apr 2021 03:02:21 +0100 Subject: [PATCH] Calendar (unix). --- date.go | 22 ++++++++++++++++++++++ date_test.go | 38 ++++++++++++++++++++++++++++++++++++++ date_unix.go | 34 ++++++++++++++++++++++++++++++++++ zenity.go | 3 +++ 4 files changed, 97 insertions(+) create mode 100644 date.go create mode 100644 date_test.go create mode 100644 date_unix.go diff --git a/date.go b/date.go new file mode 100644 index 0000000..4aedf9d --- /dev/null +++ b/date.go @@ -0,0 +1,22 @@ +package zenity + +import ( + "time" +) + +// Calendar displays the calendar dialog. +// +// Returns zero on cancel. +// +// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, +// Icon, Date. +func Calendar(text string, options ...Option) (time.Time, error) { + return calendar(text, applyOptions(options)) +} + +// 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 + }) +} diff --git a/date_test.go b/date_test.go new file mode 100644 index 0000000..c5cccf5 --- /dev/null +++ b/date_test.go @@ -0,0 +1,38 @@ +package zenity_test + +import ( + "context" + "errors" + "os" + "testing" + "time" + + "github.com/ncruces/zenity" +) + +func ExampleCalendar() { + zenity.Calendar("Select a date from below:", + zenity.DefaultDate(2006, time.January, 1)) + // Output: +} + +func TestCalendarTimeout(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second/10) + + _, err := zenity.Calendar("", zenity.Context(ctx)) + if !os.IsTimeout(err) { + t.Error("did not timeout:", err) + } + + cancel() +} + +func TestCalendarCancel(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, err := zenity.Calendar("", zenity.Context(ctx)) + if !errors.Is(err, context.Canceled) { + t.Error("was not canceled:", err) + } +} diff --git a/date_unix.go b/date_unix.go new file mode 100644 index 0000000..7197150 --- /dev/null +++ b/date_unix.go @@ -0,0 +1,34 @@ +// +build !windows,!darwin + +package zenity + +import ( + "strconv" + "time" + + "github.com/ncruces/zenity/internal/zenutil" +) + +func calendar(text string, opts options) (time.Time, error) { + args := []string{"--calendar", "--text", text, "--date-format=%F"} + args = appendTitle(args, opts) + args = appendButtons(args, opts) + args = appendWidthHeight(args, opts) + args = appendIcon(args, opts) + if opts.day != 0 { + 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)) + } + + out, err := zenutil.Run(opts.ctx, args) + str, ok, err := strResult(opts, out, err) + if ok { + return time.Parse("2006-01-02", str) + } + return time.Time{}, err +} diff --git a/zenity.go b/zenity.go index b3314f0..1e33d34 100644 --- a/zenity.go +++ b/zenity.go @@ -53,6 +53,9 @@ type options struct { disallowEmpty bool defaultItems []string + // Calendar options + year, month, day int + // File selection options directory bool confirmOverwrite bool