forked from asciimoo/wuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
273 lines (249 loc) · 6.15 KB
/
commands.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"unicode"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
)
type CommandFunc func(*gocui.Gui, *gocui.View) error
var COMMANDS map[string]func(string, *App) CommandFunc = map[string]func(string, *App) CommandFunc{
"submit": func(_ string, a *App) CommandFunc {
return a.SubmitRequest
},
"saveResponse": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
return a.OpenSaveDialog(VIEW_TITLES[SAVE_RESPONSE_DIALOG_VIEW], g,
func(g *gocui.Gui, _ *gocui.View) error {
saveLocation := getViewValue(g, SAVE_DIALOG_VIEW)
if len(a.history) == 0 {
return nil
}
req := a.history[a.historyIndex]
if req.RawResponseBody == nil {
return nil
}
err := ioutil.WriteFile(saveLocation, req.RawResponseBody, 0644)
var saveResult string
if err == nil {
saveResult = "Response saved successfully."
} else {
saveResult = "Error saving response: " + err.Error()
}
viewErr := a.OpenSaveResultView(saveResult, g)
return viewErr
})
}
},
"loadRequest": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
return a.OpenSaveDialog(VIEW_TITLES[LOAD_REQUEST_DIALOG_VIEW], g,
func(g *gocui.Gui, _ *gocui.View) error {
defer a.closePopup(g, SAVE_DIALOG_VIEW)
loadLocation := getViewValue(g, SAVE_DIALOG_VIEW)
return a.LoadRequest(g, loadLocation)
})
}
},
"saveRequest": func(_ string, a *App) CommandFunc {
return a.SaveRequest
},
"history": func(_ string, a *App) CommandFunc {
return a.ToggleHistory
},
"quit": func(_ string, _ *App) CommandFunc {
return quit
},
"focus": func(args string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
return a.setViewByName(g, args)
}
},
"nextView": func(_ string, a *App) CommandFunc {
return a.NextView
},
"prevView": func(_ string, a *App) CommandFunc {
return a.PrevView
},
"scrollDown": func(_ string, _ *App) CommandFunc {
return scrollViewDown
},
"scrollUp": func(_ string, _ *App) CommandFunc {
return scrollViewUp
},
"pageDown": func(_ string, _ *App) CommandFunc {
return pageDown
},
"pageUp": func(_ string, _ *App) CommandFunc {
return pageUp
},
"deleteLine": func(_ string, _ *App) CommandFunc {
return deleteLine
},
"deleteWord": func(_ string, _ *App) CommandFunc {
return deleteWord
},
"openEditor": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, v *gocui.View) error {
return openEditor(g, v, a.config.General.Editor)
}
},
"toggleContextSpecificSearch": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
a.config.General.ContextSpecificSearch = !a.config.General.ContextSpecificSearch
a.PrintBody(g)
return nil
}
},
"clearHistory": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
a.history = make([]*Request, 0, 31)
a.historyIndex = 0
a.Layout(g)
return nil
}
},
"redirectRestriction": func(_ string, a *App) CommandFunc {
return func(g *gocui.Gui, _ *gocui.View) error {
a.config.General.FollowRedirects = !a.config.General.FollowRedirects
return nil
}
},
}
func scrollView(v *gocui.View, dy int) error {
v.Autoscroll = false
ox, oy := v.Origin()
if oy+dy < 0 {
dy = -oy
}
if _, err := v.Line(dy); dy > 0 && err != nil {
dy = 0
}
v.SetOrigin(ox, oy+dy)
return nil
}
func scrollViewUp(_ *gocui.Gui, v *gocui.View) error {
return scrollView(v, -1)
}
func scrollViewDown(_ *gocui.Gui, v *gocui.View) error {
return scrollView(v, 1)
}
func pageUp(_ *gocui.Gui, v *gocui.View) error {
_, height := v.Size()
scrollView(v, -height*2/3)
return nil
}
func pageDown(_ *gocui.Gui, v *gocui.View) error {
_, height := v.Size()
scrollView(v, height*2/3)
return nil
}
func deleteLine(_ *gocui.Gui, v *gocui.View) error {
if !v.Editable {
return nil
}
_, curY := v.Cursor()
_, oY := v.Origin()
currentLine := curY + oY
viewLines := strings.Split(strings.TrimSpace(v.Buffer()), "\n")
if currentLine >= len(viewLines) {
return nil
}
v.Clear()
if currentLine > 0 {
fmt.Fprintln(v, strings.Join(viewLines[:currentLine], "\n"))
}
fmt.Fprint(v, strings.Join(viewLines[currentLine+1:], "\n"))
v.SetCursor(0, currentLine)
v.SetOrigin(0, oY)
return nil
}
func deleteWord(_ *gocui.Gui, v *gocui.View) error {
cX, cY := v.Cursor()
oX, _ := v.Origin()
cX = cX - 1 + oX
line, err := v.Line(cY)
if err != nil || line == "" || cX < 0 {
return nil
}
if cX >= len(line) {
cX = len(line) - 1
}
origCharCateg := getCharCategory(rune(line[cX]))
v.EditDelete(true)
cX -= 1
for cX >= 0 {
c := rune(line[cX])
if origCharCateg != getCharCategory(c) {
break
}
v.EditDelete(true)
cX -= 1
}
return nil
}
func getCharCategory(chr rune) int {
switch {
case unicode.IsDigit(chr):
return 0
case unicode.IsLetter(chr):
return 1
case unicode.IsSpace(chr):
return 2
case unicode.IsPunct(chr):
return 3
}
return int(chr)
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func openEditor(g *gocui.Gui, v *gocui.View, editor string) error {
file, err := ioutil.TempFile(os.TempDir(), "wuzz-")
if err != nil {
return nil
}
defer os.Remove(file.Name())
val := getViewValue(g, v.Name())
if val != "" {
fmt.Fprint(file, val)
}
file.Close()
info, err := os.Stat(file.Name())
if err != nil {
return nil
}
cmd := exec.Command(editor, file.Name())
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err = cmd.Run()
// sync termbox to reset console settings
// this is required because the external editor can modify the console
defer g.Update(func(_ *gocui.Gui) error {
termbox.Sync()
return nil
})
if err != nil {
rv, _ := g.View(RESPONSE_BODY_VIEW)
rv.Clear()
fmt.Fprintf(rv, "Editor open error: %v", err)
return nil
}
newInfo, err := os.Stat(file.Name())
if err != nil || newInfo.ModTime().Before(info.ModTime()) {
return nil
}
newVal, err := ioutil.ReadFile(file.Name())
if err != nil {
return nil
}
v.SetCursor(0, 0)
v.SetOrigin(0, 0)
v.Clear()
fmt.Fprint(v, strings.TrimSpace(string(newVal)))
return nil
}