-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample3.go
58 lines (50 loc) · 1.28 KB
/
example3.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
//go:build run
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/completion"
"github.com/nyaosorg/go-readline-ny/keys"
)
func mains() error {
var editor readline.Editor
editor.PromptWriter = func(w io.Writer) (int, error) {
return io.WriteString(w, "menu> ")
}
candidates := []string{"list", "say", "pewpew", "help", "exit", "Space Command"}
// If you do not want to list files with double-tab-key,
// use `CmdCompletion2` instead of `CmdCompletionOrList2`
editor.BindKey(keys.CtrlI, &completion.CmdCompletionOrList2{
// Characters listed here are excluded from completion.
Delimiter: "&|><;",
// Enclose candidates with these characters when they contain spaces
Enclosure: `"'`,
// String to append when only one candidate remains
Postfix: " ",
// Function for listing candidates
Candidates: func(field []string) (forComp []string, forList []string) {
if len(field) <= 1 {
return candidates, candidates
}
return nil, nil
},
})
ctx := context.Background()
for {
line, err := editor.ReadLine(ctx)
if err != nil {
return err
}
fmt.Printf("TEXT=%#v\n", line)
}
return nil
}
func main() {
if err := mains(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}