This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilbiline.go
217 lines (182 loc) · 4.5 KB
/
hilbiline.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
// Hilbiline is a readline-inspired line editor made in pure Go
package hilbiline
import (
"bufio"
"fmt"
"io"
"os"
"unicode/utf8"
_ "github.com/mattn/go-runewidth" // we'll need later
"golang.org/x/term"
)
const (
// Keycodes
KeyNull = 0
KeyCtrlA = 1
KeyCtrlB = 2
KeyCtrlC = 3
KeyCtrlD = 4
KeyCtrlE = 5
KeyCtrlF = 6
KeyCtrlH = 8
KeyTab = 9
KeyCtrlJ = 10
KeyCtrlK = 11
KeyCtrlL = 12
KeyEnter = 13
KeyCtrlN = 14
KeyCtrlP = 16
KeyCtrlT = 20
KeyCtrlU = 21
KeyCtrlW = 23
KeyEsc = 27
KeyBackspace = 127
)
var (
mlmode = false
)
type HilbilineState struct {
// io readers
stdio *bufio.Reader
stdout *bufio.Reader
// Readline buffer and prompt
buf []rune
prompt string
historyindex int
pos int // Current cursor position
oldpos int // Previous cursor position
width int // Number of terminal rows
height int // Num of terminal columns
histState histState
}
func New(prompt string) HilbilineState {
termwidth, termheight, _ := term.GetSize(int(os.Stdout.Fd()))
return HilbilineState{
stdio: bufio.NewReader(os.Stdin),
stdout: bufio.NewReader(os.Stdout),
buf: []rune{},
prompt: prompt,
width: termwidth,
height: termheight,
// By default, does not have a file to write to.
// AddHistFile must be used for persistent history
histState: newHistState(),
}
}
func (h *HilbilineState) Read() (string, error) {
fmt.Print(h.prompt)
oldState, _ := h.refreshLine()
defer term.Restore(int(os.Stdin.Fd()), oldState)
h.buf = []rune{}
h.pos = 0
for {
// TODO: custom read so we can handle esc properly
char, _, err := h.stdio.ReadRune()
if err != nil {
return "", err
}
switch char {
case KeyCtrlD:
// End session on CtrlD
h.destroy()
return "", io.EOF
case KeyCtrlC:
fmt.Printf("\r\n")
return "", nil
// Vertical feed
case KeyCtrlJ:
return string(h.buf), nil
case KeyCtrlL:
h.ClearScreen()
case KeyCtrlU:
// Delete whole line
h.buf = []rune{}
h.pos = 0
h.refreshLine()
// case KeyCtrlK: remove reset of word
// case KeyCtrlF: move forward one character
// case KeyCtrlB: move back one character
// case KeyCtrlT: swap buf[-1] and buf[-2]
// case KeyCtrlH: delete last rune
// case KeyCtrlN: go forward in history
// case KeyCtrlP: go back in history
// case KeyCtrlW: delete to previous word
// case KeyEsc: handle esc codes (cursor up etc)
case KeyEnter:
fmt.Print("\n\r")
h.histState.histBuf.addEntry(string(h.buf))
return string(h.buf), nil
case KeyBackspace:
h.editBackspace()
case KeyTab:
// TODO: tab completion
// Just making it a no-op so it wont print the tab
default:
h.editInsert(char)
}
}
}
func (h *HilbilineState) LoadHistory(path string) error {
// Open file with R/W perms or create it,
// perms are RW for user only
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
h.histState.file = file
return nil
}
func (h *HilbilineState) PrintPrompt() {
fmt.Print(h.prompt)
}
func (h *HilbilineState) SetPrompt(prompt string) {
h.prompt = prompt
}
func (h *HilbilineState) ClearScreen() {
fmt.Print("\x1b[H\x1b[2J")
h.PrintPrompt()
}
func (h *HilbilineState) editInsert(c rune) {
h.pos++
h.buf = append(h.buf, c)
// goes to next line if we're at the end of the term
// but why do we do this manually? handle backspace on next line better,
// it gets messed up if we let text go right to the end
if (h.width - len(h.prompt)) - h.pos == 0 {
fmt.Print("\r\n")
}
fmt.Print(string(c))
}
func (h *HilbilineState) editBackspace() {
if h.pos > 0 {
_, length := utf8.DecodeLastRuneInString(string(h.buf))
h.buf = append(h.buf[:h.pos-1], h.buf[h.pos:]...)
h.pos--
// This is atrocious
// For testing, ん has length 3, English chars have 1
// Without this if check ん will loop 4 times instead of 3
if length == 1 {
fmt.Print("\b")
} else {
for i := 1; i < length; i++ {
// Backspace code
fmt.Print("\b")
}
}
// Clear to end
fmt.Printf("\033[K")
if (h.width - len(h.prompt)) - h.pos == 1 {
fmt.Printf("\033[1A\033[%dC", h.width + len(h.prompt)) // cursor up
}
}
}
func (h *HilbilineState) refreshLine() (*term.State, error) {
// TODO: dont do this here; make a separate function
// refreshLine should do as the name suggests, only refresh
// the line
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
return oldState, err
}
func (h *HilbilineState) destroy() {
h.histState.histBuf.writeToFile(h.histState.file)
}