mirror of
https://github.com/charmbracelet/bubbles.git
synced 2026-07-22 11:39:28 -06:00
refactor(help): use setter/getter for help width
This commit is contained in:
parent
84a82dfeee
commit
538d39c727
17
help/help.go
17
help/help.go
@ -75,7 +75,6 @@ func DefaultLightStyles() Styles {
|
||||
|
||||
// Model contains the state of the help view.
|
||||
type Model struct {
|
||||
Width int
|
||||
ShowAll bool // if true, render the "full" help menu
|
||||
|
||||
ShortSeparator string
|
||||
@ -86,6 +85,8 @@ type Model struct {
|
||||
Ellipsis string
|
||||
|
||||
Styles Styles
|
||||
|
||||
width int
|
||||
}
|
||||
|
||||
// New creates a new help view with some useful defaults.
|
||||
@ -111,6 +112,16 @@ func (m Model) View(k KeyMap) string {
|
||||
return m.ShortHelpView(k.ShortHelp())
|
||||
}
|
||||
|
||||
// SetWidth sets the maximum width for the help view.
|
||||
func (m *Model) SetWidth(w int) {
|
||||
m.width = w
|
||||
}
|
||||
|
||||
// Width returns the maximum width for the help view.
|
||||
func (m Model) Width() int {
|
||||
return m.width
|
||||
}
|
||||
|
||||
// ShortHelpView renders a single line help view from a slice of keybindings.
|
||||
// If the line is longer than the maximum width it will be gracefully
|
||||
// truncated, showing only as many help items as possible.
|
||||
@ -223,10 +234,10 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
|
||||
|
||||
func (m Model) shouldAddItem(totalWidth, width int) (tail string, ok bool) {
|
||||
// If there's room for an ellipsis, print that.
|
||||
if m.Width > 0 && totalWidth+width > m.Width {
|
||||
if m.width > 0 && totalWidth+width > m.width {
|
||||
tail = " " + m.Styles.Ellipsis.Inline(true).Render(m.Ellipsis)
|
||||
|
||||
if totalWidth+lipgloss.Width(tail) < m.Width {
|
||||
if totalWidth+lipgloss.Width(tail) < m.width {
|
||||
return tail, false
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ func TestFullHelp(t *testing.T) {
|
||||
|
||||
for _, w := range []int{20, 30, 40} {
|
||||
t.Run(fmt.Sprintf("full help %d width", w), func(t *testing.T) {
|
||||
m.Width = w
|
||||
m.SetWidth(w)
|
||||
s := m.FullHelpView(kb)
|
||||
s = ansi.Strip(s)
|
||||
golden.RequireEqual(t, []byte(s))
|
||||
|
||||
@ -697,7 +697,7 @@ func (m *Model) SetSize(width, height int) {
|
||||
|
||||
m.width = width
|
||||
m.height = height
|
||||
m.Help.Width = width
|
||||
m.Help.SetWidth(width)
|
||||
m.FilterInput.SetWidth(width - promptWidth - lipgloss.Width(m.spinnerView()))
|
||||
m.updatePagination()
|
||||
m.updateKeybindings()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user