Skip to content

Commit 97afd99

Browse files
committed
cmd: slightly improve stage auto complete
1 parent 86a9882 commit 97afd99

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

autocomplete/zsh_autocomplete

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
_cli_zsh_autocomplete() {
2-
32
local -a opts
43
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
5-
64
_describe 'values' opts
7-
85
return
96
}
107

cmd/help.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ EXAMPLES:
520520
"stage": {
521521
Usage: "Add a local file to the storage",
522522
ArgsUsage: "(<local-path> [<path>]|--stdin <path>)",
523-
Complete: completeArgsUsage,
523+
Complete: completeLocalFile,
524524
Flags: []cli.Flag{
525525
cli.BoolFlag{
526526
Name: "stdin,i",

cmd/suggest.go

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"os"
57
"sort"
68
"strings"
79

@@ -137,6 +139,41 @@ func completeArgsUsage(ctx *cli.Context) {
137139
}
138140
}
139141

142+
func completeLocalFile(ctx *cli.Context) {
143+
if len(os.Args) >= 2 {
144+
lastArg := os.Args[len(os.Args)-2]
145+
cmd := findCurrentCommand(ctx)
146+
if lastArg != cmd.FullName() {
147+
return
148+
}
149+
}
150+
151+
// CAVEAT: We currently do not get partial words from bash/zsh.
152+
// e.g. "brig stage /us" will pass the following os.Args:
153+
// ["brig", "stage", "--generate-bash-completion"]
154+
//
155+
// Because of that we do no prefix completion right now.
156+
// We can probably tweak autcomplete/{z,ba}sh_autcomplete to
157+
// somehow do this, but after 30mins of googling I give up for now.
158+
//
159+
// If you read this, I challenge you to do it better.
160+
dir, err := os.Getwd()
161+
if err != nil {
162+
// silent error.
163+
return
164+
}
165+
166+
children, err := ioutil.ReadDir(dir)
167+
if err != nil {
168+
// silent error.
169+
return
170+
}
171+
172+
for _, child := range children {
173+
fmt.Println(child.Name())
174+
}
175+
}
176+
140177
func completeSubcommands(ctx *cli.Context) {
141178
if command := findCurrentCommand(ctx); command != nil {
142179
for _, subCmd := range command.Subcommands {

0 commit comments

Comments
 (0)