-
Notifications
You must be signed in to change notification settings - Fork 0
/
shelltrix.go
273 lines (240 loc) · 6.62 KB
/
shelltrix.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 shelltrix
import (
"fmt"
"os"
"strings"
"github.com/c-bata/go-prompt"
)
// CommandHandler defines the function type for handling commands
type CommandHandler func([]string) error
// SecondarySuggester defines a function for replacing suggestions for subcommands
type SecondarySuggester func(string) *[]prompt.Suggest
// ExtraHelpText defines a function for commands to produce more help text
type ExtraHelpText func([]string) *string
// Command maps commands to handlers
// Required: Name, Handler, Description
// Optional: Aliases, SecondarySuggester, ExtraHelp
type Command struct {
Name string
Handler CommandHandler
Description string
Aliases []string
Secondary SecondarySuggester
ExtraHelp ExtraHelpText
}
type HelpFormatHints struct {
longestCommand int
longestDescription int
}
var (
// Built-in commands only. Users add commands with CommandAdd
commandsBuiltIn = map[string]Command{
"exit": {
Name: "exit",
Handler: handleExit,
Description: "Exit this program",
Aliases: []string{"quit", "q"},
},
}
// Cache of commands added by external code
commandsExt = map[string]Command{}
// Dynamic list of all commands
commandsAll = map[string]Command{}
// Reverse map of alises -> commands
commandAliases = map[string]Command{}
// Cache toplevel suggestions
suggestionsTop []prompt.Suggest
// Active suggestion list
suggestions *[]prompt.Suggest
// Compute & cache some hints for help display
helpHints HelpFormatHints
)
// =-= HANDLERS =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func handleExit(args []string) error {
os.Exit(0)
return nil
}
// this can't actually be referenced from the map. okay, go?
func handleHelp(args []string) error {
if len(args) > 1 {
base := args[1]
cmd, exists := commandsAll[base]
if exists {
h := cmd.ExtraHelp
if h != nil {
text := h(args[1:])
fmt.Println(*text)
} else {
fmt.Printf("No more help for '%s'\n", base)
}
} else {
// Maybe it's an alias?
alias := aliasSearch(base)
if alias != nil {
newargs := args[2:]
retry := []string{"help", alias.Name}
retry = append(retry, newargs...)
fmt.Printf("(FYI: '%s' is an alias for '%s'.)\n\n", base, alias.Name)
handleHelp(retry)
} else {
fmt.Printf("I dunno about '%s'.\n", base)
}
}
} else {
var c = commandsAll
for name, val := range c {
fmt.Printf(" %*s : %-*s", helpHints.longestCommand, name,
helpHints.longestDescription, val.Description)
if val.Aliases != nil {
fmt.Printf(" | Aliases: ")
for _, a := range val.Aliases {
fmt.Printf("%s ", a)
}
}
fmt.Printf("\n")
}
}
return nil
}
// =-= </HANDLERS> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func completer(d prompt.Document) []prompt.Suggest {
if d.Text == "" {
return nil
}
// fmt.Println(d)
// If we have something in the commandline and we've just hit 'space',
// indicating a new 'word', we can replace the suggester with per-command
// suggestions to allow each command to customize sub-command completions.
if d.GetWordBeforeCursorWithSpace() != "" && d.GetWordBeforeCursor() == "" {
// Get the whole commandline as it stands
cmdline := d.CurrentLine()
// Pop off the root command to figure out if we have a secondary
words := strings.Fields(cmdline)
root := words[0]
cur := words[len(words)-1]
if commandsAll[root].Secondary != nil {
suggestions = commandsAll[root].Secondary(cur)
} else {
acmd := aliasSearch(root)
if acmd != nil {
suggestions = acmd.Secondary(cur)
} else {
suggestions = &[]prompt.Suggest{}
}
}
} else if d.CursorPositionCol() == 1 {
// reset
suggestions = &suggestionsTop
}
return prompt.FilterHasPrefix(*suggestions, d.GetWordBeforeCursor(), true)
}
func initSuggestions() {
suggestionsTop = []prompt.Suggest{}
for name, val := range commandsAll {
t := prompt.Suggest{Text: name, Description: val.Description}
suggestionsTop = append(suggestionsTop, t)
}
for name, val := range commandAliases {
t := prompt.Suggest{Text: name, Description: val.Name}
suggestionsTop = append(suggestionsTop, t)
}
suggestionsTop = append(suggestionsTop, prompt.Suggest{Text: "?",
Description: "Type 'help' to list commands"})
suggestionsTop = append(suggestionsTop, prompt.Suggest{Text: "help",
Description: "List commands and what they do"})
}
func reinitSuggestions() {
initSuggestions()
}
// aliasAdd adds an alias for a Command if it doesn't already exist
func aliasAdd(base Command, alias string) {
existing, exists := commandAliases[alias]
if exists {
fmt.Printf("Alias '%s' defined for command '%s', ", alias, base.Name)
fmt.Printf("but already exists under '%s'. Ignored.\n", existing.Name)
} else {
commandAliases[alias] = base
}
}
// addDefinedAliases sets up any aliases that might be defined in a Command
func addDefinedAliases(cmd Command) {
if cmd.Aliases != nil {
for _, a := range cmd.Aliases {
aliasAdd(cmd, a)
}
}
}
// aliasSearch resolves a potential alias to a command
func aliasSearch(c string) *Command {
entry, exists := commandAliases[c]
if exists {
return &entry
}
return nil
}
// initCommands initializes base commands
func initCommands() {
for name, val := range commandsBuiltIn {
commandsAll[name] = val
addDefinedAliases(val)
}
}
// scanHelp runs through all the defined help strings and caches formatting hints
func scanHelp() {
for name, val := range commandsAll {
if len(name) > helpHints.longestCommand {
helpHints.longestCommand = len(name)
}
if len(val.Description) > helpHints.longestDescription {
helpHints.longestDescription = len(val.Description)
}
}
}
// CommandAdd adds a new command to the shell
func CommandAdd(cmd Command) {
commandsExt[cmd.Name] = cmd
commandsAll[cmd.Name] = cmd
addDefinedAliases(cmd)
reinitSuggestions()
scanHelp()
}
func initShell() {
initCommands()
initSuggestions()
scanHelp()
}
// RunShell starts the input loop and takes over executions
func RunShell() {
initShell()
p := prompt.New(nil, completer,
prompt.OptionAddKeyBind(prompt.KeyBind{
Key: prompt.ControlC,
Fn: func(buf *prompt.Buffer) {
fmt.Println("interrupted")
os.Exit(130)
}}),
)
// input loop
for {
suggestions = &suggestionsTop
t := p.Input()
cmdargs := strings.Fields(t)
if len(cmdargs) > 0 {
cmd := cmdargs[0]
h, exists := commandsAll[cmd]
if exists {
h.Handler(cmdargs)
} else if cmd == "help" || cmd == "?" {
// help is special..
handleHelp(cmdargs)
} else if cmd != "" {
aliasedCmd := aliasSearch(cmd)
if aliasedCmd != nil {
aliasedCmd.Handler(cmdargs)
} else {
fmt.Printf("No such command: '%s'\n", cmd)
}
}
}
}
}