aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-03-13 00:17:16 +0900
committernsfisis <nsfisis@gmail.com>2023-03-13 00:17:16 +0900
commit8490d6166249c31972adeb2de0f39b424eee7352 (patch)
tree1e1f096a0bef2126198ec83e69d62c619e08db7f
parentc32f49625c3be59bb10d4d471446580a19557250 (diff)
downloadterm-clock-8490d6166249c31972adeb2de0f39b424eee7352.tar.gz
term-clock-8490d6166249c31972adeb2de0f39b424eee7352.tar.zst
term-clock-8490d6166249c31972adeb2de0f39b424eee7352.zip
feat(alarm): add alarm subcommand
-rw-r--r--cmd/alarm.go62
-rw-r--r--cmd/root.go1
2 files changed, 63 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),
+}
diff --git a/cmd/root.go b/cmd/root.go
index 34a12f3..3efadab 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -10,6 +10,7 @@ var rootCmd = &cobra.Command{
}
func init() {
+ rootCmd.AddCommand(alarmCmd)
rootCmd.AddCommand(clockCmd)
rootCmd.AddCommand(timerCmd)
}