Repository: rsteube/carapace
Pull request #642 (2022-12-14 15:48:14)

added style for command groups


Patch:
diff --git a/example/cmd/injection.go b/example/cmd/injection.go
index 150125f..04ae4b6 100644
--- a/example/cmd/injection.go
+++ b/example/cmd/injection.go
@@ -6,9 +6,10 @@ import (
 )
 
 var injectionCmd = &cobra.Command{
-	Use:   "injection",
-	Short: "just trying to break things",
-	Run:   func(cmd *cobra.Command, args []string) {},
+	Use:     "injection",
+	Short:   "just trying to break things",
+	GroupID: "test",
+	Run:     func(cmd *cobra.Command, args []string) {},
 }
 
 func init() {
diff --git a/example/cmd/modifier.go b/example/cmd/modifier.go
index 6c05dee..5325955 100644
--- a/example/cmd/modifier.go
+++ b/example/cmd/modifier.go
@@ -8,7 +8,7 @@ import (
 var modifierCmd = &cobra.Command{
 	Use:     "modifier [pos1]",
 	Short:   "modifier example",
-	GroupID: "main",
+	GroupID: "modifier",
 	Run:     func(cmd *cobra.Command, args []string) {},
 }
 
diff --git a/example/cmd/root.go b/example/cmd/root.go
index d850b37..b0ce9bc 100644
--- a/example/cmd/root.go
+++ b/example/cmd/root.go
@@ -31,6 +31,7 @@ func init() {
 
 	rootCmd.AddGroup(
 		&cobra.Group{ID: "main", Title: "Main Commands"},
-		&cobra.Group{ID: "other", Title: "Other Commands"},
+		&cobra.Group{ID: "modifier", Title: "Modifier Commands"},
+		&cobra.Group{ID: "test", Title: "Test Commands"},
 	)
 }
diff --git a/internal/common/group.go b/internal/common/group.go
new file mode 100644
index 0000000..bcf9613
--- /dev/null
+++ b/internal/common/group.go
@@ -0,0 +1,50 @@
+package common
+
+import (
+	"fmt"
+
+	"github.com/rsteube/carapace/pkg/style"
+	"github.com/spf13/cobra"
+)
+
+type Group struct {
+	Cmd *cobra.Command
+}
+
+func (g Group) Tag() string {
+	tag := "commands"
+	if id := g.Cmd.GroupID; id != "" {
+		tag = fmt.Sprintf("%v %v", id, tag)
+	} else if len(g.Cmd.Parent().Groups()) != 0 {
+		tag = "other commands"
+	}
+	return tag
+}
+
+func (g Group) Style() string {
+	if g.Cmd.Parent() == nil || g.Cmd.Parent().Groups() == nil {
+		return style.Default
+	}
+
+	groupStyles := []string{
+		style.Carapace.H1,
+		style.Carapace.H2,
+		style.Carapace.H3,
+		style.Carapace.H4,
+		style.Carapace.H5,
+		style.Carapace.H6,
+		style.Carapace.H7,
+		style.Carapace.H8,
+		style.Carapace.H9,
+		style.Carapace.H10,
+		style.Carapace.H11,
+		style.Carapace.H12,
+	}
+
+	for index, group := range g.Cmd.Parent().Groups() {
+		if group.ID == g.Cmd.GroupID && index < len(groupStyles) {
+			return groupStyles[index]
+		}
+	}
+	return style.Default
+}
diff --git a/internal/shell/zsh/snippet.go b/internal/shell/zsh/snippet.go
index 0a429c4..27611e3 100644
--- a/internal/shell/zsh/snippet.go
+++ b/internal/shell/zsh/snippet.go
@@ -27,6 +27,7 @@ function _%v_completion {
   IFS=$'\001' read -r -d '' zstyle message data <<<"${lines}"
   # shellcheck disable=SC2154
   zstyle ":completion:${curcontext}:*" list-colors "${zstyle}"
+  zstyle ":completion:${curcontext}:*" group-name ''
   [ -z "$message" ] || _message -r "${message}"
   
   local block tag displays values displaysArr valuesArr
diff --git a/internalActions.go b/internalActions.go
index 1d5b03b..37e4bdf 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -1,13 +1,13 @@
 package carapace
 
 import (
-	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
 	"strings"
 
+	"github.com/rsteube/carapace/internal/common"
 	"github.com/rsteube/carapace/internal/pflagfork"
 	"github.com/rsteube/carapace/pkg/style"
 	"github.com/spf13/cobra"
@@ -128,15 +128,10 @@ func actionSubcommands(cmd *cobra.Command) Action {
 		batch := Batch()
 		for _, subcommand := range cmd.Commands() {
 			if !subcommand.Hidden && subcommand.Deprecated == "" {
-				tag := "commands"
-				if id := subcommand.GroupID; id != "" {
-					tag = fmt.Sprintf("%v %v", id, tag)
-				} else if len(cmd.Groups()) != 0 {
-					tag = "other commands"
-				}
-				batch = append(batch, ActionValuesDescribed(subcommand.Name(), subcommand.Short).Tag(tag))
+				group := common.Group{Cmd: subcommand}
+				batch = append(batch, ActionStyledValuesDescribed(subcommand.Name(), subcommand.Short, group.Style()).Tag(group.Tag()))
 				for _, alias := range subcommand.Aliases {
-					batch = append(batch, ActionValuesDescribed(alias, subcommand.Short).Tag(tag))
+					batch = append(batch, ActionStyledValuesDescribed(alias, subcommand.Short, group.Style()).Tag(group.Tag()))
 				}
 			}
 		}
diff --git a/pkg/style/config.go b/pkg/style/config.go
index 7304b82..083228d 100644
--- a/pkg/style/config.go
+++ b/pkg/style/config.go
@@ -41,6 +41,20 @@ var Carapace = struct {
 	LogLevelError    string `desc:"LogLevel ERROR"`
 	LogLevelCritical string `desc:"LogLevel CRITICAL"`
 	LogLevelFatal    string `desc:"LogLevel FATAL"`
+
+	H1 string `desc:"Highlight 1"`
+	H2 string `desc:"Highlight 2"`
+	H3 string `desc:"Highlight 3"`
+	H4 string `desc:"Highlight 4"`
+	H5 string `desc:"Highlight 5"`
+	H6 string `desc:"Highlight 6"`
+
+	H7  string `desc:"Highlight 7"`
+	H8  string `desc:"Highlight 8"`
+	H9  string `desc:"Highlight 9"`
+	H10 string `desc:"Highlight 10"`
+	H11 string `desc:"Highlight 11"`
+	H12 string `desc:"Highlight 12"`
 }{
 	Value:       Default,
 	Description: Gray,
@@ -58,6 +72,20 @@ var Carapace = struct {
 	LogLevelError:    Magenta,
 	LogLevelCritical: Red,
 	LogLevelFatal:    Cyan,
+
+	H1: Blue,
+	H2: Yellow,
+	H3: Magenta,
+	H4: Cyan,
+	H5: Green,
+	H6: Bold,
+
+	H7:  Of(Blue, Dim),
+	H8:  Of(Yellow, Dim),
+	H9:  Of(Magenta, Dim),
+	H10: Of(Cyan, Dim),
+	H11: Of(Green, Dim),
+	H12: Of(Bold, Dim),
 }
 
 func init() {


Test patch:
diff --git a/example/cmd/_test/zsh.sh b/example/cmd/_test/zsh.sh
index 9108b25..f3e3375 100644
--- a/example/cmd/_test/zsh.sh
+++ b/example/cmd/_test/zsh.sh
@@ -15,6 +15,7 @@ function _example_completion {
   IFS=$'\001' read -r -d '' zstyle message data <<<"${lines}"
   # shellcheck disable=SC2154
   zstyle ":completion:${curcontext}:*" list-colors "${zstyle}"
+  zstyle ":completion:${curcontext}:*" group-name ''
   [ -z "$message" ] || _message -r "${message}"
   
   local block tag displays values displaysArr valuesArr