-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.go
157 lines (125 loc) · 2.71 KB
/
complete.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
package cli
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"nikand.dev/go/cli/complete"
"tlog.app/go/errors"
)
var ErrCouldNotDetermineShell = errors.New("couldn't determine the shell")
var CompleteCmd = &Command{
Name: "_complete",
Usage: "[bash|zsh]",
Description: "print shell completion script",
Action: completeAuto,
Hidden: true,
Commands: []*Command{{
Name: "bash",
Action: completeBash,
}, {
Name: "zsh",
Action: completeZsh,
}},
}
func beforeComplete(c *Command) (err error) {
fn := "complete_log_app"
fn, err = filepath.Abs(fn)
if err != nil {
return errors.Wrap(err, "abs path")
}
f, err := os.Create(fn)
if err != nil {
return errors.Wrap(err, "create log file")
}
defer f.Close()
for _, e := range c.Env {
if strings.HasPrefix(e, "CLI_COMP_") {
fmt.Fprintf(f, "%v\n", e)
}
}
// return errors.New("we were here")
return nil
}
func DefaultComplete(c *Command) (err error) {
var repl []string
current := complete.Current(c)
if false && current == "" {
_, err = defaultHelp(nil, "", nil)
return
}
var dashes string
cur := current
{
i := 0
for i < len(cur) && cur[i] == '-' {
i++
}
dashes = cur[:i]
cur = cur[i:]
}
if dashes == "" {
for _, sub := range c.Commands {
cmd:
for _, name := range strings.Split(sub.Name, ",") {
if strings.HasPrefix(name, "_") != strings.HasPrefix(cur, "_") {
continue
}
if strings.HasPrefix(name, cur) {
repl = append(repl, name)
break cmd
}
}
}
} else {
for _, f := range c.Flags {
flg:
for _, name := range strings.Split(f.Name, ",") {
if (len(dashes) > 1) && (len(name) == 1) {
continue
}
if strings.HasPrefix(name, cur) {
dd := "--"
if len(name) == 1 {
dd = "-"
}
repl = append(repl, dd+name)
break flg
}
}
}
}
// tlog.Printw("complete", "dashes", dashes, "cur", cur, "opts", repl)
for i := range repl {
repl[i] = strconv.Quote(repl[i])
}
fmt.Fprintf(c.Stdout, "COMPREPLY=( $(compgen -W '%[2]s' -- %[1]s) )", current, strings.Join(repl, " "))
// fmt.Fprintf(c, "COMPREPLY=(%s)", strings.Join(repl, " "))
return nil
}
func completeAuto(c *Command) error {
sh, ok := complete.Shell(c)
if !ok {
return ErrCouldNotDetermineShell
}
root := c
for root.Parent != nil {
root = root.Parent
}
return complete.ExecTemplate(c.Stdout, sh, root.OSArgs)
}
func completeBash(c *Command) error {
root := c
for root.Parent != nil {
root = root.Parent
}
return complete.ExecTemplate(c.Stdout, "bash", root.OSArgs)
}
func completeZsh(c *Command) error {
root := c
for root.Parent != nil {
root = root.Parent
}
return complete.ExecTemplate(c.Stdout, "zsh", root.OSArgs)
}