Skip to content

Commit 538d39c

Browse files
committed
refactor(help): use setter/getter for help width
1 parent 84a82df commit 538d39c

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

help/help.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func DefaultLightStyles() Styles {
7575

7676
// Model contains the state of the help view.
7777
type Model struct {
78-
Width int
7978
ShowAll bool // if true, render the "full" help menu
8079

8180
ShortSeparator string
@@ -86,6 +85,8 @@ type Model struct {
8685
Ellipsis string
8786

8887
Styles Styles
88+
89+
width int
8990
}
9091

9192
// New creates a new help view with some useful defaults.
@@ -111,6 +112,16 @@ func (m Model) View(k KeyMap) string {
111112
return m.ShortHelpView(k.ShortHelp())
112113
}
113114

115+
// SetWidth sets the maximum width for the help view.
116+
func (m *Model) SetWidth(w int) {
117+
m.width = w
118+
}
119+
120+
// Width returns the maximum width for the help view.
121+
func (m Model) Width() int {
122+
return m.width
123+
}
124+
114125
// ShortHelpView renders a single line help view from a slice of keybindings.
115126
// If the line is longer than the maximum width it will be gracefully
116127
// truncated, showing only as many help items as possible.
@@ -223,10 +234,10 @@ func (m Model) FullHelpView(groups [][]key.Binding) string {
223234

224235
func (m Model) shouldAddItem(totalWidth, width int) (tail string, ok bool) {
225236
// If there's room for an ellipsis, print that.
226-
if m.Width > 0 && totalWidth+width > m.Width {
237+
if m.width > 0 && totalWidth+width > m.width {
227238
tail = " " + m.Styles.Ellipsis.Inline(true).Render(m.Ellipsis)
228239

229-
if totalWidth+lipgloss.Width(tail) < m.Width {
240+
if totalWidth+lipgloss.Width(tail) < m.width {
230241
return tail, false
231242
}
232243
}

help/help_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestFullHelp(t *testing.T) {
3030

3131
for _, w := range []int{20, 30, 40} {
3232
t.Run(fmt.Sprintf("full help %d width", w), func(t *testing.T) {
33-
m.Width = w
33+
m.SetWidth(w)
3434
s := m.FullHelpView(kb)
3535
s = ansi.Strip(s)
3636
golden.RequireEqual(t, []byte(s))

list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ func (m *Model) SetSize(width, height int) {
697697

698698
m.width = width
699699
m.height = height
700-
m.Help.Width = width
700+
m.Help.SetWidth(width)
701701
m.FilterInput.SetWidth(width - promptWidth - lipgloss.Width(m.spinnerView()))
702702
m.updatePagination()
703703
m.updateKeybindings()

0 commit comments

Comments
 (0)