Skip to content

Commit

Permalink
Merge pull request #642 from rsteube/group-style
Browse files Browse the repository at this point in the history
added style for command groups
  • Loading branch information
rsteube authored Dec 14, 2022
2 parents aaeebf4 + 6cdb2b9 commit d832d69
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 14 deletions.
1 change: 1 addition & 0 deletions example/cmd/_test/zsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions example/cmd/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion example/cmd/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {},
}

Expand Down
3 changes: 2 additions & 1 deletion example/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
)
}
50 changes: 50 additions & 0 deletions internal/common/group.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions internal/shell/zsh/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions internalActions.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()))
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/style/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down

0 comments on commit d832d69

Please sign in to comment.