-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path10_two_word_commands.go
92 lines (82 loc) · 2.3 KB
/
10_two_word_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
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Dvacátá část
// Jazyk Go a aplikace s vlastním příkazovým řádkem
// https://www.root.cz/clanky/jazyk-go-a-aplikace-s-vlastnim-prikazovym-radkem/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z dvacáté části:
// https://github.com/tisnik/go-root/blob/master/article_20/README.md
//
// Demonstrační příklad číslo 10:
// Víceslovní příkazy.
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_20/10_two_word_commands.html
//
package main
import (
"github.com/c-bata/go-prompt"
"os"
"strings"
)
func executor(t string) {
switch t {
case "exit":
fallthrough
case "quit":
os.Exit(0)
case "help":
println("HELP:\nexit\nquit")
case "user add":
println("Adding user")
case "user del":
println("Deleting user")
default:
println("Nothing happens")
}
return
}
func completer(in prompt.Document) []prompt.Suggest {
blocks := strings.Split(in.TextBeforeCursor(), " ")
s := []prompt.Suggest{
{Text: "help", Description: "show help with all commands"},
{Text: "user", Description: "add/delete user"},
{Text: "ls", Description: "list users/storages/computers"},
{Text: "list", Description: "list users/storages/computers"},
{Text: "exit", Description: "quit the application"},
{Text: "quit", Description: "quit the application"},
}
userS := []prompt.Suggest{
{Text: "add", Description: "add new user"},
{Text: "assign", Description: "assign a role to user"},
{Text: "del", Description: "delete user"},
}
listS := []prompt.Suggest{
{Text: "users", Description: "show list of all users"},
{Text: "logs", Description: "show list of all logs"},
{Text: "storages", Description: "show list of all storages"},
{Text: "computers", Description: "show list of all computers"},
}
emptyS := []prompt.Suggest{}
if len(blocks) == 2 {
switch blocks[0] {
case "user":
return prompt.FilterHasPrefix(userS, blocks[1], true)
case "ls":
fallthrough
case "list":
return prompt.FilterHasPrefix(listS, blocks[1], true)
default:
return emptyS
}
}
return prompt.FilterHasPrefix(s, blocks[0], true)
}
func main() {
p := prompt.New(executor, completer)
p.Run()
}