aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-03-08 01:56:58 +0900
committernsfisis <nsfisis@gmail.com>2023-03-08 01:56:58 +0900
commit31c5dbe675cba2d53816f12f482a29952de39de8 (patch)
treeab407874649a36d7ae27d83ec3456fb0b6d1de60
parent527079fa9c466eb1cff44a952d4f99800682a582 (diff)
downloadterm-clock-31c5dbe675cba2d53816f12f482a29952de39de8.tar.gz
term-clock-31c5dbe675cba2d53816f12f482a29952de39de8.tar.zst
term-clock-31c5dbe675cba2d53816f12f482a29952de39de8.zip
add timer subcommand
-rw-r--r--cmd/root.go1
-rw-r--r--cmd/timer.go118
2 files changed, 119 insertions, 0 deletions
diff --git a/cmd/root.go b/cmd/root.go
index d0f9713..34a12f3 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -11,6 +11,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(clockCmd)
+ rootCmd.AddCommand(timerCmd)
}
func Execute() error {
diff --git a/cmd/timer.go b/cmd/timer.go
new file mode 100644
index 0000000..0a47f8d
--- /dev/null
+++ b/cmd/timer.go
@@ -0,0 +1,118 @@
+package cmd
+
+import (
+ "log"
+ "time"
+
+ "github.com/gdamore/tcell/v2"
+ "github.com/spf13/cobra"
+)
+
+func drawTimer(scr tcell.Screen, time time.Duration, elapsed time.Duration, bgStyle, clockStyle tcell.Style) {
+ left := time-elapsed
+ if left<0{
+ left=0
+ bgStyle, clockStyle = clockStyle, bgStyle
+ }
+
+ // Clear the entire screen.
+ scr.SetStyle(bgStyle)
+ scr.Clear()
+
+ // Calculate square width/height and offset.
+ scrW, scrH := scr.Size()
+ // 17
+ // <--------------->
+ // ### ### ### ### ^
+ // # # # # # # # # # |
+ // # # # # # # # # | 5
+ // # # # # # # # # # |
+ // ### ### ### ### v
+ squareW := scrW / (17 + 2)
+ squareH := scrH / (5 + 2)
+ if squareH > squareW {
+ squareH = squareW
+ }
+ if squareW > squareH*3/2 {
+ squareW = squareH * 3 / 2
+ }
+ xOffset := (scrW - squareW*17) / 2
+ yOffset := (scrH - squareH*5) / 2
+
+ // Minute
+ minute := left.Minutes()
+ drawNumber(scr, int(minute)/10, xOffset+squareW*0, yOffset, squareW, squareH, clockStyle)
+ drawNumber(scr, int(minute)%10, xOffset+squareW*4, yOffset, squareW, squareH, clockStyle)
+
+ // Colon
+ drawSquare(scr, xOffset+squareW*8, yOffset+squareH*1, squareW, squareH, clockStyle)
+ drawSquare(scr, xOffset+squareW*8, yOffset+squareH*3, squareW, squareH, clockStyle)
+
+ // Second
+ second := left.Seconds()
+ drawNumber(scr, int(second)/10, xOffset+squareW*10, yOffset, squareW, squareH, clockStyle)
+ drawNumber(scr, int(second)%10, xOffset+squareW*14, yOffset, squareW, squareH, clockStyle)
+}
+
+func cmdTimer(cmd *cobra.Command, args []string) {
+ timerTime, err := time.ParseDuration(args[0])
+ if err != nil {
+ log.Fatalf("%+v", err)
+ }
+
+ bgStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
+ clockStyle := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorOlive)
+
+ scr, err := tcell.NewScreen()
+ if err != nil {
+ log.Fatalf("%+v", err)
+ }
+ if err := scr.Init(); err != nil {
+ log.Fatalf("%+v", err)
+ }
+ defer scr.Fini()
+
+ startTime := time.Now()
+
+ drawTimer(scr, timerTime, time.Now().Sub(startTime), bgStyle, clockStyle)
+
+ quitC := make(chan struct{})
+
+ go func() {
+ for {
+ scr.Show()
+
+ ev := scr.PollEvent()
+ switch ev := ev.(type) {
+ case *tcell.EventResize:
+ drawTimer(scr, timerTime, time.Now().Sub(startTime), bgStyle, clockStyle)
+ scr.Sync()
+ case *tcell.EventKey:
+ if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC || ev.Rune() == 'q' {
+ close(quitC)
+ return
+ }
+ }
+ }
+ }()
+
+ t := time.NewTicker(1 * time.Second)
+ defer t.Stop()
+
+ for {
+ select {
+ case <-quitC:
+ return
+ case now := <-t.C:
+ drawTimer(scr, timerTime, now.Sub(startTime), bgStyle, clockStyle)
+ scr.Show()
+ }
+ }
+}
+
+var timerCmd =&cobra.Command{
+ Use: "timer",
+ Short: "Timer mode",
+ Run: cmdTimer,
+ Args: cobra.ExactArgs(1),
+}