-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsimple.go
196 lines (165 loc) · 3.98 KB
/
simple.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"unicode"
"github.com/Nerdmaster/terminal"
)
const CSI = "\x1b["
const ClearScreen = CSI + "2J" + CSI + ";H"
var done bool
var noise [][]rune
var nextNoise [][]rune
var userInput string
var t *terminal.Reader
var validRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*")
func onKeypress(e *terminal.KeyEvent) {
// Ignoring a key entirely: how can I ki amias without the key?
if e.Key == 'l' {
e.IgnoreDefaultHandlers = true
}
// Tab-complete example: matches foobar and autocompletes it if:
// - At least two characters have been entered
// - All entered characters are prefix of "foobar"
// - Cursor is at the end of the line
// - Current line hasn't already printed out "foobar"
if e.Key == 0x09 {
var runes = []rune("foobar")
if len(e.Line.Text) < 2 || len(e.Line.Text) >= len(runes) || e.Line.Pos != len(e.Line.Text) {
return
}
for i, r := range e.Line.Text {
if r != runes[i] {
return
}
}
e.Line.Text = runes
e.Line.Pos = len(runes)
e.IgnoreDefaultHandlers = true
return
}
// Modifications to entire line based on use of Page Up key
if e.Key == terminal.KeyPgUp && e.Modifier == terminal.ModNone {
for i, r := range e.Line.Text {
e.Line.Text[i] = unicode.SimpleFold(r)
}
e.IgnoreDefaultHandlers = true
}
// Replacing a key
if e.Key == terminal.KeyPgUp && e.Modifier == terminal.ModAlt {
e.Key = 'ģ'
}
// Ignoring a key while still implementing some kind of behavior
if e.Key == terminal.KeyLeft && e.Modifier == terminal.ModNone {
fmt.Print(ClearScreen)
e.IgnoreDefaultHandlers = true
}
}
func printAt(x, y int, output string) {
fmt.Fprintf(os.Stdout, "%s%d;%dH%s", CSI, y, x, output)
}
func randomRune() rune {
return validRunes[rand.Intn(len(validRunes))]
}
func initializeScreen() {
// Clear everything
fmt.Fprintf(os.Stdout, ClearScreen)
// Print initial runes
for y := 0; y < 10; y++ {
printAt(1, y+1, string(noise[y]))
}
// Print prompt
printAt(13, 4, "> ")
}
func setupNoise() {
noise = make([][]rune, 10)
for y := range noise {
noise[y] = make([]rune, 100)
for x := range noise[y] {
if y == 3 && x > 10 && x < 90 {
noise[y][x] = ' '
} else {
noise[y][x] = randomRune()
}
}
}
nextNoise = make([][]rune, 10)
for y := range nextNoise {
nextNoise[y] = make([]rune, 100)
for x := range nextNoise[y] {
nextNoise[y][x] = noise[y][x]
}
}
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
setupNoise()
initializeScreen()
t = terminal.NewReader(os.Stdin)
t.MaxLineLength = 70
t.OnKeypress = onKeypress
go readInput()
go printOutput()
for done == false {
x := rand.Intn(100)
y := rand.Intn(10)
nextNoise[y][x] = randomRune()
time.Sleep(time.Millisecond * 10)
}
}
func readInput() {
for {
command, err := t.ReadLine()
if command == "quit" {
done = true
return
}
if err != nil {
done = true
return
}
}
}
func printOutput() {
lastLine := ""
lastPos := 0
for {
// Print any changes to noise since last tick
for y := 0; y < 10; y++ {
for x := 0; x < 100; x++ {
if y == 3 && x > 10 && x < 90 {
nextNoise[y][x] = noise[y][x]
}
if noise[y][x] != nextNoise[y][x] {
printAt(x+1, y+1, string(nextNoise[y][x]))
noise[y][x] = nextNoise[y][x]
}
}
}
// Print any changes to user input since last tick
newLine, newPos := t.LinePos()
if lastLine != newLine {
toPrint := newLine
if len(lastLine) > len(newLine) {
toPrint += strings.Repeat(" ", len(lastLine)-len(newLine))
}
printAt(15, 4, toPrint)
}
if lastLine != newLine || lastPos != newPos {
printAt(10, 20, strings.Repeat(" ", 100))
printAt(1, 20, fmt.Sprintf("Current line: '%s'; position: %d", newLine, newPos))
lastLine = newLine
lastPos = newPos
}
printAt(15+newPos, 4, "")
time.Sleep(time.Millisecond * 50)
}
}