Skip to content

Commit

Permalink
fix: setting width should wrap textinput and textarea bubbles
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Dec 14, 2023
1 parent 157eff5 commit 9ce73de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh/accessibility"
"github.com/charmbracelet/lipgloss"
)

// Input is a form input field.
Expand Down Expand Up @@ -258,6 +259,13 @@ func (i *Input) WithTheme(theme *Theme) Field {
// WithWidth sets the width of the input field.
func (i *Input) WithWidth(width int) Field {
i.width = width
frameSize := i.theme.Blurred.Base.GetHorizontalFrameSize()
promptWidth := lipgloss.Width(i.textinput.PromptStyle.Render(i.textinput.Prompt))
titleWidth := lipgloss.Width(i.theme.Focused.Title.Render(i.title))
i.textinput.Width = width - frameSize - promptWidth - 1
if i.inline {
i.textinput.Width -= titleWidth
}
return i
}

Expand Down
1 change: 1 addition & 0 deletions field_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (t *Text) WithAccessible(accessible bool) Field {
// WithWidth sets the width of the text field.
func (t *Text) WithWidth(width int) Field {
t.width = width
t.textarea.SetWidth(width - t.theme.Blurred.Base.GetHorizontalFrameSize())
return t
}

Expand Down
11 changes: 8 additions & 3 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ type Form struct {
keymap *KeyMap
}

const defaultWidth = 80

// NewForm returns a form with the given groups and default themes and
// keybindings.
//
Expand All @@ -75,7 +73,7 @@ func NewForm(groups ...*Group) *Form {
paginator: p,
theme: ThemeCharm(),
keymap: NewDefaultKeyMap(),
width: defaultWidth,
width: 0,
results: make(map[string]any),
}

Expand Down Expand Up @@ -324,6 +322,13 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
group := f.groups[page]

switch msg := msg.(type) {
case tea.WindowSizeMsg:
if f.width > 0 {
break
}
for _, group := range f.groups {
group.WithWidth(msg.Width)
}
case tea.KeyMsg:
switch {
case key.Matches(msg, f.keymap.Quit):
Expand Down

0 comments on commit 9ce73de

Please sign in to comment.