@@ -4,26 +4,42 @@ import (
44 "fmt"
55 "io/ioutil"
66 "os/exec"
7+ "regexp"
8+ "strconv"
79 "strings"
810)
911
1012func 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 (
3450var completers = []string{
3551%v}
3652
53+ var descriptions = map[string]string{
54+ %v}
55+
3756func 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 ("^\t Short: \" (?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
72108func rootDir () (string , error ) {
0 commit comments