/
pat.go
348 lines (301 loc) · 7 KB
/
pat.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
// pat.go
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/charlesetc/pat/display"
"github.com/charlesetc/pat/editor"
"github.com/charlesetc/pat/input"
"github.com/charlesetc/pat/stack"
"github.com/nsf/termbox-go"
)
var Log func([]rune)
var files []string // filenames
// var editors []*editor // editors for the filenames
var ed *editor.Editor // the current editor.
var commandHistory *stack.Stack
func onlyHighlights(commands [][]string) bool {
for _, c := range commands {
switch c[0] {
case "", "g", "y", "lines", ",", "x":
default:
return false
}
}
return true
}
// func leadingdigits(str string) int {
// for _, r := range str {
// if
// }
// }
func escapeSpace(str string) string {
str = strings.Replace(str, "\\\\", "&#doubleslash;", -1)
str = strings.Replace(str, "\\n", "\n", -1)
str = strings.Replace(str, "\\t", "\t", -1)
str = strings.Replace(str, "&#doubleslash;", "\\\\", -1)
return str
}
func lineCommand(command string) (bool, []string, int) {
var parsed bool
var output []string
var numberParsed int
switch {
case len(command) == 0:
parsed = false
case strings.ContainsRune(command, ','):
strs := strings.SplitN(command, ",", 2)
_, err1 := strconv.Atoi(strs[0])
n, err2 := strconv.Atoi(strs[1])
if (err1 != nil && strs[0] != "") || (err2 != nil && strs[1] != "$" && strs[1] != "") {
parsed = false
break
}
parsed = true
output = []string{",", strs[0], strs[1]}
numberParsed = len(strs[0]) + 1 + len(strconv.Itoa(n))
if len(strs[1]) == 0 {
numberParsed-- // because "" translates to 0
}
default:
n, err := strconv.Atoi(command)
parsed = err == nil
numberParsed = len(strconv.Itoa(n))
output = []string{"line", command}
}
return parsed, output, numberParsed
}
func parseLine(line string) [][]string {
commands := make([][]string, 0)
// this works, it might be ugly, but it works.
line = strings.Replace(line, "\\/", "&#sslash;", -1)
line = strings.Replace(line, "/", "&#slash;", -1)
line = strings.Replace(line, "&#sslash;", "/", -1)
lines := strings.Split(line, "&#slash;")
var i int
var command []string
var c string
Lines:
for i < len(lines) {
c = lines[i]
c = strings.Replace(c, " ", "", -1) // Get rid of space
isLineCommand, lineResult, numberParsed := lineCommand(c)
switch {
case isLineCommand:
command = lineResult
LogS(fmt.Sprint(numberParsed))
lines[i] = lines[i][:numberParsed]
if numberParsed < len(c) {
i--
}
case c == "color":
display.RandomColor()
return [][]string{} // No commands
// saving
case c == "w":
ed.Save()
return [][]string{} // No commands
case c == "d":
command = []string{c}
// Parse ?re?
case len(c) > 0 && c[0] == '?':
command = []string{"?", c[1 : len(c)-1]}
break
// Do nothing at the end.
case i == len(lines)-1:
break Lines
// Parse S
case c == "s":
if len(lines) > i+2 {
command1 := []string{"x", lines[i+1]}
commands = append(commands, command1)
command = []string{"c", lines[i+2]}
i += 2
} else { // basically ignore the command otherwise.
i++
continue Lines
}
// Everything else gets one argument.
default:
command = []string{c, lines[i+1]}
i++ // One argument
}
i++ // for itself
commands = append(commands, command)
}
LogS(fmt.Sprint(commands))
return commands
}
func makeLog() func([]rune) {
file, err := os.Create(".log")
if err != nil {
panic(err)
}
return func(input []rune) {
file.WriteString(string(input))
if input[len(input)-1] != '\n' {
file.WriteString("\n")
}
file.Sync()
}
}
func parseSlash() {
runes := input.Runes()
defer func() {
input.AddRune('/')
input.Draw()
}()
// input.Reset()
if len(runes) == 0 {
return
}
commands := parseLine(string(runes))
ed.SaveDot()
if onlyHighlights(commands) {
for _, command := range commands {
ed.Command(command[0], command[1:])
}
display.Highlight(ed.Highlights())
display.Draw()
ed.UnSaveDot()
return
}
}
func LogS(str string) {
Log([]rune(str))
}
func Exit() {
LogS("Exiting now")
display.Reset()
os.Exit(0)
}
func Poll() {
for {
e := termbox.PollEvent()
// LogS(fmt.Sprintf("%d", e.Key))
switch {
case e.Type == termbox.EventResize:
display.Resize()
display.Draw()
case e.Key == termbox.KeyCtrlA: // Control-A
input.CursorAtBeginning()
case e.Key == termbox.KeyCtrlE: // Control-E
input.CursorAtEnd()
case e.Key == termbox.KeyCtrlC: // Control-C
Exit()
// Scrolling
case e.Key == termbox.KeyCtrlN:
display.ScrollDown()
case e.Key == termbox.KeyCtrlP:
display.ScrollUp()
case e.Key == termbox.KeyEsc:
input.Reset()
case e.Key == termbox.KeySpace: //Space
input.AddRune(' ')
input.Draw()
case e.Key == termbox.KeyEnter: // Return
runes := input.Runes()
commandHistory.Add(runes)
input.Reset()
if len(runes) == 0 {
break
}
commands := parseLine(string(runes))
for _, command := range commands {
ed.Command(command[0], command[1:])
}
display.ShowFile([]rune(ed.String()))
display.Highlight(ed.Highlights())
display.Draw()
case e.Ch == '/':
parseSlash()
// // Arrow keys
// Cursor Movement
case e.Key == termbox.KeyArrowLeft:
input.CursorLeft()
case e.Key == termbox.KeyArrowRight:
input.CursorRight()
case e.Key == termbox.KeyArrowUp:
past, err := commandHistory.Pop()
if err != nil {
// Empty stack.
// show alert later.
break
}
input.Reset()
input.SetRunes(past.([]rune))
input.Draw()
break
case e.Key == termbox.KeyArrowDown:
input.Reset()
past, err := commandHistory.UnPop()
if err != nil {
// Empty stack.
// show alert later.
break
}
input.SetRunes(past.([]rune))
input.Draw()
break
// Delete Key
case e.Key == termbox.KeyBackspace || e.Key == termbox.KeyBackspace2 || e.Key == termbox.KeyDelete:
input.Backspace()
input.Draw()
case e.Key == 0: // All other normal chars
input.AddRune(e.Ch)
input.Draw()
}
}
}
func contains(strings []string, match string) bool {
for _, str := range strings {
if str == match {
return true
}
}
return false
}
func init() {
flags := make([]string, 0)
files = make([]string, 0)
for _, arg := range os.Args[1:] {
if arg[0] == '-' {
flags = append(flags, arg)
continue
}
files = append(files, arg)
}
if contains(flags, "-v") || contains(flags, "--version") {
fmt.Println("The Glorious Pat Text Editor : v0.0.1")
os.Exit(0)
} else if contains(flags, "-rc") {
fmt.Println("Yay Recurse Center!")
os.Exit(0)
}
if len(files) == 0 {
fmt.Println("usage: pat [file]")
os.Exit(0)
}
display.Init(!(contains(flags, "--bottom") || contains(flags, "-b"))) // topbar
commandHistory = stack.New()
Log = makeLog()
display.Log = Log
display.LogS = LogS
input.Log = Log
input.LogS = LogS
editor.LogS = LogS
}
func main() {
defer display.Reset()
bytes, err := ioutil.ReadFile(files[0])
if err != nil {
panic(err)
}
ed = editor.NewEditor(bytes, files[0])
display.Show([]rune(string(bytes)), []rune{})
display.Draw()
Poll()
}