-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathshell_completion.go
138 lines (109 loc) · 2.62 KB
/
shell_completion.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
package main
import (
"context"
"os"
"path/filepath"
"sort"
"strings"
"time"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/komplete"
)
type shellCompletionCmd struct {
*komplete.Command `embed:""`
}
func (c *shellCompletionCmd) Help() string {
return text.Dedent(`
To set up shell completion, eval the output of this command
from your shell's rc file.
For example:
# bash
eval "$(gs shell completion bash)"
# zsh
eval "$(gs shell completion zsh)"
# fish
eval "$(gs shell completion fish)"
If shell name is not provided, the current shell is guessed
using a heuristic.
`)
}
func predictBranches(_ komplete.Args) (predictions []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
repo, err := git.Open(ctx, ".", git.OpenOptions{})
if err != nil {
return nil
}
branches, err := repo.LocalBranches(ctx)
if err != nil {
return nil
}
for _, branch := range branches {
predictions = append(predictions, branch.Name)
}
return predictions
}
func predictTrackedBranches(_ komplete.Args) (predictions []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
repo, err := git.Open(ctx, ".", git.OpenOptions{})
if err != nil {
return nil
}
db := newRepoStorage(repo, nil /* log */)
store, err := state.OpenStore(ctx, db, nil /* log */)
if err != nil {
return nil // not initialized
}
branches, err := store.ListBranches(ctx)
if err != nil {
return nil
}
return branches
}
func predictRemotes(_ komplete.Args) (predictions []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
repo, err := git.Open(ctx, ".", git.OpenOptions{})
if err != nil {
return nil
}
remotes, err := repo.ListRemotes(ctx)
if err != nil {
return nil
}
return remotes
}
func predictDirs(args komplete.Args) (predictions []string) {
dir, last := filepath.Split(args.Last)
dir = filepath.Clean(dir)
ents, err := os.ReadDir(dir)
if err != nil {
return nil
}
sep := string(filepath.Separator)
for _, ent := range ents {
if !ent.IsDir() || strings.HasPrefix(ent.Name(), ".") {
continue
}
if strings.HasPrefix(ent.Name(), last) {
name := filepath.Join(dir, ent.Name())
if !strings.HasSuffix(name, sep) {
name += sep
}
predictions = append(predictions, name)
}
}
return predictions
}
func predictForges(_ komplete.Args) (predictions []string) {
var ids []string
for f := range forge.All {
ids = append(ids, f.ID())
}
sort.Strings(ids)
return ids
}