diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..44d0d84 --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,15 @@ +--- +version: 3 + +tasks: + run: + cmds: + - go run main.go + + build: + cmds: + - go build + + test: + cmds: + - go test diff --git a/main.go b/main.go index 2383ffa..76086d9 100644 --- a/main.go +++ b/main.go @@ -48,12 +48,13 @@ func tick() tea.Cmd { } type model struct { - zones []*Zone - now time.Time - hour int - showDates bool - interactive bool - isMilitary bool + zones []*Zone + now time.Time + hour int + showDates bool + interactive bool + isMilitary bool + isTwelveHour bool } func (m model) Init() tea.Cmd { @@ -107,6 +108,7 @@ func main() { when := flag.Int64("when", 0, "time in seconds since unix epoch") doSearch := flag.Bool("list", false, "list zones by name") military := flag.Bool("m", false, "use 24-hour time") + twelveHour := flag.Bool("t", false, "use 12-hour time") flag.Parse() if *showVersion == true { @@ -133,11 +135,12 @@ func main() { os.Exit(2) } var initialModel = model{ - zones: config.Zones, - now: Now.Time(), - hour: Now.Time().Hour(), - showDates: false, - isMilitary: *military, + zones: config.Zones, + now: Now.Time(), + hour: Now.Time().Hour(), + showDates: false, + isMilitary: *military, + isTwelveHour: *twelveHour, } initialModel.interactive = !*exitQuick diff --git a/view.go b/view.go index 61df62e..d00fe09 100644 --- a/view.go +++ b/view.go @@ -41,8 +41,24 @@ func (m model) View() string { dateChanged := false for i := startHour; i < startHour+24; i++ { - hour := ((i % 24) + 24) % 24 // mod 24 - out := termenv.String(fmt.Sprintf("%2d", hour)) + var hour int + var period string + + // switch AM/PM vs 24-hour in future view + if m.isTwelveHour { + hour = ((i % 24) + 24) % 12 + if hour == 0 { + hour = 12 + } + if i%24 >= 12 { + period = "PM" + } else { + period = " " + } + } else { + hour = ((i % 24) + 24) % 24 // mod 24 + } + out := termenv.String(fmt.Sprintf("%2d%s", hour, period)) out = out.Foreground(term.Color(hourColorCode(hour))) // Cursor @@ -64,12 +80,20 @@ func (m model) View() string { if hour == 0 { dates.WriteString(formatDayChange(&m, zone)) dateChanged = true + } else if hour == 12 && period != "PM" { + dates.WriteString(formatDayChange(&m, zone)) + dateChanged = true } - if !dateChanged { + + // handle the spacing for the date/calendar icon + if !dateChanged && !m.isTwelveHour { dates.WriteString(" ") + } else if m.isTwelveHour && !dateChanged { + dates.WriteString(" ") } } + // switch current timestamp from 12 to 24 hour var datetime string if m.isMilitary { datetime = zone.ShortMT()