This repository has been archived by the owner on Aug 8, 2021. It is now read-only.
forked from Bios-Marcel/cordless
/
commandview.go
189 lines (152 loc) · 5.63 KB
/
commandview.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
package ui
import (
"strings"
tcell "github.com/gdamore/tcell/v2"
tview "github.com/gord-project/gview"
)
const noHistoryIndexSelected = -1
// CommandView contains a simple textview for output and an input field for
// input. All commands are added to the history when confirmed via enter.
type CommandView struct {
commandOutput *tview.TextView
commandInput *Editor
// commandHistoryIndex is the current index cycling through the history.
// -1 means that no index is selected.
commandHistoryIndex int
// commandHistory is just a flat array of text that the user has sent.
// The more recent ones are on the right side of the array.
commandHistory []string
onExecuteCommand func(command string)
}
// NewCommandView creates a new struct containing the components necessary
// for a command view. It also contains the state for those components.
func NewCommandView(app *tview.Application, onExecuteCommand func(command string)) *CommandView {
commandOutput := tview.NewTextView()
commandOutput.SetDynamicColors(true).
SetWordWrap(true).
SetWrap(true).
SetText("[::b]### Welcome back. ###\n If you need to know more, run the [::b]man[::-] command.\n").
SetBorder(true).
SetIndicateOverflow(true)
commandInput := NewEditor(app)
commandInput.internalTextView.
SetWrap(false).
SetWordWrap(false)
cmdView := &CommandView{
commandOutput: commandOutput,
commandInput: commandInput,
commandHistoryIndex: noHistoryIndexSelected,
commandHistory: make([]string, 0),
onExecuteCommand: onExecuteCommand,
}
commandInput.SetInputCapture(cmdView.handleInput)
return cmdView
}
// SetInputCaptureForInput defines the input capture for the input component of
// the command view while prioritizing the predefined handler before passing the
// event to the externally specified handler.
func (cmdView *CommandView) SetInputCaptureForInput(handler func(event *tcell.EventKey) *tcell.EventKey) {
cmdView.commandInput.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
passedEvent := cmdView.handleInput(event)
if passedEvent == nil {
return nil
}
return handler(passedEvent)
})
}
// SetInputCaptureForOutput defines the input capture for the output component
// of the command view while prioritizing the predefined handler before passing
// the event to the externally specified handler.
func (cmdView *CommandView) SetInputCaptureForOutput(handler func(event *tcell.EventKey) *tcell.EventKey) {
cmdView.commandOutput.SetInputCapture(handler)
}
func (cmdView *CommandView) handleInput(event *tcell.EventKey) *tcell.EventKey {
if event.Modifiers() == tcell.ModNone {
if event.Key() == tcell.KeyPgUp {
handler := cmdView.commandOutput.InputHandler()
handler(tcell.NewEventKey(tcell.KeyPgUp, 0, tcell.ModNone), nil)
return nil
}
if event.Key() == tcell.KeyPgDn {
handler := cmdView.commandOutput.InputHandler()
handler(tcell.NewEventKey(tcell.KeyPgDn, 0, tcell.ModNone), nil)
return nil
}
if event.Key() == tcell.KeyEnter {
//We are resetting the index whenever hitting enter, no matter
//whether the command itself was run successfully or not.
//So that when you cycle up again, you'll be at the most recent command,
//meaning the one that you just entered.
cmdView.commandHistoryIndex = noHistoryIndexSelected
command := cmdView.commandInput.GetText()
if command == "" {
return nil
}
cmdView.onExecuteCommand(strings.TrimSpace(command))
cmdView.commandInput.SetText("")
cmdView.commandHistory = append(cmdView.commandHistory, command)
return nil
}
if event.Key() == tcell.KeyDown {
if cmdView.commandHistoryIndex > len(cmdView.commandHistory)-1 {
cmdView.commandHistoryIndex = 0
} else {
cmdView.commandHistoryIndex++
}
if cmdView.commandHistoryIndex > len(cmdView.commandHistory)-1 {
return nil
}
cmdView.commandInput.SetText(cmdView.commandHistory[cmdView.commandHistoryIndex])
}
if event.Key() == tcell.KeyUp {
if cmdView.commandHistoryIndex < 0 {
cmdView.commandHistoryIndex = len(cmdView.commandHistory) - 1
} else {
cmdView.commandHistoryIndex--
}
if cmdView.commandHistoryIndex < 0 {
return nil
}
cmdView.commandInput.SetText(cmdView.commandHistory[cmdView.commandHistoryIndex])
}
}
if event.Modifiers() == tcell.ModCtrl {
if event.Key() == tcell.KeyUp {
handler := cmdView.commandOutput.InputHandler()
handler(tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone), nil)
return nil
}
if event.Key() == tcell.KeyDown {
handler := cmdView.commandOutput.InputHandler()
handler(tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone), nil)
return nil
}
}
return event
}
// GetCommandInputWidget returns the component that can be added to the layout
// for the users command input.
func (cmdView *CommandView) GetCommandInputWidget() *tview.TextView {
return cmdView.commandInput.internalTextView
}
// GetCommandOutputWidget is the component that can be added to the layout
// for the users command output.
func (cmdView *CommandView) GetCommandOutputWidget() *tview.TextView {
return cmdView.commandOutput
}
// SetVisible sets the given visible state to both the input component and
// the output component
func (cmdView *CommandView) SetVisible(visible bool) {
cmdView.commandInput.internalTextView.SetVisible(visible)
cmdView.commandOutput.SetVisible(visible)
}
// Write lets us implement the io.Writer interface. Tab characters will be
// replaced with TabSize space characters. A "\n" or "\r\n" will be interpreted
// as a new line.
func (cmdView *CommandView) Write(p []byte) (n int, err error) {
n, err = cmdView.commandOutput.Write(p)
if err == nil {
cmdView.commandOutput.ScrollToEnd()
}
return
}