Calendar (unix).
This commit is contained in:
parent
238d216de3
commit
ff30098813
4 changed files with 97 additions and 0 deletions
22
date.go
Normal file
22
date.go
Normal file
|
@ -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
|
||||
})
|
||||
}
|
38
date_test.go
Normal file
38
date_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
34
date_unix.go
Normal file
34
date_unix.go
Normal file
|
@ -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
|
||||
}
|
|
@ -53,6 +53,9 @@ type options struct {
|
|||
disallowEmpty bool
|
||||
defaultItems []string
|
||||
|
||||
// Calendar options
|
||||
year, month, day int
|
||||
|
||||
// File selection options
|
||||
directory bool
|
||||
confirmOverwrite bool
|
||||
|
|
Loading…
Reference in a new issue