This repository was archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (149 loc) · 3.22 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"time"
"github.com/arifamir/goscreen"
)
func main() {
// for keeping things easy to read and type-safe
type placeholder [5]string
// put the digits (placeholders) into variables
// using the placeholder array type above
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
// This array's type is "like": [10][5]string
//
// However:
// + "placeholder" is not equal to [5]string in type-wise.
// + Because: "placeholder" is a defined type, which is different
// from [5]string type.
// + [5]string is an unnamed type.
// + placeholder is a named type.
// + The underlying type of [5]string and placeholder is the same:
// [5]string
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// For Go Playground, do not use this.
goscreen.Clear()
// Go Playground will not run an infinite loop.
// Loop for example 1000 times instead, like this:
// for i := 0; i < 1000; i++ { ... }
for {
// For Go Playground, use this instead:
// fmt.Print("\f")
goscreen.MoveTopLeft()
// get the current hour, minute and second
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
// extract the digits: 17 becomes, 1 and 7 respectively
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
// Explanation: clock[0]
// + Each element of clock has the same length.
// + So: Getting the length of only one element is OK.
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
//
// The range clause below is ~equal to the following code:
// line := 0; line < len(clock[0]); line++
for line := range clock[0] {
// Print a line for each placeholder in clock
for index, digit := range clock {
// Colon blink on every two seconds.
// + On each sec divisible by two, prints an empty line
// + Otherwise: prints the current pixel
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
// Print the next line and,
// give it enough space for the next placeholder
fmt.Print(next, " ")
}
// After each line of a placeholder, print a newline
fmt.Println()
}
// pause for 1 second
time.Sleep(time.Second)
}
}