-
Notifications
You must be signed in to change notification settings - Fork 0
/
ublumgr.go
311 lines (295 loc) · 7.73 KB
/
ublumgr.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
// Package goublu UbluManager is the gocui manager for Ublu input and output.
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
)
// How far from bottom we reserve our input area
const inputLineOffset = 3
// UbluManager is a gocui Manager for Ublu io.
type UbluManager struct {
U *Ublu
G *gocui.Gui
Opts *Options
Hist *History
CommandLineEditor gocui.Editor
Completor *Completor
ExitChan chan string
Dialoging bool
CompileDate string
Version string
}
// NewUbluManager instances a new manager.
func NewUbluManager(ublu *Ublu, g *gocui.Gui, opts *Options, hist *History) (um *UbluManager) {
um = &UbluManager{
U: ublu,
G: g,
Opts: opts,
Hist: hist,
Completor: NewCompletor(),
ExitChan: make(chan string),
Dialoging: false,
}
um.CommandLineEditor = gocui.EditorFunc(func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
cx, cy := v.Cursor()
text, _ := v.Line(cy)
text = strings.Trim(strings.TrimSpace(text), "\000")
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case ch == 'b' && mod == gocui.ModAlt:
backWord(v, cx, text)
case ch == 'f' && mod == gocui.ModAlt:
foreWord(v, cx, text)
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
case key == gocui.KeyDelete:
v.EditDelete(false)
case key == gocui.KeyInsert:
v.Overwrite = !v.Overwrite
case key == gocui.KeyEnter:
um.Ubluin(um.G, v)
termbox.Interrupt() // for good luck
case key == gocui.KeyArrowDown:
replaceLine(v, cx, um.Hist.Forward())
case key == gocui.KeyArrowUp:
replaceLine(v, cx, um.Hist.Back())
case key == gocui.KeyPgup:
replaceLine(v, cx, um.Hist.First())
case key == gocui.KeyPgdn:
replaceLine(v, cx, um.Hist.Last())
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
v.MoveCursor(1, 0, false)
case key == gocui.KeyCtrlSpace:
replaceLine(v, cx, um.tryComplete(text))
case key == gocui.KeyCtrlA || key == gocui.KeyHome:
v.MoveCursor(0-cx, 0, false)
case key == gocui.KeyCtrlB:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyCtrlD:
delWord(v, cx, text)
case key == gocui.KeyCtrlE || key == gocui.KeyEnd:
v.MoveCursor(len(text)-cx, 0, false)
case key == gocui.KeyCtrlF:
v.MoveCursor(1, 0, false)
case key == gocui.KeyCtrlK:
// this isn't quite correct but sorta works
for i := cy; i < len(text); i++ {
v.EditDelete(false)
}
case key == gocui.KeyF1:
rm := NewHelpReq(um, um.G)
rm.StartReq()
case key == gocui.KeyF2:
rm := NewAllOutReq(um, um.G)
rm.StartReq()
case key == gocui.KeyF3:
um.doExitDialog(g, 9, 3)
case key == gocui.KeyF4:
f, err := ioutil.TempFile(um.Opts.SaveOutDir, "goublu.out.")
if err != nil {
log.Panicln(err)
}
um.Ubluout(um.G, "Saving output to "+f.Name()+"\n")
f.Write([]byte(um.Hist.AllOut))
f.Close()
case key == gocui.KeyF5:
replaceLine(v, cx, um.tryExpand(text))
case key == gocui.KeyF9:
replaceLine(v, cx, um.Hist.BackWrap())
case key == gocui.MouseLeft:
case key == gocui.MouseMiddle:
case key == gocui.MouseRight:
case key == gocui.MouseRelease:
case key == gocui.MouseWheelUp:
case key == gocui.MouseWheelDown:
}
if key != gocui.KeyCtrlSpace {
um.Completor.Valid = false
}
})
go func() {
var exitMsg string
for {
exitMsg = <-um.ExitChan
if exitMsg == "YES" {
um.G.Close()
fmt.Printf("Ublu exiting quickly on F3. Try to use 'bye' instead.\n")
um.U.QuickExit(3)
}
}
}()
return um
}
// Ubluin pipes input to Ublu.
func (um *UbluManager) Ubluin(g *gocui.Gui, v *gocui.View) {
var l string
var err error
cx, cy := v.Cursor()
_, gy := g.Size()
if l, err = v.Line(cy); err != nil {
l = ""
}
l = strings.Trim(strings.TrimSpace(l), "\000")
um.Ubluout(g, l+"\n")
io.WriteString(um.U.Stdin, l+"\n")
if l != "" {
um.Hist.Append(l)
}
v.Clear()
v.MoveCursor(0-cx, (gy-inputLineOffset)-cy, false)
}
// Ubluout writes to console output from Ublu.
func (um *UbluManager) Ubluout(g *gocui.Gui, text string) {
v, err := g.View("Ubluout")
if err != nil {
// handle error
}
fmt.Fprint(v, text)
um.Hist.AppendAllOut(text)
termbox.Interrupt()
}
// Layout is the obligatory gocui layout redraw function
func (um *UbluManager) Layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("Ubluout", 0, 0, maxX-1, maxY-inputLineOffset); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = " Ublu Output [F1 Help] [F2 Review] [F3 Quit?] [F4 Save] [F5 Macro] [F9 Prev] "
v.Autoscroll = true
v.Wrap = true
v.BgColor = um.Opts.BgColorOut
v.FgColor = um.Opts.FgColorOut
}
if v, err := g.SetView("Ubluin", 0, maxY-inputLineOffset, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = " Ublu Input "
v.Autoscroll = true
v.Editable = true
v.Editor = um.CommandLineEditor
v.Wrap = true
v.BgColor = um.Opts.BgColorIn
v.FgColor = um.Opts.FgColorIn
}
if !um.Dialoging {
if _, err := g.SetCurrentView("Ubluin"); err != nil {
return err
}
}
return nil
}
func (um *UbluManager) tryComplete(text string) (newtext string) {
newtext = text
if um.Completor.Valid == false {
um.Completor.Clear()
}
if text != "" {
words := strings.Fields(text)
lastword := words[len(words)-1]
candidate := um.Completor.Next()
if candidate == "" {
candidate = um.Completor.Complete(lastword)
}
if candidate != "" {
newtext = text[0:strings.LastIndex(text, lastword)] + candidate
}
}
return newtext
}
func (um *UbluManager) tryExpand(text string) (newtext string) {
newtext = text
if text != "" {
words := strings.Fields(text)
lastword := words[len(words)-1]
candidate := um.Opts.Macros.Expand(lastword)
if candidate != "" {
newtext = text[0:strings.LastIndex(text, lastword)] + candidate
}
} else {
um.Ubluout(um.G, "Goublu version "+um.Version+" compiled "+um.CompileDate+"\n")
um.Ubluout(um.G, "***\n")
um.Ubluout(um.G, "Goublu Options:\n")
um.Ubluout(um.G, um.Opts.AllOpts())
um.Ubluout(um.G, "***\n")
um.Ubluout(um.G, "Goublu Macros:\n")
um.Ubluout(um.G, um.Opts.Macros.AllMacros())
um.Ubluout(um.G, "***\n")
}
return newtext
}
func foreWord(v *gocui.View, cx int, text string) {
nbfound := false
if cx <= len(text) {
i := cx
for ; i < len(text); i++ {
if text[i] != ' ' {
nbfound = true
}
if nbfound && text[i] == ' ' {
break
}
}
v.MoveCursor(i-cx, 0, false)
}
}
func backWord(v *gocui.View, cx int, text string) {
nbfound := false
if cx > 0 {
i := Min(cx, len(text))
for i = i - 1; i >= 0; i-- {
if text[i] != ' ' {
nbfound = true
}
if nbfound && text[i] == ' ' {
break
}
}
v.MoveCursor(i-cx, 0, false)
}
}
func delWord(v *gocui.View, cx int, text string) {
// if text[len(text)-1] != ' ' {
// text = text + " "
// }
offset := Min(cx, len(text))
head := text[:offset]
tail := text[offset:]
if len(tail) > 0 {
tailSplit := strings.SplitN(tail, " ", 2)
if len(tailSplit) > 1 {
if len(tailSplit[1]) > 0 {
if tailSplit[1][0] == ' ' {
replaceLine(v, cx, head+tailSplit[1])
} else {
replaceLine(v, cx, head+" "+tailSplit[1])
}
}
} else {
replaceLine(v, cx, text[:Max(0, cx)])
}
} else {
replaceLine(v, cx, text[:Max(0, cx)])
}
}
func replaceLine(v *gocui.View, cx int, newtext string) {
v.Clear()
v.MoveCursor(0-cx, 0, false)
for _, ch := range newtext {
v.EditWrite(ch)
}
}
func (um *UbluManager) doExitDialog(g *gocui.Gui, x int, y int) {
NewTextLineDialog(um, g, x, y, "Exit", gocui.ColorRed, gocui.ColorBlack, "NO\nYES").StartDialog(um.ExitChan)
}