-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (85 loc) · 2.51 KB
/
util.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
package main
import (
"errors"
"golang.org/x/sys/unix"
"io/ioutil"
"os"
"strings"
)
func Ctrl(b byte) byte {
return b & 0x1f
}
func Min(x, y uint) uint {
if x < y {
return x
} else {
return y
}
}
func isControlChar(x byte) bool {
return x <= 31 || x == 127
}
func RevertTerminalMode(original *unix.Termios) error {
return unix.IoctlSetTermios(int(os.Stdin.Fd()), ioctlWriteTermios, original)
}
func EnableRawMode() (unix.Termios, error) {
fd := int(os.Stdin.Fd())
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
returnTermios := *termios
if err != nil {
return returnTermios, err
}
termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.IXON
termios.Iflag &^= unix.IXON // Ctrl-S and Ctrl-Q
termios.Iflag &^= unix.ICRNL // Ctrl-M
termios.Oflag &^= unix.OPOST // #Output Processing
termios.Lflag &^= unix.ECHO | unix.ECHONL // Echo
termios.Lflag &^= unix.ICANON // Canonical Mode
termios.Lflag &^= unix.ISIG // Ctrl-C and Ctrl-Z
termios.Lflag &^= unix.IEXTEN // ctrl-V, ctrl-O (on macOS
termios.Cflag &^= unix.CSIZE | unix.PARENB
termios.Cflag |= unix.CS8
termios.Cc[unix.VMIN] = 0 // Number of bytes to let Read() return
termios.Cc[unix.VTIME] = 1 // Maximum wait time for Read(). Measured in tenths of a second
if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil {
return returnTermios, err
}
return returnTermios, err
}
func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !errors.Is(err, os.ErrNotExist)
}
// GetWindowSize returns number of rows, then columns. (0, 0) if error occurs
// TODO: Sometimes unix.IoctlGetWinsize will fail. Implement fallback
//
// https://viewsourcecode.org/snaptoken/kilo/03.rawInputAndOutput.html#window-size-the-hard-way
func GetWindowSize() (uint, uint) {
ws, err := unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ)
if err != nil {
return 0, 0
}
return uint(ws.Row), uint(ws.Col)
}
func Touch(filename string) error {
return ioutil.WriteFile(filename, []byte{}, 0666)
}
func OpenOrCreate(filename string) ([]Row, error) {
if !fileExists(filename) {
err := Touch(filename)
if err != nil {
return []Row{}, err
}
}
raw, err := ioutil.ReadFile(filename)
if err != nil {
return []Row{}, err
}
file := strings.ReplaceAll(string(raw), "\r", "\n")
rawRows := strings.Split(file, "\n")
rows := make([]Row, len(rawRows))
for i, s := range rawRows {
rows[i] = ConstructRow(s)
}
return rows, nil
}