From 78e9c28b70429145b09bca73aac9d9916a7b65cb Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 28 Feb 2023 00:44:43 +0900 Subject: initial implementation --- cmd/clock.go | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 18 ++++++ 2 files changed, 221 insertions(+) create mode 100644 cmd/clock.go create mode 100644 cmd/root.go (limited to 'cmd') diff --git a/cmd/clock.go b/cmd/clock.go new file mode 100644 index 0000000..69c125d --- /dev/null +++ b/cmd/clock.go @@ -0,0 +1,203 @@ +package cmd + +import ( + "log" + "time" + + "github.com/gdamore/tcell/v2" + "github.com/spf13/cobra" +) + +func drawSquare(scr tcell.Screen, xOffset, yOffset, w, h int, style tcell.Style) { + for dx := 0; dx < w; dx++ { + x := xOffset + dx + for dy := 0; dy < h; dy++ { + y := yOffset + dy + scr.SetContent(x, y, ' ', nil, style) + } + } +} + +func drawNumber(scr tcell.Screen, n, xOffset, yOffset, squareW, squareH int, style tcell.Style) { + defs := [...][15]bool{ + { + true, true, true, + true, false, true, + true, false, true, + true, false, true, + true, true, true, + }, + { + false, false, true, + false, false, true, + false, false, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + false, false, true, + true, true, true, + true, false, false, + true, true, true, + }, + { + true, true, true, + false, false, true, + true, true, true, + false, false, true, + true, true, true, + }, + { + true, false, true, + true, false, true, + true, true, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + true, false, false, + true, true, true, + false, false, true, + true, true, true, + }, + { + true, true, true, + true, false, false, + true, true, true, + true, false, true, + true, true, true, + }, + { + true, true, true, + false, false, true, + false, false, true, + false, false, true, + false, false, true, + }, + { + true, true, true, + true, false, true, + true, true, true, + true, false, true, + true, true, true, + }, + { + true, true, true, + true, false, true, + true, true, true, + false, false, true, + true, true, true, + }, + } + + squares := defs[n] + for i, draw := range squares { + if !draw { + continue + } + x := i % 3 + y := i / 3 + drawSquare(scr, xOffset+squareW*x, yOffset+squareH*y, squareW, squareH, style) + } +} + +func drawClock(scr tcell.Screen, now time.Time, bgStyle, clockStyle tcell.Style) { + // 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 + + // Hour + hour := now.Hour() + drawNumber(scr, hour/10, xOffset+squareW*0, yOffset, squareW, squareH, clockStyle) + drawNumber(scr, hour%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) + + // Minute + minute := now.Minute() + drawNumber(scr, minute/10, xOffset+squareW*10, yOffset, squareW, squareH, clockStyle) + drawNumber(scr, minute%10, xOffset+squareW*14, yOffset, squareW, squareH, clockStyle) +} + +func cmdClock(cmd *cobra.Command, args []string) { + 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() + + drawClock(scr, time.Now(), bgStyle, clockStyle) + + quitC := make(chan struct{}) + + go func() { + for { + scr.Show() + + ev := scr.PollEvent() + switch ev := ev.(type) { + case *tcell.EventResize: + drawClock(scr, time.Now(), 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() + + prev := time.Now() + for { + select { + case <-quitC: + return + case now := <-t.C: + if now.Minute() != prev.Minute() { + drawClock(scr, now, bgStyle, clockStyle) + scr.Show() + prev = now + } + } + } +} + +var clockCmd =&cobra.Command{ + Use: "clock", + Short: "Clock mode", + Run: cmdClock, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..d0f9713 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "term-clock", + Short: "A clock on your terminal", +} + +func init() { + rootCmd.AddCommand(clockCmd) +} + +func Execute() error { + return rootCmd.Execute() +} -- cgit v1.2.3-70-g09d2