diff --git a/date_darwin.go b/date_darwin.go new file mode 100644 index 0000000..bc05c73 --- /dev/null +++ b/date_darwin.go @@ -0,0 +1,28 @@ +package zenity + +import ( + "time" + + "github.com/ncruces/zenity/internal/zenutil" +) + +func calendar(text string, opts options) (time.Time, error) { + var date zenutil.Date + + date.Date = time.Now().Unix() + date.Format = "yyyy-MM-dd" + + if opts.title != nil { + date.Text = *opts.title + date.Info = text + } else { + date.Text = text + } + + out, err := zenutil.Run(opts.ctx, "date", date) + str, err := strResult(opts, out, err) + if err != nil { + return time.Time{}, err + } + return time.Parse("2006-01-02", str) +} diff --git a/date_unix.go b/date_unix.go index 7197150..9fcb7ac 100644 --- a/date_unix.go +++ b/date_unix.go @@ -1,4 +1,4 @@ -// +build !windows,!darwin +//go:build !windows && !darwin package zenity @@ -26,9 +26,9 @@ func calendar(text string, opts options) (time.Time, error) { } out, err := zenutil.Run(opts.ctx, args) - str, ok, err := strResult(opts, out, err) - if ok { - return time.Parse("2006-01-02", str) + str, err := strResult(opts, out, err) + if err != nil { + return time.Time{}, err } - return time.Time{}, err + return time.Parse("2006-01-02", str) } diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index de571e3..c780ffb 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -19,6 +19,30 @@ app.activate() var res=app.chooseColor({defaultColor:{{json .}}}) {'rgb('+res.map(x=>Math.round(x*255))+')'} {{- end}} +{{define "date" -}} +var app=Application.currentApplication() +app.includeStandardAdditions=true +app.activate() +ObjC.import('Cocoa') +ObjC.import('stdio') +ObjC.import('stdlib') +var date=$.NSDatePicker.alloc.init +date.setDatePickerStyle($.NSDatePickerStyleClockAndCalendar) +date.setDatePickerElements($.NSDatePickerElementFlagYearMonthDay) +date.setDateValue($.NSDate.dateWithTimeIntervalSince1970({{.Date}})) +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}} +var res=alert.runModal +switch(res){case $.NSAlertThirdButtonReturn:$.puts({{json .Buttons}}[2]) +case $.NSAlertSecondButtonReturn:$.exit(1)} +var fmt=$.NSDateFormatter.alloc.init +fmt.dateFormat={{json .Format}} +fmt.stringFromDate(date.dateValue) +{{- end}} {{define "dialog" -}} var app=Application.currentApplication() app.includeStandardAdditions=true diff --git a/internal/zenutil/osascripts/date.gojs b/internal/zenutil/osascripts/date.gojs new file mode 100644 index 0000000..a4f7125 --- /dev/null +++ b/internal/zenutil/osascripts/date.gojs @@ -0,0 +1,31 @@ +var app = Application.currentApplication() +app.includeStandardAdditions = true +app.activate() + +ObjC.import('Cocoa') +ObjC.import('stdio') +ObjC.import('stdlib') + +var date = $.NSDatePicker.alloc.init +date.setDatePickerStyle($.NSDatePickerStyleClockAndCalendar) +date.setDatePickerElements($.NSDatePickerElementFlagYearMonthDay) +date.setDateValue($.NSDate.dateWithTimeIntervalSince1970({{.Date}})) +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}} + +var res = alert.runModal +switch (res) { +case $.NSAlertThirdButtonReturn: + $.puts({{json .Buttons}}[2]) +case $.NSAlertSecondButtonReturn: + $.exit(1) +} + +var fmt = $.NSDateFormatter.alloc.init +fmt.dateFormat = {{json .Format}} +fmt.stringFromDate(date.dateValue) \ No newline at end of file diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index d7e9b87..663c0ec 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -216,3 +216,12 @@ type Progress struct { Description *string Total *int } + +// Date is internal. +type Date struct { + Date int64 + Text string + Info string + Format string + Buttons []string +}