-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.go
More file actions
143 lines (126 loc) · 3.68 KB
/
Copy pathclock.go
File metadata and controls
143 lines (126 loc) · 3.68 KB
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"time"
)
// commandline args
var color string
var char string
var redraw time.Duration
var version bool
var offset struct{ x, y uint }
var zoom uint
var background string
// data
var font map[rune][][]bool
var colors map[string]string
func init() {
flag.StringVar(&color, "color", "none", "set the digits' color")
flag.StringVar(&background, "back", "none", "set background color")
flag.StringVar(&char, "char", "▀ ", "set the character(s) to use for drawing")
flag.DurationVar(&redraw, "redraw", 15*time.Second, "set time to wait between redraws")
flag.BoolVar(&version, "version", false, "print version information and exit")
flag.UintVar(&offset.x, "x", 2, "x offset (from left)")
flag.UintVar(&offset.y, "y", 1, "y offset (from top)")
flag.UintVar(&zoom, "zoom", 1, "display digits X times bigger")
font = map[rune][][]bool{
'0': {{true, true, true}, {true, false, true}, {true, false, true}, {true, false, true}, {true, true, true}},
'1': {{false, false, true}, {false, false, true}, {false, false, true}, {false, false, true}, {false, false, true}},
'2': {{true, true, true}, {false, false, true}, {true, true, true}, {true, false, false}, {true, true, true}},
'3': {{true, true, true}, {false, false, true}, {true, true, true}, {false, false, true}, {true, true, true}},
'4': {{true, false, true}, {true, false, true}, {true, true, true}, {false, false, true}, {false, false, true}},
'5': {{true, true, true}, {true, false, false}, {true, true, true}, {false, false, true}, {true, true, true}},
'6': {{true, true, true}, {true, false, false}, {true, true, true}, {true, false, true}, {true, true, true}},
'7': {{true, true, true}, {false, false, true}, {false, false, true}, {false, false, true}, {false, false, true}},
'8': {{true, true, true}, {true, false, true}, {true, true, true}, {true, false, true}, {true, true, true}},
'9': {{true, true, true}, {true, false, true}, {true, true, true}, {false, false, true}, {true, true, true}},
':': {{false}, {true}, {false}, {true}, {false}},
}
colors = map[string]string{
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"white": "37",
"none": "0",
}
}
func clear() {
fmt.Printf("\033[2J")
}
func hideCursor() {
fmt.Printf("\033[?25l")
}
func showCursor() {
fmt.Printf("\033[?25h")
}
func setAt(x, y int, char rune) {
fmt.Printf("\033[%d;%dH%c", y+1, x+1, char)
}
func setColor(color string) {
fmt.Printf("\033[%sm", colors[color])
}
func setColorBack(color string) {
if color == "none" {
return
}
fmt.Printf("\033[%sm", strings.Replace(colors[color], "3", "4", 1))
}
func drawNumberAt(x, y int, num rune) {
for l, line := range font[num] {
i := 0
for c, col := range line {
if col {
for z := 0; z < int(zoom)*2; z++ {
for zz := 0; zz < int(zoom); zz++ {
ch := []rune(char)[i%len([]rune(char))]
setAt(x+c*2*int(zoom)+z, y+l*int(zoom)+zz, ch)
}
i++
}
} else {
i += int(zoom) * 2
}
}
}
}
func drawString(x, y int, str string) {
for _, char := range str {
drawNumberAt(x, y, char)
x += (len(font[char][len(font[char])-1]) + 1) * int(zoom) * 2
}
}
func drawTime(x, y int) {
drawString(x, y, time.Now().Format("15:04"))
}
func main() {
flag.Parse()
if version {
fmt.Println("clock.go v1.0")
return
}
hideCursor()
defer showCursor()
setColor(color)
defer setColor("none")
setColorBack(background)
csig := make(chan os.Signal, 1)
signal.Notify(csig, os.Interrupt)
go func() {
for {
clear()
drawTime(int(offset.x), int(offset.y))
time.Sleep(redraw)
}
}()
<-csig
fmt.Print("\033[2D\033[K")
fmt.Println()
}