Skip to content

Commit 58bac5d

Browse files
authored
Merge pull request #464 from rsteube/completer-desciptions
added completer descriptions
2 parents 962e7f7 + 9cbc12a commit 58bac5d

File tree

200 files changed

+282
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+282
-215
lines changed

cmd/carapace/cmd/root.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"strconv"
1112
"strings"
1213

1314
"github.com/rsteube/carapace/pkg/ps"
@@ -52,7 +53,7 @@ var rootCmd = &cobra.Command{
5253
case "--version":
5354
println(cmd.Version)
5455
case "--list":
55-
fmt.Println(strings.Join(completers, "\n"))
56+
printCompleters()
5657
case "_carapace":
5758
shell := ps.DetermineShell()
5859
if len(args) > 1 {
@@ -87,6 +88,19 @@ var rootCmd = &cobra.Command{
8788
DisableFlagParsing: true,
8889
}
8990

91+
func printCompleters() {
92+
maxlen := 0
93+
for _, name := range completers {
94+
if len := len(name); len > maxlen {
95+
maxlen = len
96+
}
97+
}
98+
99+
for _, name := range completers {
100+
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, descriptions[name])
101+
}
102+
}
103+
90104
func invokeCompleter(completer string) {
91105
old := os.Stdout
92106
r, w, _ := os.Pipe()

cmd/generate/gen.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,42 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os/exec"
7+
"regexp"
8+
"strconv"
79
"strings"
810
)
911

1012
func main() {
11-
completers := readCompleters()
13+
names, descriptions := readCompleters()
1214

13-
imports := make([]string, len(completers))
14-
for index, c := range readCompleters() {
15-
imports[index] = fmt.Sprintf(` %v "github.com/rsteube/carapace-bin/completers/%v_completer/cmd"`, varName(c), c)
15+
imports := make([]string, 0, len(names))
16+
for _, name := range names {
17+
imports = append(imports, fmt.Sprintf(` %v "github.com/rsteube/carapace-bin/completers/%v_completer/cmd"`, varName(name), name))
1618
}
1719

18-
vars := make([]string, len(completers))
19-
for index, c := range readCompleters() {
20-
vars[index] = fmt.Sprintf(` "%v",`, c)
20+
formattedNames := make([]string, 0)
21+
for _, name := range names {
22+
formattedNames = append(formattedNames, fmt.Sprintf("\t\"%v\",", name))
2123
}
2224

23-
cases := make([]string, len(completers)*2)
24-
for index, c := range readCompleters() {
25-
cases[index*2] = fmt.Sprintf(` case "%v":`, c)
26-
cases[(index*2)+1] = fmt.Sprintf(` %v.Execute()`, varName(c))
25+
maxlen := 0
26+
for _, name := range names {
27+
if l := len(name); l > maxlen {
28+
maxlen = l
29+
}
30+
}
31+
32+
formattedDescriptions := make([]string, 0)
33+
for _, name := range names {
34+
formattedDescriptions = append(formattedDescriptions, fmt.Sprintf(` %--`+strconv.Itoa(maxlen+4)+`v"%v",`, fmt.Sprintf(`"%v": `, name), descriptions[name]))
35+
}
36+
37+
cases := make([]string, 0)
38+
for _, name := range names {
39+
cases = append(cases,
40+
fmt.Sprintf(` case "%v":`, name),
41+
fmt.Sprintf(` %v.Execute()`, varName(name)),
42+
)
2743
}
2844

2945
content := fmt.Sprintf(`package cmd
@@ -34,13 +50,17 @@ import (
3450
var completers = []string{
3551
%v}
3652
53+
var descriptions = map[string]string{
54+
%v}
55+
3756
func executeCompleter(completer string) {
3857
switch completer {
3958
%v }
4059
}
4160
`,
4261
fmt.Sprintln(strings.Join(imports, "\n")),
43-
fmt.Sprintln(strings.Join(vars, "\n")),
62+
fmt.Sprintln(strings.Join(formattedNames, "\n")),
63+
fmt.Sprintln(strings.Join(formattedDescriptions, "\n")),
4464
fmt.Sprintln(strings.Join(cases, "\n")),
4565
)
4666
if root, err := rootDir(); err == nil {
@@ -55,18 +75,34 @@ func varName(name string) string {
5575
return strings.Replace(name, "-", "_", -1)
5676
}
5777

58-
func readCompleters() []string {
59-
completers := make([]string, 0)
78+
func readCompleters() ([]string, map[string]string) {
79+
names := make([]string, 0)
80+
descriptions := make(map[string]string)
6081
if root, err := rootDir(); err == nil {
6182
if files, err := ioutil.ReadDir(root + "/completers/"); err == nil {
6283
for _, file := range files {
6384
if file.IsDir() && strings.HasSuffix(file.Name(), "_completer") {
64-
completers = append(completers, strings.TrimSuffix(file.Name(), "_completer"))
85+
name := strings.TrimSuffix(file.Name(), "_completer")
86+
description := readDescription(root, file.Name())
87+
names = append(names, name)
88+
descriptions[name] = description
6589
}
6690
}
6791
}
6892
}
69-
return completers
93+
return names, descriptions
94+
}
95+
96+
func readDescription(root string, completer string) string {
97+
if content, err := ioutil.ReadFile(fmt.Sprintf("%v/completers/%v/cmd/root.go", root, completer)); err == nil {
98+
re := regexp.MustCompile("^\tShort: \"(?P<description>.*)\",$")
99+
for _, line := range strings.Split(string(content), "\n") {
100+
if re.MatchString(line) {
101+
return re.FindStringSubmatch(line)[1]
102+
}
103+
}
104+
}
105+
return ""
70106
}
71107

72108
func rootDir() (string, error) {

completers/adb_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "adb",
10-
Short: "",
10+
Short: "Android Debug Bridge",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

completers/alsamixer_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
var rootCmd = &cobra.Command{
1010
Use: "alsamixer",
11-
Short: "",
11+
Short: "soundcard mixer for ALSA soundcard driver, with ncurses interface",
1212
Run: func(cmd *cobra.Command, args []string) {},
1313
}
1414

completers/apropos_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "apropos",
10-
Short: "",
10+
Short: "search the manual page names and descriptions",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

completers/awk_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "awk",
10-
Short: "",
10+
Short: "pattern scanning and processing language",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

completers/basename_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "basename",
10-
Short: "",
10+
Short: "strip directory and suffix from filenames",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

completers/bash_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "bash",
10-
Short: "",
10+
Short: "GNU Bourne-Again SHell",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

completers/bat_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var rootCmd = &cobra.Command{
1111
Use: "bat",
12-
Short: "",
12+
Short: "a cat clone with syntax highlighting and Git integration",
1313
Run: func(cmd *cobra.Command, args []string) {},
1414
}
1515

completers/bats_completer/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var rootCmd = &cobra.Command{
99
Use: "bats",
10-
Short: "",
10+
Short: "Bash Automated Testing System",
1111
Run: func(cmd *cobra.Command, args []string) {},
1212
}
1313

0 commit comments

Comments
 (0)