Skip to content

Commit 3fc9585

Browse files
authored
Golang rewrite (#9)
Rewrite of the project from Python to Go. Will enable easier distribution and installation for users in upcoming PRs.
1 parent 10e4dc0 commit 3fc9585

File tree

7 files changed

+260
-372
lines changed

7 files changed

+260
-372
lines changed

go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/joshdk/tty-qlock
2+
3+
go 1.14
4+
5+
require (
6+
github.com/JoelOtter/termloop v0.0.0-20200516124750-8b96492addf4
7+
github.com/mattn/go-runewidth v0.0.9 // indirect
8+
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 // indirect
9+
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/JoelOtter/termloop v0.0.0-20200516124750-8b96492addf4 h1:L1C+9IivrCOvDO4WYJmqfmf1tJpej0gpsTm0MGoL05I=
2+
github.com/JoelOtter/termloop v0.0.0-20200516124750-8b96492addf4/go.mod h1:Tie7OOEgasw91JpzA8UywemPyGehxZ06Gqtl5B1/vXI=
3+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
4+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
5+
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 h1:lh3PyZvY+B9nFliSGTn5uFuqQQJGuNrD0MLCokv09ag=
6+
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Josh Komoroske. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package main
6+
7+
import (
8+
"github.com/JoelOtter/termloop"
9+
"github.com/joshdk/tty-qlock/qlock"
10+
)
11+
12+
func main() {
13+
game := termloop.NewGame()
14+
game.Screen().SetFps(5)
15+
game.Screen().AddEntity(qlock.New(termloop.ColorBlue, termloop.ColorBlack))
16+
game.Start()
17+
}

qlock/board.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright Josh Komoroske. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package qlock
6+
7+
type entry struct {
8+
x int
9+
y int
10+
text string
11+
}
12+
13+
var (
14+
width = 44
15+
height = 20
16+
17+
it = entry{2, 1, "I T"}
18+
is = entry{14, 1, "I S"}
19+
a = entry{2, 3, "A"}
20+
to = entry{38, 7, "T O"}
21+
past = entry{2, 9, "P A S T"}
22+
oclock = entry{22, 19, "O' C L O C K"}
23+
24+
fiveOff = entry{26, 5, "F I V E"}
25+
tenOff = entry{22, 7, "T E N"}
26+
quarter = entry{10, 3, "Q U A R T E R"}
27+
twenty = entry{2, 5, "T W E N T Y"}
28+
half = entry{2, 7, "H A L F"}
29+
30+
one = entry{2, 11, "O N E"}
31+
two = entry{34, 13, "T W O"}
32+
three = entry{26, 11, "T H R E E"}
33+
four = entry{2, 13, "F O U R"}
34+
fiveSecond = entry{18, 13, "F I V E"}
35+
six = entry{14, 11, "S I X"}
36+
seven = entry{2, 17, "S E V E N"}
37+
eight = entry{2, 15, "E I G H T"}
38+
nine = entry{30, 9, "N I N E"}
39+
tenSecond = entry{2, 19, "T E N"}
40+
eleven = entry{22, 15, "E L E V E N"}
41+
twelve = entry{22, 17, "T W E L V E"}
42+
43+
dotOne = entry{0, 0, "●"}
44+
dotTwo = entry{44, 0, "●"}
45+
dotThree = entry{44, 20, "●"}
46+
dotFour = entry{0, 20, "●"}
47+
48+
all = map[entry]struct{}{
49+
it: {},
50+
is: {},
51+
a: {},
52+
to: {},
53+
quarter: {},
54+
half: {},
55+
oclock: {},
56+
past: {},
57+
one: {},
58+
two: {},
59+
three: {},
60+
four: {},
61+
fiveOff: {},
62+
fiveSecond: {},
63+
six: {},
64+
seven: {},
65+
eight: {},
66+
nine: {},
67+
tenOff: {},
68+
tenSecond: {},
69+
eleven: {},
70+
twelve: {},
71+
twenty: {},
72+
73+
dotOne: {},
74+
dotTwo: {},
75+
dotThree: {},
76+
dotFour: {},
77+
78+
{10, 1, "L"}: {},
79+
{22, 1, "A S A M P M"}: {},
80+
{6, 3, "C"}: {},
81+
{38, 3, "D C"}: {},
82+
{42, 5, "X"}: {},
83+
{18, 7, "S"}: {},
84+
{34, 7, "F"}: {},
85+
{18, 9, "E R U"}: {},
86+
{14, 19, "S E"}: {},
87+
}
88+
89+
hours = []entry{
90+
twelve,
91+
one,
92+
two,
93+
three,
94+
four,
95+
fiveSecond,
96+
six,
97+
seven,
98+
eight,
99+
nine,
100+
tenSecond,
101+
eleven,
102+
}
103+
)

qlock/rules.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright Josh Komoroske. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package qlock
6+
7+
func entries(hour int, minute int) map[entry]struct{} {
8+
items := []entry{
9+
it, is,
10+
}
11+
12+
switch minute - minute%5 {
13+
case 55:
14+
items = append(items, fiveOff, to)
15+
case 50:
16+
items = append(items, tenOff, to)
17+
case 45:
18+
items = append(items, a, quarter, to)
19+
case 40:
20+
items = append(items, twenty, to)
21+
case 35:
22+
items = append(items, twenty, fiveOff, to)
23+
case 30:
24+
items = append(items, half, past)
25+
case 25:
26+
items = append(items, twenty, fiveOff, past)
27+
case 20:
28+
items = append(items, twenty, past)
29+
case 15:
30+
items = append(items, a, quarter, past)
31+
case 10:
32+
items = append(items, tenOff, past)
33+
case 5:
34+
items = append(items, fiveOff, past)
35+
case 0:
36+
items = append(items, oclock)
37+
}
38+
39+
switch minute % 5 {
40+
case 1:
41+
items = append(items, dotOne)
42+
case 2:
43+
items = append(items, dotOne, dotTwo)
44+
case 3:
45+
items = append(items, dotOne, dotTwo, dotThree)
46+
case 4:
47+
items = append(items, dotOne, dotTwo, dotThree, dotFour)
48+
}
49+
50+
if minute >= 35 {
51+
items = append(items, hours[(hour+1)%12])
52+
} else {
53+
items = append(items, hours[hour%12])
54+
}
55+
56+
set := make(map[entry]struct{}, len(items))
57+
for _, item := range items {
58+
set[item] = struct{}{}
59+
}
60+
return set
61+
}

qlock/widget.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright Josh Komoroske. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package qlock
6+
7+
import (
8+
"time"
9+
10+
"github.com/JoelOtter/termloop"
11+
)
12+
13+
type clock struct {
14+
colorOn termloop.Attr
15+
colorOff termloop.Attr
16+
}
17+
18+
func New(colorOn, colorOff termloop.Attr) termloop.Drawable {
19+
return &clock{
20+
colorOn: colorOn,
21+
colorOff: colorOff,
22+
}
23+
}
24+
25+
func (*clock) Tick(termloop.Event) {}
26+
27+
func (c *clock) Draw(s *termloop.Screen) {
28+
// Get the current hour and minute.
29+
now := time.Now()
30+
hour, minute := now.Hour(), now.Minute()
31+
32+
// Get the current screen size.
33+
screenWidth, screenHeight := s.Size()
34+
35+
// Get the X "origin" for drawing all components relative to this offset.
36+
originX := (screenWidth - width) / 2
37+
if originX < 0 {
38+
originX = 0
39+
}
40+
41+
// Get the Y "origin" for drawing all components relative to this offset.
42+
originY := (screenHeight - height) / 2
43+
if originY < 0 {
44+
originY = 0
45+
}
46+
47+
// Get all the components that should be enabled.
48+
enabled := entries(hour, minute)
49+
50+
// Loop over the list of all components...
51+
for entry := range all {
52+
if _, found := enabled[entry]; found {
53+
// If the component was one of the enabled ones, draw it with the
54+
// "on" color.
55+
t := termloop.NewText(originX+entry.x, originY+entry.y, entry.text, c.colorOn, termloop.ColorDefault)
56+
t.Draw(s)
57+
} else {
58+
// If the component was not one of the enabled ones, draw it with
59+
// the "off" color.
60+
t := termloop.NewText(originX+entry.x, originY+entry.y, entry.text, c.colorOff, termloop.ColorDefault)
61+
t.Draw(s)
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)