aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2021-11-13 10:47:03 +0900
committernsfisis <nsfisis@gmail.com>2021-11-13 10:47:03 +0900
commit9b331f01bfac9d1ef6013c173c07825e0cc233b8 (patch)
tree3018ddf106f64bb2eb556cf9548dc3b27fca749a
downloadbig-clock-mode-9b331f01bfac9d1ef6013c173c07825e0cc233b8.tar.gz
big-clock-mode-9b331f01bfac9d1ef6013c173c07825e0cc233b8.tar.zst
big-clock-mode-9b331f01bfac9d1ef6013c173c07825e0cc233b8.zip
initial commit
-rw-r--r--.gitignore1
-rw-r--r--Makefile13
-rw-r--r--go.mod5
-rw-r--r--go.sum16
-rw-r--r--main.go185
5 files changed, 220 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..141aa54
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/big-clock-mode
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..31a0db7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+BIN := big-clock-mode
+
+all: build
+
+.PHONY: build
+build: $(BIN)
+
+$(BIN): main.go
+ go build -o $(BIN)
+
+.PHONY: clean
+clean:
+ go clean
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..62c1a24
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module github.com/nsfisis/big-clock-mode
+
+go 1.16
+
+require github.com/gdamore/tcell/v2 v2.4.0
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..26b403b
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,16 @@
+github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
+github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
+github.com/gdamore/tcell/v2 v2.4.0 h1:W6dxJEmaxYvhICFoTY3WrLLEXsQ11SaFnKGVEXW57KM=
+github.com/gdamore/tcell/v2 v2.4.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
+github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
+github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
+github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
+github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
+github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
+github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
+golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..27709ec
--- /dev/null
+++ b/main.go
@@ -0,0 +1,185 @@
+package main
+
+import (
+ "log"
+ "time"
+
+ "github.com/gdamore/tcell/v2"
+)
+
+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.SetCell(x, y, 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)
+ 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 main() {
+ 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.NewTimer(30 * time.Second)
+ defer t.Stop()
+
+ for {
+ select {
+ case <-quitC:
+ return
+ case <-t.C:
+ drawClock(scr, time.Now(), bgStyle, clockStyle)
+ }
+ }
+}