-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (148 loc) · 3.36 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
package main
import (
"fmt"
"io"
"log"
"os"
"golang.org/x/sys/unix"
)
/*** data ***/
type editorConfig struct {
screenRows int
screenCols int
origTermios *unix.Termios
}
var E editorConfig
/*** terminal ***/
func die(err error) {
io.WriteString(os.Stdout, "\x1b[2J")
io.WriteString(os.Stdout, "\x1b[H")
disableRawMode()
log.Fatal(err)
}
func TcSetAttr(fd int, termios *unix.Termios) error {
err := unix.IoctlSetTermios(fd, unix.TIOCSETA+1, termios)
if err != nil {
return err
}
return nil
}
func TcGetAttr(fd int) (*unix.Termios, error) {
termios, err := unix.IoctlGetTermios(fd, unix.TIOCGETA)
if err != nil {
log.Fatalf("Failed to get terminal attributes: %s\n", err)
return nil, err
}
return termios, nil
}
func getCursorPosition(rows *int, cols *int) int {
io.WriteString(os.Stdout, "\x1b[6n")
fmt.Printf("\r\n")
var buffer [1]byte
var buf []byte
var cc int
for cc, _ = os.Stdin.Read(buffer[:]); cc == 1; cc, _ = os.Stdin.Read(buffer[:]) {
if buffer[0] == 'R' {
break
}
buf = append(buf, buffer[0])
}
if string(buf[0:2]) != "\x1b[" {
log.Printf("failed to read rows or cols from tty\n")
return -1
}
if n, e := fmt.Sscanf(string(buf[2:]), "%d;%d", rows, cols); n != 2 || e != nil {
if e != nil {
log.Printf("get cursor position: fmt.Sscanf() failed: %s\n", e)
}
if n != 2 {
log.Printf("get cursor porition: got %d items, wanted 2\n", n)
}
return -1
}
return 0
}
func getWindowSize(rows *int, cols *int) int {
winSize, err := unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ)
if true {
io.WriteString(os.Stdout, "\x1b[999C\x1b[999B")
return getCursorPosition(rows, cols)
}
if err == nil {
*rows = int(winSize.Row)
*cols = int(winSize.Col)
return 0
}
return -1
}
func enableRawMode() {
STDIN := int(os.Stdin.Fd())
E.origTermios, _ = TcGetAttr(STDIN)
raw := *E.origTermios
raw.Iflag &^= unix.BRKINT | unix.ICRNL | unix.INPCK | unix.ISTRIP | unix.IXON
raw.Oflag &^= unix.OPOST
raw.Cflag |= unix.CS8
raw.Lflag &^= unix.ECHO | unix.ICANON | unix.IEXTEN | unix.ISIG
raw.Cc[unix.VMIN+1] = 0
raw.Cc[unix.VTIME+1] = 1
err := TcSetAttr(STDIN, &raw)
if err != nil {
log.Fatalf("Failed to enable raw mode: %s\n", err)
}
}
func disableRawMode() {
err := TcSetAttr(int(os.Stdin.Fd()), E.origTermios)
if err != nil {
log.Fatalf("Failed to disable raw mode: %s\n", err)
}
}
func editorReadKey() byte {
var buffer [1]byte
var cc int
var err error
for cc, err = os.Stdin.Read(buffer[:]); cc != 1; cc, err = os.Stdin.Read(buffer[:]) {
}
if err != nil {
die(err)
}
return buffer[0]
}
/*** input ***/
func editorProcessKeypress() {
c := editorReadKey()
switch c {
case ('q' & 0x1f): // this means we need to use CTRL + Q to quit
io.WriteString(os.Stdout, "\x1b[2J")
io.WriteString(os.Stdout, "\x1b[H")
disableRawMode()
os.Exit(0)
}
}
/*** output ***/
func editorRefreshScreen() {
io.WriteString(os.Stdout, "\x1b[2J")
io.WriteString(os.Stdout, "\x1b[H")
editorDrawRows()
io.WriteString(os.Stdout, "\x1b[H")
}
func editorDrawRows() {
for i := 0; i < E.screenRows; i++ {
io.WriteString(os.Stdout, "~\r\n")
}
}
/*** init ***/
func initEditor() {
err := getWindowSize(&E.screenRows, &E.screenCols)
if err == -1 {
die(fmt.Errorf("couldnt get screen size"))
}
}
func main() {
enableRawMode()
defer disableRawMode()
initEditor()
for {
editorRefreshScreen()
editorProcessKeypress()
}
}