-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (145 loc) · 3.52 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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/gdamore/tcell/v2"
"golang.org/x/term"
)
var (
abc string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
misclicks = 0
lent = 52
)
func init() {
if len(os.Args) != 1 {
abc = os.Args[1]
lent = len(abc)
} else {
fmt.Printf("Usage: %s [set of characters]\nPress any key to continue with default set, \"q\" to quit.\n", os.Args[0])
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("Error setting terminal to raw mode:", err)
os.Exit(1)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
buf := make([]byte, 1)
_, err = os.Stdin.Read(buf)
if err != nil {
fmt.Printf("Error reading from standard input: %s", err)
os.Exit(1)
}
if buf[0] == 'q' {
os.Exit(1)
}
}
}
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()
w, h := s.Size()
if w < 80 || h < 20 {
s.Fini()
fmt.Println("minimum window size is 80x20")
os.Exit(1)
}
result, marks := print(s)
s.Sync()
start := time.Now()
go func() {
for {
timestring := fmt.Sprintf("%s", time.Since(start).Abs().Round(time.Second))
length := len(timestring)
for i := w - length; i < w; i++ {
s.SetContent(i, h-1, rune(timestring[i-w+length]), nil, tcell.StyleDefault.Reverse(true))
}
time.Sleep(1 * time.Second)
s.Sync()
}
}()
for {
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
// s.Fini()
// fmt.Println("Resizing is unsupported")
// os.Exit(1)
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape || ev.Rune() == 'q' || ev.Key() == tcell.KeyCtrlC {
s.Fini()
os.Exit(0)
}
case *tcell.EventMouse:
w, h := s.Size()
x, y := ev.Position()
switch ev.Buttons() {
case tcell.ButtonPrimary:
if abc[0] == byte(result[x][y]) {
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))
marks[i][j] = true
}
}
}
_, abc = abc[0], abc[1:]
} else {
blink(s, result[x][y], x, y, marks[x][y])
}
}
}
if len(abc) > 0 {
nextchar := fmt.Sprintf("next char %c", abc[0])
for i := 0; i < len(nextchar); i++ {
s.SetContent(i, 0, rune(nextchar[i]), nil, tcell.StyleDefault.Reverse(true))
}
} else {
break
}
}
s.Fini()
fmt.Printf("You win! %s. %dx%d matrix. %d characters long set. %d misclicks\n", time.Since(start), w, h, lent, misclicks)
}
func blink(s tcell.Screen, letter rune, x, y int, mark bool) {
misclicks++
for i := 0; i < 5; i++ {
s.SetContent(x, y, letter, nil, tcell.StyleDefault.Background(tcell.ColorRed))
s.Sync()
time.Sleep(50 * time.Millisecond)
if mark {
s.SetContent(x, y, letter, nil, tcell.StyleDefault.Background(tcell.ColorGreen))
} else {
s.SetContent(x, y, letter, nil, tcell.StyleDefault)
}
s.Sync()
}
}
func print(s tcell.Screen) ([][]rune, [][]bool) {
w, h := s.Size()
result := make([][]rune, w)
marks := make([][]bool, w)
for i := range result {
result[i] = make([]rune, h)
}
for i := range marks {
marks[i] = make([]bool, 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, marks
}