-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path07_completion_description.go
54 lines (49 loc) · 1.29 KB
/
07_completion_description.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
// 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 7:
// Go-prompt: vylepšená nápověda.
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_20/07_completion_description.html
//
package main
import (
"github.com/c-bata/go-prompt"
"os"
)
func executor(t string) {
switch t {
case "exit":
fallthrough
case "quit":
os.Exit(0)
case "help":
println("HELP:\nexit\nquit")
default:
println("Nothing happens")
}
return
}
func completer(in prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "help", Description: "show help with all commands"},
{Text: "exit", Description: "quit the application"},
{Text: "quit", Description: "quit the application"},
}
return prompt.FilterHasPrefix(s, in.GetWordBeforeCursor(), true)
}
func main() {
p := prompt.New(executor, completer)
p.Run()
}