-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
427 lines (366 loc) · 10.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"golang.org/x/sys/unix"
)
type key int
const (
stdInFileNo = 0
stdoutFileNo = 1
goditorVersion = 0.1
quit key = 17
escapeSeq key = '\x1b'
escapeFollowed key = '['
arrowUp key = iota + 1000
arrowDown
arrowRight
arrowLeft
pageUp
pageDown
deleteKey
)
// cursorMap maps some of the control character
var cursorMap = map[byte]key{
'A': arrowUp,
'B': arrowDown,
'C': arrowRight,
'D': arrowLeft,
}
// storeGoditorRow for storing a row of text in our editor
type storeGoditorRow struct {
text bytes.Buffer
}
// goditorStateT is to keep track of the cursor’s x and y position
// and Winsize
type goditorStateT struct {
curRow uint16
curCol uint16
winsizeStruct *unix.Winsize
row []storeGoditorRow // slice of storeGoditorRow to store all row.
numrows uint16
rowat uint16 // row of the file the user is currently scrolled to.
}
// global state.
var goditorState goditorStateT
// enableRawMode is used to put the terminal into raw mode.
// There are number of attributes of the terminal, but in its defaultt state
// called "cooked mode" input is line buffered, characters are automatically
// echoed, ^C - raise SIGINT, ^D signals EOF, and so on.
// In raw mode none of thos things happens; you get every keystroke
// immediately without waiting for a new line, and it's not echoed.
// ^C doesn't cause SIGINT, and so on.
func enableRawMode() (*unix.Termios, error) {
cooked, err := unix.IoctlGetTermios(stdInFileNo, unix.TCGETS)
if err != nil {
return nil, err
}
state := *cooked
// IXON (enable start/stop output control)
// Disable control characters that Ctrl-S and Ctrl-Q produce
// ICRNL - Input is gathered into lines, terminated by one of the line-delimiter characters:
// NL, EOL, EOL2 (if the IEXTEN flag is set), EOF (at anything other than the initial
// position in the line), or CR (if the ICRNL flag is enabled).
// Default setting ^M
// with the ICRNL (map CR to NL on input) flag set
// (the default), this character is first converted to a newline (ASCII 10 decimal, ^J)
// before being passed to the reading proces
// BRKINT - Disable Signal interrupt (SIGINT) on BREAK condition
state.Iflag &^= (unix.IXON | unix.ICRNL | unix.BRKINT)
// OPOST - to disables output postprocessing "\n" to "\r\n" and
state.Oflag &^= (unix.OPOST)
// The ECHO feature causes each key you type to be printed to the terminal,
// so you can see what you’re typing
// ICANON flag to turn off canonical mode, so that input can be read, byte
// by byte instead of line-by-line.
// ln line-by-line mode you have to press Enter for program to read input.
// ISIG - To disable signal-generating characters (INTR, quit, SUSP)
// Eg disable Ctrl-C(SIGINT) and Ctrl-Z(SIGTSTP) signals
// IEXTERN - Disable Ctrl-V
state.Lflag &^= (unix.ECHO | unix.ICANON | unix.ISIG | unix.IEXTEN)
// set timeout read
// VMIN - the minimum number of bytes of input needed before read,
// Character-at-a-time input
state.Cc[unix.VMIN] = 1
// VTIME -the maximum amount of time to wait before read returns,
// It is in tenths of a second, so we set it to 1/10 of a second,
// or 100 milliseconds
// times out, it will return 0
state.Cc[unix.VTIME] = 1
//TCSANOW is TCSETS
err = unix.IoctlSetTermios(stdInFileNo, unix.TCSETS, &state)
if err != nil {
return nil, err
}
return cooked, nil
}
// Disable raw mode at exit
func disableRawMode(cookedState *unix.Termios) error {
err := unix.IoctlSetTermios(stdInFileNo, unix.TCSETS, cookedState)
if err != nil {
return err
}
return nil
}
// goditorMoveCursor move the cursor around
func goditorMoveCursor() {
// CUP – Cursor Position
// https://vt100.net/docs/vt100-ug/chapter3.html#CUP
writeToTerminal(fmt.Sprintf("\x1b[%d;%dH", goditorState.curRow, goditorState.curCol))
}
// to wait for one keypress, and return it
func goditorReadKey(reader io.ByteReader) (key, error) {
char, err := reader.ReadByte()
if err != nil {
return key(char), err
}
// check if the char type that we have got is escapeSeq
switch key(char) {
case escapeSeq:
// wait for two other input.
input1, _ := reader.ReadByte()
if key(input1) == escapeFollowed {
input2, _ := reader.ReadByte()
// check if arrowLeft or arrowRight or
// arrowUp or arrowDown key has been pressed.
if val, ok := cursorMap[input2]; ok {
return val, nil
}
// checkIf the PageUp or PageDown key has been
// pressed
// PageUp <esc>[5~
// PageDown <esc>[6~
if input2 >= 0 && input2 <= 9 {
input3, _ := reader.ReadByte()
switch input3 {
case 3:
return deleteKey, nil
case 5:
return pageUp, nil
case 6:
return pageDown, nil
}
}
return key(input2), nil
}
}
return key(char), nil
}
// goditorActionKeypress waits for a keypress, and then handles it
func goditorActionKeypress(reader io.ByteReader) (int, error) {
char, err := goditorReadKey(reader)
if err != nil {
return 0, err
}
// mapping Ctrl + Q(17) to quit is
switch char {
case quit:
return 1, nil
case arrowUp:
// Prevent moving the cursor values to go into the negatives
if goditorState.curRow != 1 {
goditorState.curRow--
}
goditorMoveCursor()
case arrowDown:
if goditorState.curRow != goditorState.winsizeStruct.Row-1 {
goditorState.curRow++
}
goditorMoveCursor()
case arrowLeft:
if goditorState.curCol != 1 {
goditorState.curCol--
}
goditorMoveCursor()
case arrowRight:
if goditorState.curCol != goditorState.winsizeStruct.Col-1 {
goditorState.curCol++
}
goditorMoveCursor()
default:
goditorInsertChar(char)
}
return 0, nil
}
// goditorRowInsertChar inserts a single character into an row
func goditorRowInsertChar(char key) {
writeToTerminal(string(char))
}
func goditorInsertChar(char key) {
goditorRowInsertChar(char)
}
// writeToTerminal writes n bytes to the os.Stdout file
func writeToTerminal(value string) error {
STDOUTFILE := os.Stdout
_, err := fmt.Fprintf(STDOUTFILE, value)
return err
}
// goditorDrawRows() draws a tilde in each rowof the buffer.
// and each row is not part of the file.
func goditorDrawRows() string {
// get the size of the terminal
// to know how many rows to draw
editorConfig := *goditorState.winsizeStruct
// Number of rows
erow := editorConfig.Row
crowat := goditorState.rowat
var yaxis uint16
// buffer is to avoid make a whole bunch of small
// writeToTerminal every time of the loop.
var buffer bytes.Buffer
// ToDo: golang 1.10 has strings.Builder type,
// which achieves this even more efficiently
for yaxis = 0; yaxis < erow; yaxis++ {
editorrow := yaxis + crowat
// printing \r\n will cause the terminal to scroll
// for a new blank line
// so we should not printing the \r\n to last line
// display the name of our editor and a version number too.
if editorrow >= goditorState.numrows {
// if numrows is zero, then only display the Goditor version
// as in othercase we've opened a file
if goditorState.numrows == 0 && yaxis == erow/2 {
// and position it to the center of the screen
message := "Goditor: v0.1"
ecol := editorConfig.Col
leftPad := (int(ecol) - len(message)) / 2
if leftPad > 1 {
buffer.WriteString("~")
leftPad--
}
for {
leftPad--
if leftPad < 1 {
break
}
buffer.WriteString(" ")
}
buffer.WriteString(message)
} else {
buffer.WriteString("~")
}
} else {
text := goditorState.row[editorrow].text.String()
buffer.WriteString(text)
}
// Erase In Line - the part of the line to the right of the cursor
buffer.WriteString("\x1b[K")
if yaxis < erow-1 {
buffer.WriteString("\r\n")
}
}
return buffer.String()
}
// Clear the screen
func goditorRefreshScreen() error {
// write only once
var buffer bytes.Buffer
// terminal is drawning, cursor might be displayed.
// hide the cursor for the flicker moments
// https://vt100.net/docs/vt510-rm/DECTCEM.html
// set Mode
buffer.WriteString("\x1b[?25l")
// CUP – Cursor Position
// https://vt100.net/docs/vt100-ug/chapter3.html#CUP
// position the cursor at the first row and first column,
// not at the bottom.
// get the position from cursorState
buffer.WriteString(fmt.Sprintf("\x1b[%d;%dH", goditorState.curRow, goditorState.curCol))
editor := goditorDrawRows()
buffer.WriteString(editor)
// reposition the cursor back up at the top-left corner.
buffer.WriteString("\x1b[H")
// cursor reset mode
buffer.WriteString("\x1b[?25h")
// Once write to Screen
err := writeToTerminal(buffer.String())
if err != nil {
return err
}
return nil
}
// clearScreenOnExit clear the screen and
// reposition the cursor when program exits
func clearScreenOnExit() {
var buffer bytes.Buffer
buffer.WriteString("\x1b[2J")
buffer.WriteString("\x1b[H")
writeToTerminal(buffer.String())
}
// goditorOpen reads the contents from the file, and display
// the file content.
func goditorOpen(file *os.File) {
// create a tmp slice
rowvalue := make([]storeGoditorRow, 0)
br := bufio.NewReader(file)
// ReadLine does not include the line end ("\r\n" or "\n")
i := 0
for {
line, _, err := br.ReadLine()
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
newRow := storeGoditorRow{}
newRow.text.WriteString(string(line))
rowvalue = append(rowvalue, newRow)
i++
goditorState.numrows++
}
// assign the goditor with the row value
goditorState.row = rowvalue
}
func main() {
// a process can use the ioctl() TIOCGWINSZ operation to
// find out the current size of the terminal window
winsizeS, err := unix.IoctlGetWinsize(stdoutFileNo, unix.TIOCGWINSZ)
if err != nil {
log.Fatal(err)
}
// Initialize the initial Cursor position.
goditorState = goditorStateT{curRow: 1, curCol: 1, winsizeStruct: winsizeS, numrows: 0, rowat: 0}
// if args is the fileName open the file content
args := os.Args
if len(args) == 2 {
// open the file, for reading.
file, err := os.Open("test.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
goditorOpen(file)
}
cookedState, err := enableRawMode()
if err != nil {
clearScreenOnExit()
log.Fatalln(err)
}
reader := bufio.NewReader(os.Stdin)
goditorRefreshScreen()
// Each Iteration, reader reads a byte of data from the
// source and assign it to the charValue, until there are
// no more bytes to read.
for {
read, err := goditorActionKeypress(reader)
if err != nil {
disableRawMode(cookedState)
clearScreenOnExit()
log.Fatalln(err)
}
// if read is 1 that means we need to end the loop.
if read == 1 {
clearScreenOnExit()
err := disableRawMode(cookedState)
if err != nil {
log.Fatalln(err)
}
os.Exit(0)
}
}
}