From dcc3b6536ba6b2a6b4dca2eba422a513413fa2a0 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Sun, 25 Aug 2024 22:05:40 -0700 Subject: [PATCH 01/14] Press t to go to current time --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index 2383ffa..c80b4c7 100644 --- a/main.go +++ b/main.go @@ -90,6 +90,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.hour++ } + case "t": + Now = NewClock(0) + m.hour = Now.Time().Hour() + case "d": m.showDates = !m.showDates } From 3cec8c82f1d645433b63391516e6cd675257072c Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Sun, 25 Aug 2024 23:53:39 -0700 Subject: [PATCH 02/14] Update clock as you move, allow day movements, add month to view --- clock.go | 15 +++++++++++++++ main.go | 10 +++++++++- zone.go | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/clock.go b/clock.go index c1dde0f..b442406 100644 --- a/clock.go +++ b/clock.go @@ -27,6 +27,21 @@ func (c *Clock) AddDays(n int) { c.t = c.t.AddDate(0, 0, n) } +// AddDays adds n days to the current date. +func (c *Clock) AddHours(n int) { + c.t = time.Date( + c.t.Year(), // Year + c.t.Month(), // Month + c.t.Day(), // Day + c.t.Hour(), // Hour + 0, // Minutes set to 0 + 0, // Seconds set to 0 + 0, // Nanoseconds set to 0 + c.t.Location(), // Location (timezone) + ) + c.t = c.t.Add(time.Hour * time.Duration(n)) +} + // Get the wrapped time.Time struct func (c *Clock) Time() time.Time { return c.t diff --git a/main.go b/main.go index c80b4c7..3db1f89 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+c", "q", "esc": return m, tea.Quit + case "H": + Now.AddDays(-1) + case "left", "h": if m.hour == 0 { m.hour = 23 @@ -81,6 +84,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } else { m.hour-- } + Now.AddHours(-1) + + case "L": + Now.AddDays(1) case "right", "l": if m.hour > 22 { @@ -89,6 +96,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } else { m.hour++ } + Now.AddHours(1) case "t": Now = NewClock(0) @@ -140,7 +148,7 @@ func main() { zones: config.Zones, now: Now.Time(), hour: Now.Time().Hour(), - showDates: false, + showDates: true, isMilitary: *military, } diff --git a/zone.go b/zone.go index b64ff24..f711a3e 100644 --- a/zone.go +++ b/zone.go @@ -64,12 +64,12 @@ func (z Zone) ClockEmoji() string { // ShortDT returns the current time in short format. func (z Zone) ShortDT() string { - return z.currentTime().Format("3:04PM, Mon 02") + return z.currentTime().Format("3:04PM, Mon Jan 02") } // ShortMT returns the current military time in short format. func (z Zone) ShortMT() string { - return z.currentTime().Format("15:04, Mon 02") + return z.currentTime().Format("15:04, Mon Jan 02") } func (z Zone) currentTime() time.Time { From 42ebf82e6056c254641c7b2eab58f712048da945 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 00:04:19 -0700 Subject: [PATCH 03/14] Update clock on minute tick --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 3db1f89..2f689f5 100644 --- a/main.go +++ b/main.go @@ -108,6 +108,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tickMsg: m.now = time.Time(msg) + Now = NewClock(0) return m, tick() } return m, nil From 35cd553fdf5b3d029b7029b12dacd2675b7cc723 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:35:17 -0700 Subject: [PATCH 04/14] Scroll by week and day --- main.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2f689f5..1bbca74 100644 --- a/main.go +++ b/main.go @@ -74,9 +74,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+c", "q", "esc": return m, tea.Quit - case "H": - Now.AddDays(-1) - case "left", "h": if m.hour == 0 { m.hour = 23 @@ -86,9 +83,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } Now.AddHours(-1) - case "L": - Now.AddDays(1) - case "right", "l": if m.hour > 22 { m.hour = 0 @@ -98,6 +92,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } Now.AddHours(1) + case "H": + Now.AddDays(-1) + + case "L": + Now.AddDays(1) + + case "<": + Now.AddDays(-7) + + case ">": + Now.AddDays(7) + case "t": Now = NewClock(0) m.hour = Now.Time().Hour() From 798ad5448520cd32c612951cba329a662c7beff4 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:36:10 -0700 Subject: [PATCH 05/14] Open on timeanddate.com --- main.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/main.go b/main.go index 1bbca74..a88aaf9 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,8 @@ import ( "flag" "fmt" "os" + "os/exec" + "runtime" "strings" "time" @@ -47,6 +49,30 @@ func tick() tea.Cmd { }) } +func openURL(url string) error { + var cmd *exec.Cmd + + switch runtime.GOOS { + case "windows": + cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url) + case "darwin": + cmd = exec.Command("open", url) + case "linux": + cmd = exec.Command("xdg-open", url) + default: + return fmt.Errorf("unsupported platform") + } + + return cmd.Start() +} + +func openInTimeAndDateDotCom(t time.Time) error { + utcTime := t.In(time.UTC).Format("20060102T150405") + url := fmt.Sprintf("https://www.timeanddate.com/worldclock/converter.html?iso=%s&p1=1440", utcTime) + + return openURL(url) +} + type model struct { zones []*Zone now time.Time @@ -104,6 +130,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case ">": Now.AddDays(7) + case "o": + openInTimeAndDateDotCom(Now.Time()) + case "t": Now = NewClock(0) m.hour = Now.Time().Hour() From 0a8a7e66cae8a010b819b2e3e8bba2ded9901cbf Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:36:23 -0700 Subject: [PATCH 06/14] watch flag --- main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a88aaf9..0802dd7 100644 --- a/main.go +++ b/main.go @@ -80,6 +80,7 @@ type model struct { showDates bool interactive bool isMilitary bool + watch bool } func (m model) Init() tea.Cmd { @@ -142,8 +143,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case tickMsg: - m.now = time.Time(msg) - Now = NewClock(0) + if m.watch { + m.now = time.Time(msg) + Now = NewClock(0) + m.hour = Now.Time().Hour() + } return m, tick() } return m, nil @@ -155,6 +159,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") + watch := flag.Bool("w", false, "watch live, set time to now every minute") flag.Parse() if *showVersion == true { @@ -186,6 +191,7 @@ func main() { hour: Now.Time().Hour(), showDates: true, isMilitary: *military, + watch: *watch, } initialModel.interactive = !*exitQuick From 1786583f4ab062e4726bf65d6994ccb36303f86b Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:37:13 -0700 Subject: [PATCH 07/14] Use time color as highlighted bg color, right align time --- view.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/view.go b/view.go index 61df62e..353d3d8 100644 --- a/view.go +++ b/view.go @@ -47,7 +47,7 @@ func (m model) View() string { out = out.Foreground(term.Color(hourColorCode(hour))) // Cursor if m.hour == i-startHour { - out = out.Background(term.Color("#00B67F")) + out = out.Background(term.Color(hourColorCode(hour))) if hasDarkBackground { out = out.Foreground(term.Color("#262626")).Bold() } else { @@ -77,7 +77,7 @@ func (m model) View() string { datetime = zone.ShortDT() } - zoneHeader := fmt.Sprintf("%s %s %s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(datetime)) + zoneHeader := fmt.Sprintf("%s %-60s %76s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(datetime)) s += fmt.Sprintf(" %s\n %s\n %s\n", zoneHeader, hours.String(), dates.String()) } From 6a0ece59c931538efd90063f43d603d5278be8e5 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:38:39 -0700 Subject: [PATCH 08/14] Add year to time --- zone.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zone.go b/zone.go index f711a3e..32ad9f7 100644 --- a/zone.go +++ b/zone.go @@ -64,12 +64,12 @@ func (z Zone) ClockEmoji() string { // ShortDT returns the current time in short format. func (z Zone) ShortDT() string { - return z.currentTime().Format("3:04PM, Mon Jan 02") + return z.currentTime().Format("3:04PM, Mon Jan 02, 2006") } // ShortMT returns the current military time in short format. func (z Zone) ShortMT() string { - return z.currentTime().Format("15:04, Mon Jan 02") + return z.currentTime().Format("15:04, Mon Jan 02, 2006") } func (z Zone) currentTime() time.Time { From 0d734bad966a1c5005e0f29429c3e6ebbddb38ac Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:42:09 -0700 Subject: [PATCH 09/14] status line for latest commands --- view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view.go b/view.go index 353d3d8..6f70049 100644 --- a/view.go +++ b/view.go @@ -89,7 +89,7 @@ func (m model) View() string { } func status() string { - text := " q: quit, d: toggle date" + text := " q: quit, h/l: hours, H/L: days, : weeks, d: toggle date, t: now, o: open in web" for { text += " " if len(text) > UIWidth { From 488fd9488979f4fa864b19cf1996b753e011c7ac Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:40:05 -0700 Subject: [PATCH 10/14] Fix double day jump bug --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 0802dd7..40de61f 100644 --- a/main.go +++ b/main.go @@ -104,7 +104,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "left", "h": if m.hour == 0 { m.hour = 23 - Now.AddDays(-1) } else { m.hour-- } @@ -113,7 +112,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "right", "l": if m.hour > 22 { m.hour = 0 - Now.AddDays(1) } else { m.hour++ } From a4d6e54748f9d31f12500d1da809e50e9b662d70 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:40:22 -0700 Subject: [PATCH 11/14] Revert showDates default to false --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 40de61f..aa4963a 100644 --- a/main.go +++ b/main.go @@ -187,7 +187,7 @@ func main() { zones: config.Zones, now: Now.Time(), hour: Now.Time().Hour(), - showDates: true, + showDates: false, isMilitary: *military, watch: *watch, } From 55333eae26fc37ad5c03032028cb597761a8ce32 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:40:40 -0700 Subject: [PATCH 12/14] Add help toggle --- main.go | 5 +++++ view.go | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index aa4963a..93ce3b2 100644 --- a/main.go +++ b/main.go @@ -81,6 +81,7 @@ type model struct { interactive bool isMilitary bool watch bool + showHelp bool } func (m model) Init() tea.Cmd { @@ -136,6 +137,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { Now = NewClock(0) m.hour = Now.Time().Hour() + case "?": + m.showHelp = !m.showHelp + case "d": m.showDates = !m.showDates } @@ -190,6 +194,7 @@ func main() { showDates: false, isMilitary: *military, watch: *watch, + showHelp: false, } initialModel.interactive = !*exitQuick diff --git a/view.go b/view.go index 6f70049..257a9d1 100644 --- a/view.go +++ b/view.go @@ -83,13 +83,21 @@ func (m model) View() string { } if m.interactive { - s += status() + s += status(m) } return s } -func status() string { - text := " q: quit, h/l: hours, H/L: days, : weeks, d: toggle date, t: now, o: open in web" +func status(m model) string { + + var text string + + if m.showHelp { + text = " q: quit, ?: help, h/l: hours, H/L: days, : weeks, d: toggle date, t: now, o: open in web" + } else { + text = " q: quit, ?: help" + } + for { text += " " if len(text) > UIWidth { From 11efc358e217ba3541f73f81d5efd89cebd92b1c Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Tue, 27 Aug 2024 20:21:19 -0700 Subject: [PATCH 13/14] Consolidate clocks --- clock.go | 18 ++++++++-------- config.go | 5 ++--- main.go | 58 +++++++++++++++++++++++----------------------------- main_test.go | 39 ++++++++++++++++++++++++----------- view.go | 14 ++++++------- zone.go | 23 ++++++++++----------- 6 files changed, 82 insertions(+), 75 deletions(-) diff --git a/clock.go b/clock.go index b442406..c58918e 100644 --- a/clock.go +++ b/clock.go @@ -27,17 +27,17 @@ func (c *Clock) AddDays(n int) { c.t = c.t.AddDate(0, 0, n) } -// AddDays adds n days to the current date. +// AddDays adds n days to the current date and clears the minutes func (c *Clock) AddHours(n int) { c.t = time.Date( - c.t.Year(), // Year - c.t.Month(), // Month - c.t.Day(), // Day - c.t.Hour(), // Hour - 0, // Minutes set to 0 - 0, // Seconds set to 0 - 0, // Nanoseconds set to 0 - c.t.Location(), // Location (timezone) + c.t.Year(), + c.t.Month(), + c.t.Day(), + c.t.Hour(), + 0, // Minutes set to 0 + 0, // Seconds set to 0 + 0, // Nanoseconds set to 0 + c.t.Location(), ) c.t = c.t.Add(time.Hour * time.Duration(n)) } diff --git a/config.go b/config.go index b5dce5f..202dbfb 100644 --- a/config.go +++ b/config.go @@ -47,8 +47,7 @@ func LoadConfig(tzConfigs []string) (*Config, error) { zones := make([]*Zone, len(tzConfigs)+1) // Setup with Local time zone - now := Now.Time() - localZoneName, _ := now.Zone() + localZoneName, _ := time.Now().Zone() zones[0] = &Zone{ Name: fmt.Sprintf("(%s) Local", localZoneName), DbName: localZoneName, @@ -56,7 +55,7 @@ func LoadConfig(tzConfigs []string) (*Config, error) { // Add zones from TZ_LIST for i, zoneConf := range tzConfigs { - zone, err := SetupZone(now, zoneConf) + zone, err := SetupZone(time.Now(), zoneConf) if err != nil { return nil, err } diff --git a/main.go b/main.go index 93ce3b2..d4a7c12 100644 --- a/main.go +++ b/main.go @@ -35,9 +35,6 @@ const CurrentVersion = "0.7.0" var ( term = termenv.ColorProfile() hasDarkBackground = termenv.HasDarkBackground() - - // Now is used around tz to share/set the current time. - Now *Clock = NewClock(0) ) type tickMsg time.Time @@ -75,8 +72,7 @@ func openInTimeAndDateDotCom(t time.Time) error { type model struct { zones []*Zone - now time.Time - hour int + clock Clock showDates bool interactive bool isMilitary bool @@ -103,39 +99,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit case "left", "h": - if m.hour == 0 { - m.hour = 23 - } else { - m.hour-- - } - Now.AddHours(-1) + m.clock.AddHours(-1) case "right", "l": - if m.hour > 22 { - m.hour = 0 - } else { - m.hour++ - } - Now.AddHours(1) + m.clock.AddHours(1) case "H": - Now.AddDays(-1) + m.clock.AddDays(-1) case "L": - Now.AddDays(1) + m.clock.AddDays(1) case "<": - Now.AddDays(-7) + m.clock.AddDays(-7) case ">": - Now.AddDays(7) + m.clock.AddDays(7) case "o": - openInTimeAndDateDotCom(Now.Time()) + openInTimeAndDateDotCom(m.clock.Time()) case "t": - Now = NewClock(0) - m.hour = Now.Time().Hour() + m.clock = *NewClock(0) case "?": m.showHelp = !m.showHelp @@ -146,9 +131,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tickMsg: if m.watch { - m.now = time.Time(msg) - Now = NewClock(0) - m.hour = Now.Time().Hour() + m.clock = *NewClock(0) } return m, tick() } @@ -179,9 +162,6 @@ func main() { os.Exit(0) } - if *when != 0 { - Now = NewClock(*when) - } config, err := LoadConfig(flag.Args()) if err != nil { fmt.Fprintf(os.Stderr, "Config error: %s\n", err) @@ -189,19 +169,33 @@ func main() { } var initialModel = model{ zones: config.Zones, - now: Now.Time(), - hour: Now.Time().Hour(), + clock: *NewClock(0), showDates: false, isMilitary: *military, watch: *watch, showHelp: false, } + if *when != 0 { + initialModel.clock = *NewClock(*when) + } + initialModel.interactive = !*exitQuick + // if len(os.Getenv("DEBUG")) > 0 { + // f, err := tea.LogToFile("debug.log", "debug") + // if err != nil { + // fmt.Println("fatal:", err) + // os.Exit(1) + // } + // defer f.Close() + // } + p := tea.NewProgram(initialModel) if err := p.Start(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1) } + + // loadConfigFile() } diff --git a/main_test.go b/main_test.go index 96758ca..0a7ef2a 100644 --- a/main_test.go +++ b/main_test.go @@ -19,10 +19,27 @@ package main import ( "strings" "testing" + "time" tea "github.com/charmbracelet/bubbletea" ) +func getTimestampWithHour(hour int) int64 { + if hour == -1 { + hour = time.Now().Hour() + } + return time.Date( + time.Now().Year(), + time.Now().Month(), + time.Now().Day(), + hour, + 0, // Minutes set to 0 + 0, // Seconds set to 0 + 0, // Nanoseconds set to 0 + time.Now().Location(), + ).Unix() +} + func TestUpdateIncHour(t *testing.T) { // "l" key -> go right msg := tea.KeyMsg{ @@ -45,19 +62,18 @@ func TestUpdateIncHour(t *testing.T) { for _, test := range tests { m := model{ zones: DefaultZones, - hour: test.startHour, + clock: *NewClock(getTimestampWithHour(test.startHour)), } - // Do we enjoy global mutable state? - db := Now.Time().Day() + db := m.clock.Time().Day() nextState, cmd := m.Update(msg) - da := Now.Time().Day() + da := m.clock.Time().Day() if cmd != nil { t.Errorf("Expected nil Cmd, but got %v", cmd) return } - h := nextState.(model).hour + h := nextState.(model).clock.t.Hour() if h != test.nextHour { t.Errorf("Expected %d, but got %d", test.nextHour, h) } @@ -88,14 +104,14 @@ func TestUpdateDecHour(t *testing.T) { for _, test := range tests { m := model{ zones: DefaultZones, - hour: test.startHour, + clock: *NewClock(getTimestampWithHour(test.startHour)), } nextState, cmd := m.Update(msg) if cmd != nil { t.Errorf("Expected nil Cmd, but got %v", cmd) return } - h := nextState.(model).hour + h := nextState.(model).clock.t.Hour() if h != test.nextHour { t.Errorf("Expected %d, but got %d", test.nextHour, h) } @@ -112,7 +128,7 @@ func TestUpdateQuitMsg(t *testing.T) { m := model{ zones: DefaultZones, - hour: 10, + clock: *NewClock(getTimestampWithHour(-1)), } _, cmd := m.Update(msg) if cmd == nil { @@ -126,13 +142,12 @@ func TestUpdateQuitMsg(t *testing.T) { func TestMilitaryTime(t *testing.T) { m := model{ zones: DefaultZones, - hour: 14, - now: Now.Time(), + clock: *NewClock(getTimestampWithHour(-1)), isMilitary: true, showDates: true, } s := m.View() - if !strings.Contains(s, m.now.Format("15:04")) { - t.Errorf("Expected military time of %s, but got %s", m.now.Format("15:04"), s) + if !strings.Contains(s, m.clock.t.Format("15:04")) { + t.Errorf("Expected military time of %s, but got %s", m.clock.t.Format("15:04"), s) } } diff --git a/view.go b/view.go index 257a9d1..ce67826 100644 --- a/view.go +++ b/view.go @@ -36,7 +36,7 @@ func (m model) View() string { startHour := 0 if zi > 0 { - startHour = (zone.currentTime().Hour() - m.zones[0].currentTime().Hour()) % 24 + startHour = (zone.currentTime(m.clock.t).Hour() - m.zones[0].currentTime(m.clock.t).Hour()) % 24 } dateChanged := false @@ -46,7 +46,7 @@ func (m model) View() string { out = out.Foreground(term.Color(hourColorCode(hour))) // Cursor - if m.hour == i-startHour { + if m.clock.t.Hour() == i-startHour { out = out.Background(term.Color(hourColorCode(hour))) if hasDarkBackground { out = out.Foreground(term.Color("#262626")).Bold() @@ -72,12 +72,12 @@ func (m model) View() string { var datetime string if m.isMilitary { - datetime = zone.ShortMT() + datetime = zone.ShortMT(m.clock.t) } else { - datetime = zone.ShortDT() + datetime = zone.ShortDT(m.clock.t) } - zoneHeader := fmt.Sprintf("%s %-60s %76s", zone.ClockEmoji(), normalTextStyle(zone.String()), dateTimeStyle(datetime)) + zoneHeader := fmt.Sprintf("%s %-60s %76s", zone.ClockEmoji(m.clock.t), normalTextStyle(zone.String()), dateTimeStyle(datetime)) s += fmt.Sprintf(" %s\n %s\n %s\n", zoneHeader, hours.String(), dates.String()) } @@ -117,8 +117,8 @@ func status(m model) string { } func formatDayChange(m *model, z *Zone) string { - zTime := z.currentTime() - if zTime.Hour() > m.now.Hour() { + zTime := z.currentTime(m.clock.t) + if zTime.Hour() > m.clock.t.Hour() { zTime = zTime.AddDate(0, 0, 1) } diff --git a/zone.go b/zone.go index 32ad9f7..32ddac6 100644 --- a/zone.go +++ b/zone.go @@ -57,30 +57,29 @@ func (z Zone) String() string { } // ClockEmoji returns the corresponding emoji clock for a given hour -func (z Zone) ClockEmoji() string { - h := ((z.currentTime().Hour() % 12) + 12) % 12 +func (z Zone) ClockEmoji(t time.Time) string { + h := ((z.currentTime(t).Hour() % 12) + 12) % 12 return EmojiClocks[h] } // ShortDT returns the current time in short format. -func (z Zone) ShortDT() string { - return z.currentTime().Format("3:04PM, Mon Jan 02, 2006") +func (z Zone) ShortDT(t time.Time) string { + return z.currentTime(t).Format("3:04PM, Mon Jan 02, 2006") } // ShortMT returns the current military time in short format. -func (z Zone) ShortMT() string { - return z.currentTime().Format("15:04, Mon Jan 02, 2006") +func (z Zone) ShortMT(t time.Time) string { + return z.currentTime(t).Format("15:04, Mon Jan 02, 2006") } -func (z Zone) currentTime() time.Time { - now := Now.Time() - zName, _ := now.Zone() +func (z Zone) currentTime(t time.Time) time.Time { + zName, _ := t.Zone() if z.DbName != zName { loc, err := time.LoadLocation(z.DbName) if err != nil { - return now + return t } - return now.In(loc) + return t.In(loc) } - return now + return t } From a0fc73907be65d17219cc85a1236eeaab56c20a1 Mon Sep 17 00:00:00 2001 From: Justin Wyne <1986068+wyne@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:17:41 -0700 Subject: [PATCH 14/14] Fix test --- main.go | 15 ++------------- main_test.go | 4 ++-- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index d4a7c12..8432c60 100644 --- a/main.go +++ b/main.go @@ -90,7 +90,7 @@ func (m model) Init() tea.Cmd { return tick() } -func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: @@ -182,20 +182,9 @@ func main() { initialModel.interactive = !*exitQuick - // if len(os.Getenv("DEBUG")) > 0 { - // f, err := tea.LogToFile("debug.log", "debug") - // if err != nil { - // fmt.Println("fatal:", err) - // os.Exit(1) - // } - // defer f.Close() - // } - - p := tea.NewProgram(initialModel) + p := tea.NewProgram(&initialModel) if err := p.Start(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) os.Exit(1) } - - // loadConfigFile() } diff --git a/main_test.go b/main_test.go index 0a7ef2a..8d87616 100644 --- a/main_test.go +++ b/main_test.go @@ -73,7 +73,7 @@ func TestUpdateIncHour(t *testing.T) { t.Errorf("Expected nil Cmd, but got %v", cmd) return } - h := nextState.(model).clock.t.Hour() + h := nextState.(*model).clock.t.Hour() if h != test.nextHour { t.Errorf("Expected %d, but got %d", test.nextHour, h) } @@ -111,7 +111,7 @@ func TestUpdateDecHour(t *testing.T) { t.Errorf("Expected nil Cmd, but got %v", cmd) return } - h := nextState.(model).clock.t.Hour() + h := nextState.(*model).clock.t.Hour() if h != test.nextHour { t.Errorf("Expected %d, but got %d", test.nextHour, h) }