-
Notifications
You must be signed in to change notification settings - Fork 19
/
editor.go
398 lines (346 loc) · 8.63 KB
/
editor.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"fmt"
"strconv"
"strings"
"github.com/jroimartin/gocui"
)
type LayoutFunc func(*gocui.Gui) error
func NewLayoutFunc(pSt *State) LayoutFunc {
fnEditor := NewEditorFunc(pSt)
return func(pGui *gocui.Gui) error {
// Set when doing a double-esc
if pSt.ShouldQuit {
return gocui.ErrQuit
}
maxX, maxY := pGui.Size()
if v, err := pGui.SetView("cmd", 1, maxY-2, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.Editor = gocui.EditorFunc(fnEditor)
v.Editable = true
v.Clear()
}
// View: output for received messages (rest)
v, err := pGui.SetView("out", -1, -1, maxX, maxY-2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Wrap = true
v.Editor = gocui.EditorFunc(fnEditor)
v.Editable = true
pSt.Writer = v
}
// For more information about KeepAutoscrolling, see Scrolling in editor.go
v.Autoscroll = pSt.Mode != modeEscape || pSt.KeepAutoscrolling
pGui.Mouse = pSt.Mode == modeEscape
// calc dims of "Welcome" message
sLines := strings.Split(welcomeScreen, "\n")
hY := len(sLines) / 2
hX := 0
for _, line := range sLines {
if len(line) > hX {
hX = len(line)
}
}
hX = (hX / 2) + 3
// View: "Welcome" Message
if v, err := pGui.SetView("help", (maxX/2)-hX, (maxY/2)-hY-1, (maxX/2)+hX, (maxY/2)+hY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = true
v.Wrap = true
v.Title = "Welcome"
if version == "devel" && commit != "" {
version = commit
if len(version) > 5 {
version = version[:5]
}
}
v.Write([]byte(fmt.Sprintf(welcomeScreen, version)))
}
if pSt.HideHelp {
pGui.SetViewOnTop("out")
} else {
pGui.SetViewOnTop("help")
}
curView := "cmd"
if pSt.Mode == modeEscape {
curView = "out"
}
if _, err := pGui.SetCurrentView(curView); err != nil {
return err
}
modeBox(pSt, pGui)
if !pSt.FirstDrawDone {
if pSt.Settings.LastWebsocketURL != "" {
pSt.PrintDebug(fmt.Sprintf("The last URL you connected to was %q. Type <Esc>c<Enter> to connect.", pSt.Settings.LastWebsocketURL))
}
pSt.FirstDrawDone = true
}
return nil
}
}
func modeBox(pSt *State, g *gocui.Gui) {
maxX, maxY := g.Size()
for i := 0; i < maxX; i++ {
g.SetRune(i, maxY-2, '─', gocui.ColorWhite, gocui.ColorBlack)
}
ch := modeChars[pSt.Mode]
g.SetRune(0, maxY-1, ch.Char, gocui.ColorWhite|gocui.AttrBold, ch.BgColor)
g.SetRune(1, maxY-1, ' ', gocui.ColorBlack, 0)
}
type ActionFunc func(*State, string)
// enterActions is the actions that can be done when KeyEnter is pressed
// (outside of modeEscape), based on the mode.
var enterActions = [modeMax]ActionFunc{
modeInsert: enterActionSendMessage,
modeOverwrite: enterActionSendMessage,
modeConnect: enterActionConnect,
modeSetPing: enterActionSetPing,
}
type EditorFunc func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier)
func NewEditorFunc(pSt *State) EditorFunc {
return func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
pSt.HideHelp = true
if pSt.Mode == modeEscape {
escEditor(pSt, v, key, ch, mod)
return
}
if ch != 0 && mod == 0 {
v.EditWrite(ch)
}
switch key {
case gocui.KeyEsc:
pSt.Mode = modeEscape
pSt.KeepAutoscrolling = true
// Space, backspace, Del
case gocui.KeySpace:
v.EditWrite(' ')
case gocui.KeyBackspace, gocui.KeyBackspace2:
v.EditDelete(true)
moveAhead(v)
case gocui.KeyDelete:
v.EditDelete(false)
// Cursor movement
case gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
moveAhead(v)
case gocui.KeyArrowRight:
x, _ := v.Cursor()
x2, _ := v.Origin()
x += x2
buf := v.Buffer()
// Position of cursor should be on space that gocui adds at the end if at end
if buf != "" && len(strings.TrimRight(buf, "\r\n")) > x {
v.MoveCursor(1, 0, false)
}
// Insert/Overwrite
case gocui.KeyInsert:
if pSt.Mode == modeInsert {
pSt.Mode = modeOverwrite
} else {
pSt.Mode = modeInsert
}
v.Overwrite = pSt.Mode == modeOverwrite
// History browse
case gocui.KeyArrowDown:
n := pSt.BrowseActions(-1)
setText(v, n)
case gocui.KeyArrowUp:
n := pSt.BrowseActions(1)
setText(v, n)
case gocui.KeyEnter:
buf := v.Buffer()
v.Clear()
v.SetCursor(0, 0)
if buf != "" {
buf = buf[:len(buf)-1]
}
if strings.TrimSpace(buf) != "" {
pSt.PushAction(buf)
pSt.ActionIndex = -1
}
enterActions[pSt.Mode](pSt, buf)
}
}
}
func setText(v *gocui.View, text string) {
v.Clear()
v.Write([]byte(text))
v.SetCursor(len(text), 0)
}
// moveAhead makes sure there are at least 8 characters visibile to the left
// of the cursor when moving backwards, mostly useful when deleting text.
func moveAhead(v *gocui.View) {
cX, _ := v.Cursor()
oX, _ := v.Origin()
if cX < 8 && oX > 0 {
newOX := oX - 1
forward := 1
if newOX < 0 {
forward += newOX
newOX = 0
}
v.SetOrigin(newOX, 0)
v.MoveCursor(forward, 0, false)
}
}
func enterActionSetPing(pSt *State, buf string) {
secs, _ := strconv.Atoi(strings.TrimSpace(buf))
pSt.SetPingInterval(secs)
if secs > 0 {
pSt.PrintDebug(fmt.Sprintf("Ping interval set to %d seconds.", secs))
} else {
pSt.PrintDebug("Ping disabled.")
}
pSt.Mode = modeInsert
}
func enterActionSendMessage(pSt *State, buf string) {
if strings.TrimSpace(buf) != "" {
pSt.PrintFromUser(buf)
pSt.WsSendMsg(buf)
}
}
func enterActionConnect(pSt *State, buf string) {
pSt.Mode = modeInsert
go pSt.StartConnection(buf)
}
func moveDown(v *gocui.View) {
_, yPos := v.Cursor()
if _, err := v.Line(yPos + 1); err == nil {
v.MoveCursor(0, 1, false)
}
}
// escEditor handles keys when esc has been pressed.
func escEditor(pSt *State, v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch key {
case gocui.KeyEsc:
// silently ignore - we're already in esc mode!
case gocui.KeyInsert:
pSt.Mode = modeInsert
// Scrolling
//
// When one of the movements keys is pressed, autoscrolling is disabled so that
// the user can move through the output without having text moving underneath
// the cursor.
case gocui.KeyArrowUp, gocui.MouseWheelDown:
pSt.KeepAutoscrolling = false
v.MoveCursor(0, -1, false)
case gocui.KeyArrowDown, gocui.MouseWheelUp:
pSt.KeepAutoscrolling = false
moveDown(v)
case gocui.KeyArrowLeft:
pSt.KeepAutoscrolling = false
v.MoveCursor(-1, 0, false)
case gocui.KeyArrowRight:
pSt.KeepAutoscrolling = false
_, y := v.Cursor()
if _, err := v.Word(0, y); err == nil {
v.MoveCursor(1, 0, false)
}
case gocui.KeyPgup:
pSt.KeepAutoscrolling = false
_, ySize := v.Size()
for i := 0; i < ySize; i++ {
v.MoveCursor(0, -1, false)
}
case gocui.KeyPgdn:
pSt.KeepAutoscrolling = false
_, ySize := v.Size()
for i := 0; i < ySize; i++ {
moveDown(v)
}
case gocui.KeyHome:
pSt.KeepAutoscrolling = false
v.SetCursor(0, 0)
v.SetOrigin(0, 0)
case gocui.KeyEnd:
pSt.KeepAutoscrolling = false
lines := len(strings.Split(v.ViewBuffer(), "\n"))
_, y := v.Size()
origin := lines - y - 1
if origin < 0 {
origin = 0
}
v.SetOrigin(0, origin)
cursorY := y - 1
if lines <= y {
cursorY = lines - 2
}
v.SetCursor(0, cursorY)
}
if ch == 0 {
return
}
switch ch {
case 'c':
pSt.Mode = modeConnect
return
case 'p':
pSt.Mode = modeSetPing
return
case 'q':
if err := pSt.WsClose(); len(err) > 0 {
for _, e := range err {
pSt.PrintError(e)
}
}
pSt.PrintDebug("WebSocket closed (use C-c to quit)")
return
case 'i':
// goes into insert mode
case 'h':
pSt.HideHelp = false
case 'j':
// toggle JSON formatting
pSt.Settings.JSONFormatting = !pSt.Settings.JSONFormatting
err := pSt.Settings.Update("JSONFormatting")
if err != nil {
pSt.PrintError(err)
}
e := "disabled"
if pSt.Settings.JSONFormatting {
e = "enabled"
}
pSt.PrintDebug("JSON formatting " + e)
case 't':
// toggle timestamps
if pSt.Settings.Timestamp == "" {
pSt.Settings.Timestamp = "=> 2006-01-02 15:04:05 "
} else {
pSt.Settings.Timestamp = ""
}
err := pSt.Settings.Update("Timestamp")
if err != nil {
pSt.PrintError(err)
}
e := "disabled"
if pSt.Settings.Timestamp != "" {
e = "enabled"
}
pSt.PrintDebug("Timestamps " + e)
case 'R':
// overwrite mode
pSt.Mode = modeOverwrite
return
default:
pSt.PrintDebug("No action for key '" + string(ch) + "'")
return
}
pSt.Mode = modeInsert
}
const welcomeScreen = `
claws %s
Awesome WebSocket Client
Ctrl-C quit
<Esc>c connect to specified websocket
<Esc>q close websocket
<Esc>p set ping interval (in seconds)
<Up>/<Down> navigate history
https://howl.moe/claws
`