Add option to show descriptions of completion items
diff --git a/completion.go b/completion.go
index 239609b..2f730d9 100644
--- a/completion.go
+++ b/completion.go
@@ -43,6 +43,8 @@
type completion struct {
parser *Parser
+
+ ShowDescriptions bool `short:"d" long:"show-descriptions" description:"Show descriptions next to completion items"`
}
// Filename is a string alias which provides filename completion.
@@ -262,8 +264,28 @@
func (c *completion) Execute(args []string) error {
ret := c.complete(args)
- for _, v := range ret {
- fmt.Println(v.Item)
+ if c.ShowDescriptions && len(ret) > 1 {
+ maxl := 0
+
+ for _, v := range ret {
+ if len(v.Item) > maxl {
+ maxl = len(v.Item)
+ }
+ }
+
+ for _, v := range ret {
+ fmt.Printf("%s", v.Item)
+
+ if len(v.Description) > 0 {
+ fmt.Printf("%s # %s", strings.Repeat(" ", maxl-len(v.Item)), v.Description)
+ }
+
+ fmt.Printf("\n")
+ }
+ } else {
+ for _, v := range ret {
+ fmt.Println(v.Item)
+ }
}
return nil