Skip to content

Commit

Permalink
style: added Highlight function
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 15, 2022
1 parent 3d37765 commit 583b133
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
19 changes: 2 additions & 17 deletions internal/common/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,9 @@ func (g Group) Style() string {
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]
if group.ID == g.Cmd.GroupID {
return style.Carapace.Highlight(index)
}
}
return style.Default
Expand Down
39 changes: 37 additions & 2 deletions pkg/style/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Register(name string, i interface{}) { config.RegisterStyle(name, i) }
// Set("carapace.Value", "bold magenta")
func Set(key, value string) error { return config.SetStyle(key, value) }

var Carapace = struct {
type carapace struct {
Value string `desc:"default style for values"`
Description string `desc:"default style for descriptions"`
Error string `desc:"default style for errors"`
Expand Down Expand Up @@ -56,7 +56,9 @@ var Carapace = struct {

H11 string `desc:"Highlight 11"`
H12 string `desc:"Highlight 12"`
}{
}

var Carapace = carapace{
Value: Default,
Description: Gray,
Error: Of(Bold, Red),
Expand Down Expand Up @@ -90,6 +92,39 @@ var Carapace = struct {
H12: Of(Bold, Dim),
}

// Highlight returns the style for given level (0..n)
func (c carapace) Highlight(level int) string {
switch level {
case 0:
return c.H1
case 1:
return c.H2
case 2:
return c.H3
case 3:
return c.H4
case 4:
return c.H5
case 5:
return c.H6
case 6:
return c.H7
case 7:
return c.H8
case 8:
return c.H9
case 9:
return c.H10
case 10:
return c.H11
case 11:
return c.H12
default:
return Default
}

}

func init() {
Register("carapace", &Carapace)
}

0 comments on commit 583b133

Please sign in to comment.