-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (92 loc) · 2.05 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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/gdamore/tcell/v2"
)
var abc string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func main() {
s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e := s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
s.EnableMouse()
result := print(s)
s.Sync()
start := time.Now()
for {
go func() {
w, h := s.Size()
elapsed := time.Since(start)
length := len(elapsed.String())
for i := w - length; i < w; i++ {
s.SetContent(i, h-1, rune(elapsed.String()[i-w+length]), nil, tcell.StyleDefault.Reverse(true))
}
s.Sync()
}()
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
result = print(s)
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
s.Fini()
os.Exit(0)
}
if ev.Key() == tcell.KeyRune {
if ev.Rune() == 'r' {
result = print(s)
}
}
case *tcell.EventMouse:
x, y := ev.Position()
switch ev.Buttons() {
case tcell.ButtonPrimary:
if abc[0] == byte(result[x][y]) {
w, h := s.Size()
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
if abc[0] == byte(result[i][j]) {
s.SetContent(i, j, result[i][j], nil, tcell.StyleDefault.Background(tcell.ColorGreen))
}
}
}
_, abc = abc[0], abc[1:]
} else {
blink(s, result[x][y], x, y)
}
}
}
}
}
func blink(s tcell.Screen, letter rune, x, y int) {
for i := 0; i < 5; i++ {
s.SetContent(x, y, letter, nil, tcell.StyleDefault.Background(tcell.ColorRed))
s.Sync()
time.Sleep(50 * time.Millisecond)
s.SetContent(x, y, letter, nil, tcell.StyleDefault)
s.Sync()
}
}
func print(s tcell.Screen) [][]rune {
w, h := s.Size()
result := make([][]rune, w)
for i := range result {
result[i] = make([]rune, h)
}
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
randomNumber := rand.Int() % len(abc)
letter := rune(abc[randomNumber])
result[i][j] = letter
s.SetContent(i, j, letter, nil, tcell.StyleDefault)
}
}
return result
}