diff options
Diffstat (limited to 'cmd/alarm.go')
| -rw-r--r-- | cmd/alarm.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/cmd/alarm.go b/cmd/alarm.go new file mode 100644 index 0000000..0846baa --- /dev/null +++ b/cmd/alarm.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "log" + "time" + + "github.com/spf13/cobra" + + "github.com/nsfisis/term-clock/internal/term" +) + +func drawAlarm(scr *term.Screen, now time.Time, alarmTime time.Time, bgStyle, fgStyle term.Style) { + h1, m1, s1 := now.Clock() + h2, m2, s2 := alarmTime.Clock() + + if h1*3600+m1*60+s1 >= h2*3600+m2*60+s2 { + bgStyle, fgStyle = fgStyle, bgStyle + } + + drawClock(scr, now, bgStyle, fgStyle) +} + +func cmdAlarm(cmd *cobra.Command, args []string) { + alarmTime, err := time.Parse("15:04", args[0]) + if err != nil { + log.Fatalf("%+v", err) + } + + scr, err := term.NewScreen() + if err != nil { + log.Fatalf("%+v", err) + } + defer scr.Close() + + drawAlarm(scr, time.Now(), alarmTime, term.BgStyle, term.FgStyle) + + scr.OnResize(func() bool { + drawAlarm(scr, time.Now(), alarmTime, term.BgStyle, term.FgStyle) + return false + }) + go scr.DoEventLoop() + + t := time.NewTicker(1 * time.Second) + defer t.Stop() + + for { + select { + case <-scr.QuitC: + return + case now := <-t.C: + drawAlarm(scr, now, alarmTime, term.BgStyle, term.FgStyle) + scr.Show() + } + } +} + +var alarmCmd = &cobra.Command{ + Use: "alarm", + Short: "Alarm mode", + Run: cmdAlarm, + Args: cobra.ExactArgs(1), +} |
