forked from charmbracelet/bubbles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(help): add styled whitespace with configurable background
Added WhitespaceStyle fields to both short and full help Styles struct, llowing customization of whitespace appearance between keys and descriptions. Previously whitespace was unstyled, now it matches the aesthetic of other elements and can be customized (e.g. with background colors for testing). - Added ShortWhitespace and FullWhitespace to Styles struct - Modified ShortHelpView to use styled whitespace between key and desc - Modified FullHelpView to use styled whitespace between key and desc - Added tests to verify whitespace styling behavior Fixes charmbracelet#571
- Loading branch information
1 parent
8624776
commit ee5636d
Showing
12 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package help | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/charmbracelet/bubbles/key" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/charmbracelet/x/exp/golden" | ||
) | ||
|
||
func TestWhitespaceStyle(t *testing.T) { | ||
m := New() | ||
m.FullSeparator = " | " | ||
|
||
// Set a distinctive background color for whitespace to make it visible in tests | ||
whitespaceBg := lipgloss.Color("#FF0000") | ||
m.Styles.ShortWhitespace = m.Styles.ShortWhitespace.Background(whitespaceBg) | ||
m.Styles.FullWhitespace = m.Styles.FullWhitespace.Background(whitespaceBg) | ||
|
||
// Standard keys setup | ||
k := key.WithKeys("x") | ||
kb := [][]key.Binding{ | ||
{ | ||
key.NewBinding(k, key.WithHelp("enter", "continue")), | ||
}, | ||
{ | ||
key.NewBinding(k, key.WithHelp("esc", "back")), | ||
key.NewBinding(k, key.WithHelp("?", "help")), | ||
}, | ||
{ | ||
key.NewBinding(k, key.WithHelp("H", "home")), | ||
key.NewBinding(k, key.WithHelp("ctrl+c", "quit")), | ||
key.NewBinding(k, key.WithHelp("ctrl+l", "log")), | ||
}, | ||
} | ||
|
||
// Test both views at different widths | ||
for _, w := range []int{20, 30, 40} { | ||
t.Run(fmt.Sprintf("full_help_width_%d", w), func(t *testing.T) { | ||
m.Width = w | ||
s := m.FullHelpView(kb) | ||
golden.RequireEqual(t, []byte(s)) | ||
}) | ||
|
||
t.Run(fmt.Sprintf("short_help_width_%d", w), func(t *testing.T) { | ||
m.Width = w | ||
// Flatten the bindings for short help | ||
var shortBindings []key.Binding | ||
for _, group := range kb { | ||
shortBindings = append(shortBindings, group...) | ||
} | ||
s := m.ShortHelpView(shortBindings) | ||
golden.RequireEqual(t, []byte(s)) | ||
}) | ||
} | ||
|
||
// Test with a disabled item and custom style | ||
for _, tc := range []struct { | ||
name string | ||
setupFn func() | ||
bindings [][]key.Binding | ||
}{ | ||
{ | ||
name: "disabled_item", | ||
setupFn: func() { | ||
m.Width = 40 | ||
}, | ||
bindings: [][]key.Binding{{ | ||
key.NewBinding(k, key.WithHelp("enter", "continue")), | ||
key.NewBinding(k, key.WithHelp("ctrl+c", "quit"), key.WithDisabled()), | ||
}}, | ||
}, | ||
{ | ||
name: "custom_style", | ||
setupFn: func() { | ||
m.Width = 40 | ||
customBg := lipgloss.Color("#00FF00") | ||
m.Styles.FullWhitespace = m.Styles.FullWhitespace.Background(customBg) | ||
m.Styles.ShortWhitespace = m.Styles.ShortWhitespace.Background(customBg) | ||
}, | ||
bindings: kb, | ||
}, | ||
} { | ||
t.Run(tc.name+"_full", func(t *testing.T) { | ||
tc.setupFn() | ||
s := m.FullHelpView(tc.bindings) | ||
golden.RequireEqual(t, []byte(s)) | ||
}) | ||
|
||
t.Run(tc.name+"_short", func(t *testing.T) { | ||
tc.setupFn() | ||
// Flatten the bindings for short help | ||
var shortBindings []key.Binding | ||
for _, group := range tc.bindings { | ||
shortBindings = append(shortBindings, group...) | ||
} | ||
s := m.ShortHelpView(shortBindings) | ||
golden.RequireEqual(t, []byte(s)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enter continue | esc back | H home | ||
? help ctrl+c quit | ||
ctrl+l log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue • esc back • ? help … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
enter continue | esc back … | ||
? help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enter continue | esc back | H home | ||
? help ctrl+c quit | ||
ctrl+l log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue • esc back … |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enter continue • esc back • ? help … |