-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzerz.go
270 lines (258 loc) · 7.6 KB
/
zerz.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
package main
import (
termutil "github.com/japanoise/termbox-util"
termbox "github.com/nsf/termbox-go"
)
const (
ZBgColor termbox.Attribute = termbox.ColorDefault
ZFgColor = termbox.ColorDefault
ZBgUPColor = ZBgColor
ZFgUPColor = termbox.ColorRed
ZBoxColor = ZFgColor
ZCursorChar = termbox.ColorGreen
ZCursorPattern = termbox.ColorBlue
ZCursorInt = termbox.ColorMagenta
ZCursorUInt = termbox.ColorYellow
ZCursorFg = termbox.ColorBlack
ZStatBg = ZBgColor
ZStatFg = termbox.AttrReverse
ZFlagColorL = termbox.ColorGreen
ZFlagColorR = termbox.ColorBlack
ZHelpBg = termbox.ColorWhite
ZHelpFg = termbox.ColorBlack
)
const (
ZBoxHor rune = '═'
ZBoxVer = '║'
ZBoxTopL = '╔'
ZBoxTopR = '╗'
ZBoxBotL = '╚'
ZBoxBotR = '╝'
ZLineVert = '│'
ZLineTUH = '┴'
ZLineHor = '─'
ZFlagTri = '◢'
)
func initTerm() {
err := termbox.Init()
if err != nil {
panic(err)
}
termbox.SetInputMode(termbox.InputAlt | termbox.InputMouse)
}
func box(x1, y1, x2, y2 int) {
termbox.SetCell(x1, y1, ZBoxTopL, ZBoxColor, ZBgColor)
termbox.SetCell(x1, y2, ZBoxBotL, ZBoxColor, ZBgColor)
termbox.SetCell(x2, y1, ZBoxTopR, ZBoxColor, ZBgColor)
termbox.SetCell(x2, y2, ZBoxBotR, ZBoxColor, ZBgColor)
for x := x1 + 1; x < x2; x++ {
termbox.SetCell(x, y1, ZBoxHor, ZBoxColor, ZBgColor)
termbox.SetCell(x, y2, ZBoxHor, ZBoxColor, ZBgColor)
}
for y := y1 + 1; y < y2; y++ {
termbox.SetCell(x1, y, ZBoxVer, ZBoxColor, ZBgColor)
termbox.SetCell(x2, y, ZBoxVer, ZBoxColor, ZBgColor)
}
}
func infoBox(title, prompt string, messages []string) {
done := false
sx, sy := termbox.Size()
lens := make([]int, 0, len(messages))
for _, message := range messages {
lens = append(lens, len(message))
}
for !done {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
box(1, 1, sx-2, sy-2)
termutil.PrintStringFgBg(2, 1, title, ZFgColor, ZBgColor)
termutil.PrintStringFgBg(2, 2, prompt, ZFgColor, ZBgColor)
for i, message := range messages {
if lens[i] > sx-4 {
termutil.PrintStringFgBg(2, 4+i, message[:sx-4], ZFgColor, ZBgColor)
} else {
termutil.PrintStringFgBg(2, 4+i, message, ZFgColor, ZBgColor)
}
}
termbox.Flush()
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventResize:
termbox.Sync()
sx, sy = termbox.Size()
case termbox.EventKey:
if ev.Key == termbox.KeyEnter || ev.Key ==
termbox.KeyCtrlC || ev.Key == termbox.KeyCtrlG ||
ev.Ch|0x20 == 'q' {
done = true
}
}
}
}
func showErrorList(title, prompt string, errors []error) {
messages := make([]string, 0, len(errors))
for _, err := range errors {
messages = append(messages, err.Error())
}
infoBox(title, prompt, messages)
}
func zerz(global *ZerzEditor, startupErrors []error) {
initTerm()
defer termbox.Close()
if len(startupErrors) > 0 {
showErrorList("Zerz", "Some files had errors:", startupErrors)
}
done := false
sx, sy := termbox.Size()
tabbarscroll := 0
for !done {
bufareatop, bufareabot := 2, sy-2
global.Draw(tabbarscroll, sx, sy, 0, bufareatop, sx, bufareabot)
termbox.Flush()
event := termbox.PollEvent()
if event.Type == termbox.EventResize {
termbox.Sync()
sx, sy = termbox.Size()
} else if event.Type == termbox.EventKey {
if event.Ch == 0 {
switch event.Key {
case termbox.KeyCtrlC:
done = true
case termbox.KeyCtrlF, termbox.KeyArrowRight:
if event.Mod == termbox.ModAlt {
global.FocusBuf().ForwardDWord()
} else {
global.FocusBuf().ForwardByte()
}
case termbox.KeyCtrlN, termbox.KeyArrowDown:
global.FocusBuf().ForwardParagraph()
case termbox.KeyCtrlB, termbox.KeyArrowLeft:
if event.Mod == termbox.ModAlt {
global.FocusBuf().BackwardDWord()
} else {
global.FocusBuf().BackwardByte()
}
case termbox.KeyCtrlP, termbox.KeyArrowUp:
global.FocusBuf().BackwardParagraph()
case termbox.KeyPgup:
global.PageUp(0, bufareatop, sx, bufareabot)
case termbox.KeyCtrlV, termbox.KeyPgdn:
global.PageDown(0, bufareatop, sx, bufareabot)
case termbox.KeyCtrlA, termbox.KeyHome:
global.FocusBuf().StartOfLine()
case termbox.KeyCtrlE, termbox.KeyEnd:
global.FocusBuf().EndOfLine()
case termbox.KeyF1:
helpscreen()
termbox.Sync()
sx, sy = termbox.Size()
case termbox.KeyEnter:
global.FocusBuf().Edit(global, tabbarscroll)
termbox.Sync()
sx, sy = termbox.Size()
}
} else if event.Mod == termbox.ModAlt {
if '1' <= event.Ch && event.Ch <= '9' && int(event.Ch-'1') < len(global.Buffers) {
global.SwitchBuf(int(event.Ch - '1'))
}
switch event.Ch {
case '<':
global.FocusBuf().StartOfFile()
case '>':
global.FocusBuf().EndOfFile()
case 'v':
global.PageUp(0, bufareatop, sx, bufareabot)
case 'f':
global.FocusBuf().ForwardWord()
case 'b':
global.FocusBuf().BackwardWord()
case 'g':
global.FocusBuf().GoTo(global, tabbarscroll)
termbox.Sync()
sx, sy = termbox.Size()
case '-', '_':
global.VSplit()
case '|':
global.HSplit()
case 'u', 'U':
global.SplitUp()
case 'd', 'D':
global.SplitDown()
case 'l', 'L':
global.SplitLeft()
case 'r', 'R':
global.SplitRight()
case '0':
global.KillSplit()
}
} else {
switch event.Ch {
case 'H':
if Int8 < global.FocusBuf().IntWidth {
global.FocusBuf().IntWidth--
}
case 'L':
if global.FocusBuf().IntWidth < Int64 {
global.FocusBuf().IntWidth++
}
case 'A':
// alt+arrows give ABCD on my term, and zerz is modal, so why not :)
global.SplitUp()
case 'B':
global.SplitDown()
case 'D':
global.SplitLeft()
case 'C':
global.SplitRight()
case 'c':
global.FocusBuf().Mode = ModeChar
case 'p', 'P':
global.FocusBuf().Mode = ModePattern
case 'i', 'I':
global.FocusBuf().Mode = ModeInt
case 'u', 'U':
global.FocusBuf().Mode = ModeUInt
case 'e', 'E':
global.FocusBuf().BigEndian = !global.FocusBuf().BigEndian
case '?':
helpscreen()
termbox.Sync()
sx, sy = termbox.Size()
case 'q', 'Q':
done = true
}
}
global.DoScroll(0, bufareatop, sx, bufareabot)
} else if event.Type == termbox.EventMouse {
if event.MouseY < 1 {
if (event.Key == termbox.MouseWheelUp || (event.Key == termbox.MouseLeft &&
event.MouseX == 0)) && tabbarscroll > 0 {
tabbarscroll--
} else if (event.Key == termbox.MouseWheelDown || (event.Key == termbox.MouseLeft &&
event.MouseX == sx-1)) && tabbarscroll < len(global.Buffers)-1 {
tabbarscroll++
} else if event.Key == termbox.MouseLeft {
otbx := 0
tbx := 1
i := tabbarscroll
for _, buf := range global.Buffers[tabbarscroll:] {
tbx += buf.File.FilenameWidth + 3
if otbx < event.MouseX && event.MouseX < tbx {
global.SwitchBuf(i)
continue
}
i++
otbx = tbx
}
}
} else if 1 < event.MouseY && event.MouseY <= bufareabot {
if event.Key == termbox.MouseWheelDown {
global.FocusBuf().ScrollDown()
} else if event.Key == termbox.MouseWheelUp {
global.ScrollUp(0, bufareatop, sx, bufareabot)
} else if event.Key == termbox.MouseLeft {
global.Click(0, bufareatop, sx, bufareabot, event.MouseX, event.MouseY)
}
}
}
}
}