-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.go
98 lines (86 loc) · 2.11 KB
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"strings"
"time"
"github.com/gdamore/tcell/v2"
)
// Clock keeps current time and 2d array of bits
// used to display binary clock
type Clock struct {
now time.Time
Display [6][4]int
}
func (c Clock) String() string {
s := &strings.Builder{}
fmt.Fprintf(s, "Clock: %s\n", c.now)
for y := 3; y >= 0; y-- {
for x := 0; x < 6; x++ {
fmt.Fprintf(s, " %d ", c.Display[x][y])
}
fmt.Fprintf(s, "\n")
}
return s.String()
}
// Update sets internal clock time to tim.Now() and updates bit array
func (c *Clock) Update() {
c.now = time.Now()
c.updateDisplaySection(0, c.now.Hour())
c.updateDisplaySection(1, c.now.Minute())
c.updateDisplaySection(2, c.now.Second())
}
func (c *Clock) updateDisplaySection(section, number int) {
a, b := splitNum(number)
s := section * 2
c.Display[s] = getBin(a)
c.Display[s+1] = getBin(b)
}
// ClockWidgetConfig contains configuration of clock widget
type ClockWidgetConfig struct {
x, y, padX, padY, sectionPad int
bitOn rune
bitOff rune
}
// ClockWidget is struct used to display Clock on tcell screen
type ClockWidget struct {
*ClockWidgetConfig
*Clock
}
// Draw clock on provided tcell screen with given style
func (c ClockWidget) Draw(s tcell.Screen, style *tcell.Style) {
cx, cy := c.x, c.y
for y := 3; y >= 0; y-- {
for x := 0; x < 6; x++ {
num := c.Display[x][y]
if num == 0 {
s.SetContent(cx, cy, c.bitOff, nil, *style)
} else {
s.SetContent(cx, cy, c.bitOn, nil, *style)
}
cx += c.padX
if x == 1 || x == 3 {
cx += c.sectionPad
}
}
cx = c.x
cy += c.padY
}
}
func (c ClockWidget) size() (int, int) {
return (c.padX * 5) + (c.sectionPad * 2), (c.padY * 4)
}
// CenterPos changes x and y values of ClockWidget to
// keep ClockWidget centered. Call this after every screen resize event
func (c *ClockWidget) CenterPos(s tcell.Screen) {
screenW, screenH := s.Size()
wW, wH := c.size()
c.x = (screenW / 2) - (wW / 2)
c.y = (screenH / 2) - (wH / 2)
}
func newClockWidget(config *ClockWidgetConfig) *ClockWidget {
c := &ClockWidget{
config,
&Clock{},
}
return c
}