-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_prompt.go
51 lines (43 loc) · 940 Bytes
/
exec_prompt.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
package main
import (
"fmt"
"github.com/c-bata/go-prompt"
"os"
)
type ExePrompt struct {
entries []*Entry
entryMap map[string]*Entry
}
func (e *ExePrompt) completer() prompt.Completer {
f := func(d prompt.Document) []prompt.Suggest {
s := make([]prompt.Suggest, 0)
for _, entry := range e.entries {
suggest := prompt.Suggest{
Text: entry.command.String(),
Description: entry.explain.String(),
}
s = append(s, suggest)
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
return f
}
func execCommand() {
exePrompt := &ExePrompt{
entries: LoadContent(),
entryMap: make(map[string]*Entry, 0),
}
for _, entry := range exePrompt.entries {
exePrompt.entryMap[entry.command.String()] = entry
}
for {
t := prompt.Input(">> ", exePrompt.completer())
entry, ok := exePrompt.entryMap[t]
if t == "exit" {
os.Exit(0)
}
if ok {
fmt.Println(entry.detail.String())
}
}
}