-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
212 lines (186 loc) · 4.65 KB
/
move.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import ("os"
"fmt"
"io"
"errors"
//"flag"
//"time"
"unsafe"
//"strconv"
//"strings"
"syscall"
"io/ioutil"
"os/exec"
//"os/signal"
//"bufio"
)
type winsize struct {
rows uint16
cols uint16
xpixels uint16
ypixels uint16
}
/** what is the size of the terminal
@param pointer to output stream
@return width, height
*/
func get_term_size(fd uintptr) (int, int) {
var sz winsize
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.cols), int(sz.rows)
}
/** ESC Commands */
const (
_ESC_SAVE_SCREEN = "?47h"
_ESC_RESTORE_SCREEN = "?47l"
_ESC_SAVE_CURSOR = "s"
_ESC_RESTORE_CURSOR = "u"
_ESC_BOLD_ON = "1m"
_ESC_BOLD_OFF = "0m"
_ESC_CURSOR_ON = "?25h"
_ESC_CURSOR_OFF = "?25l"
_ESC_CLEAR_SCREEN = "2J"
_ESC_CLEAR_LINE = "2K"
)
/**
Util Function Read a file
Not tested!
@param full path to read
@return empty string on error, file contents otherwise
*/
func readFile(file_path string) string {
content, err := ioutil.ReadFile(file_path)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
return ""
}
return string(content)
}
func processLetter(b byte) {
if b != '\033' {
fmt.Printf("got one letter %s\n", string(b))
}
}
func processLine(line string) {
fmt.Printf("got one line %s\n", line)
}
func processCode(line string) {
fmt.Printf("got one code %s\n", line)
}
func startKeyListener(c chan byte) {
defer close(c)
exec.Command("stty", "-f", "/dev/tty", "cbreak", "min", "1").Run()
exec.Command("stty", "-f", "/dev/tty", "-echo").Run()
defer exec.Command("stty", "-f", "/dev/tty", "echo").Run()
f, err := os.Open("/dev/tty")
if err != nil {
fmt.Printf("open error\n")
os.Stderr.WriteString(err.Error() + "\n")
panic(err)
}
defer f.Close()
line := ""
three := ""
five := ""
b := make([]byte, 1)
for {
_, err := f.Read(b)
if err != nil && !errors.Is(err, io.EOF) {
fmt.Printf("read error %v", err)
break
}
if b[0] == '\n' {
if line == "quit" {
break
}
processLine(line)
line = ""
} else {
c <-b[0]
processLetter(b[0])
line = line + string(b[0])
three = three + string(b[0])
five = five + string(b[0])
if len(three)>3 {
three = three[1:4]
}
if len(five)>5 {
five = five[1:6]
}
switch three {
case "\033[A":
processCode("up")
three = ""
case "\033[B":
processCode("down")
three = ""
case "\033[C":
processCode("right")
three = ""
case "\033[D":
processCode("left")
three = ""
case "\033OP":
processCode("F1")
three = ""
case "\033OQ":
processCode("F2")
three = ""
case "\033OR":
processCode("F3")
three = ""
case "\033OS":
processCode("F4")
three = ""
}
switch five {
case "\033[15~":
processCode("F5")
three = ""
case "\033[17~":
processCode("F6")
three = ""
case "\033[18~":
processCode("F7")
three = ""
case "\033[19~":
processCode("F8")
three = ""
case "\033[20~":
processCode("F9")
three = ""
case "\033[21~":
processCode("F10")
three = ""
case "\033[22~", "\033[23~":
processCode("F11")
three = ""
case "\033[24~":
processCode("F12")
three = ""
}
}
if err != nil {
// end of file
fmt.Printf("end of file\n")
break
}
}
}
func init() {
}
func main() {
ch := make(chan byte)
go startKeyListener(ch)
running := true
for running {
if letter, okay := <-ch ; okay {
if letter == 0x0000 {
fmt.Println (string(letter))
}
} else {
running = false
}
}
}