-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (123 loc) · 2.88 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"math"
"time"
termbox "github.com/nsf/termbox-go"
)
const centerX = 30
const centerY = 12
const axisX = 20
const axisY = 10
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
key := make(chan string)
go keyEvent(key)
loop:
for {
select {
case k := <-key:
if k == "esc" {
break loop
}
default:
}
drawClock()
}
}
func keyEvent(key chan string) {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
key <- "esc"
case termbox.KeyCtrlC:
key <- "esc"
default:
key <- string(ev.Ch)
}
}
}
}
func drawClock() {
t := time.Now()
termbox.Clear(termbox.ColorWhite, termbox.ColorDefault)
drawCircle(centerX, centerY, axisX-2, axisY-1, 30, ' ', termbox.ColorBlack, termbox.ColorRed) // HourMark
drawHand(t.Minute(), centerX, centerY, axisX, axisY, 360/60, ' ', termbox.ColorWhite, termbox.ColorCyan)
drawHand(t.Hour()*5+int(t.Minute()/12), centerX, centerY, axisX-6, axisY-3, 360/60, ' ', termbox.ColorWhite, termbox.ColorBlue)
drawHand(t.Second(), centerX, centerY, axisX, axisY, 360/60, ' ', termbox.ColorWhite, termbox.ColorGreen)
drawCircle(centerX, centerY, axisX, axisY, 1, ' ', termbox.ColorWhite, termbox.ColorWhite)
termbox.SetCell(centerX, centerY, ' ', termbox.ColorWhite, termbox.ColorWhite)
termbox.Flush()
}
func drawCircle(cx, cy, h, v, s int, ch rune, fg termbox.Attribute, bg termbox.Attribute) {
if s < 0 {
s = 1
}
for i := 0; i <= 90; i += s {
x := float64(h) * math.Cos(float64(i)*3.14/180)
y := float64(v) * math.Sin(float64(i)*3.14/180)
termbox.SetCell(cx-int(x), cy-int(y), ch, fg, bg) // 0 to 90 degrees
termbox.SetCell(cx+int(x), cy-int(y), ch, fg, bg) // 90 to 180 degrees
termbox.SetCell(cx+int(x), cy+int(y), ch, fg, bg) // 180 to 270 degrees
termbox.SetCell(cx-int(x), cy+int(y), ch, fg, bg) // 270 to 360 degrees
}
}
func drawLine(x1, y1, x2, y2 int, ch rune, fg termbox.Attribute, bg termbox.Attribute) {
var dx, dy, sx, sy int
if x2 > x1 {
sx = 1
} else {
sx = -1
}
if x2 > x1 {
dx = x2 - x1
} else {
dx = x1 - x2
}
if y2 > y1 {
sy = 1
} else {
sy = -1
}
if y2 > y1 {
dy = y2 - y1
} else {
dy = y1 - y2
}
x := x1
y := y1
if dx >= dy {
e := -dx
for i := 0; i <= dx; i++ {
termbox.SetCell(x, y, ch, fg, bg)
x += sx
e += 2 * dy
if e >= 0 {
y += sy
e -= 2 * dx
}
}
} else {
e := -dy
for i := 0; i <= dy; i++ {
termbox.SetCell(x, y, ch, fg, bg)
y += sy
e += 2 * dx
if e >= 0 {
x += sx
e -= 2 * dy
}
}
}
}
func drawHand(time, cx, cy, h, v, dg int, ch rune, fg termbox.Attribute, bg termbox.Attribute) {
x := float64(h) * math.Cos(float64(dg*time-90)*3.14/180)
y := float64(v) * math.Sin(float64(dg*time-90)*3.14/180)
drawLine(cx, cy, cx+int(x), cy+int(y), ch, fg, bg)
}