Skip to content

Commit

Permalink
refactor!: use the new bubbletea stringer api
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 23, 2025
1 parent 23f19ab commit b41c21c
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 83 deletions.
8 changes: 6 additions & 2 deletions field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (c *Confirm) activeStyles() *FieldStyles {
}

// View renders the confirm field.
func (c *Confirm) View() string {
func (c *Confirm) View() fmt.Stringer {
styles := c.activeStyles()

var sb strings.Builder
Expand Down Expand Up @@ -284,7 +284,11 @@ func (c *Confirm) View() string {
style := lipgloss.NewStyle().Width(renderWidth).Align(lipgloss.Center)

sb.WriteString(style.Render(buttonsRow))
return styles.Base.Render(sb.String())

var s strings.Builder
s.WriteString(styles.Base.Render(sb.String()))

return &s
}

// Run runs the confirm field in accessible mode.
Expand Down
8 changes: 6 additions & 2 deletions field_filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (f *FilePicker) activeStyles() *FieldStyles {
}

// View renders the file field.
func (f *FilePicker) View() string {
func (f *FilePicker) View() fmt.Stringer {
styles := f.activeStyles()

var sb strings.Builder
Expand All @@ -282,7 +282,11 @@ func (f *FilePicker) View() string {
sb.WriteString(styles.TextInput.Placeholder.Render("No file selected."))
}
}
return styles.Base.Render(sb.String())

var s strings.Builder
s.WriteString(styles.Base.Render(sb.String()))

return &s
}

func (f *FilePicker) setPicking(v bool) {
Expand Down
7 changes: 5 additions & 2 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (i *Input) activeStyles() *FieldStyles {
}

// View renders the input field.
func (i *Input) View() string {
func (i *Input) View() fmt.Stringer {
styles := i.activeStyles()

// NB: since the method is on a pointer receiver these are being mutated.
Expand Down Expand Up @@ -412,7 +412,10 @@ func (i *Input) View() string {
}
sb.WriteString(i.textinput.View())

return styles.Base.Render(sb.String())
var s strings.Builder
s.WriteString(styles.Base.Render(sb.String()))

return &s
}

// Run runs the input field in accessible mode.
Expand Down
8 changes: 6 additions & 2 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (m *MultiSelect[T]) optionsView() string {
}

// View renders the multi-select field.
func (m *MultiSelect[T]) View() string {
func (m *MultiSelect[T]) View() fmt.Stringer {
styles := m.activeStyles()

m.viewport.SetContent(m.optionsView())
Expand All @@ -593,7 +593,11 @@ func (m *MultiSelect[T]) View() string {
sb.WriteString(m.descriptionView() + "\n")
}
sb.WriteString(m.viewport.View())
return styles.Base.Render(sb.String())

var s strings.Builder
s.WriteString(styles.Base.Render(sb.String()))

return &s
}

func (m *MultiSelect[T]) printOptions() {
Expand Down
8 changes: 6 additions & 2 deletions field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (n *Note) activeStyles() *FieldStyles {
}

// View renders the note field.
func (n *Note) View() string {
func (n *Note) View() fmt.Stringer {
styles := n.activeStyles()
sb := strings.Builder{}

Expand All @@ -232,7 +232,11 @@ func (n *Note) View() string {
if n.showNextButton {
sb.WriteString(styles.Next.Render(n.nextLabel))
}
return styles.Card.Height(n.height).Render(sb.String())

var s strings.Builder
s.WriteString(styles.Card.Height(n.height).Render(sb.String()))

return &s
}

// Run runs the note field.
Expand Down
8 changes: 6 additions & 2 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (s *Select[T]) optionsView() string {
}

// View renders the select field.
func (s *Select[T]) View() string {
func (s *Select[T]) View() fmt.Stringer {
styles := s.activeStyles()
s.viewport.SetContent(s.optionsView())

Expand All @@ -610,7 +610,11 @@ func (s *Select[T]) View() string {
}
}
sb.WriteString(s.viewport.View())
return styles.Base.Render(sb.String())

var v strings.Builder
v.WriteString(styles.Base.Render(sb.String()))

return &v
}

// clearFilter clears the value of the filter.
Expand Down
7 changes: 5 additions & 2 deletions field_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (t *Text) activeTextAreaStyles() *textarea.StyleState {
}

// View renders the text field.
func (t *Text) View() string {
func (t *Text) View() fmt.Stringer {
styles := t.activeStyles()
textareaStyles := t.activeTextAreaStyles()

Expand All @@ -392,7 +392,10 @@ func (t *Text) View() string {
}
sb.WriteString(t.textarea.View())

return styles.Base.Render(sb.String())
var v strings.Builder
v.WriteString(styles.Base.Render(sb.String()))

return &v
}

// Run runs the text field.
Expand Down
11 changes: 7 additions & 4 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -132,7 +133,7 @@ type Field interface {
// Bubble Tea Model
Init() (tea.Model, tea.Cmd)
Update(tea.Msg) (tea.Model, tea.Cmd)
View() string
View() fmt.Stringer

// Bubble Tea Events
Blur() tea.Cmd
Expand Down Expand Up @@ -608,12 +609,14 @@ func (f *Form) isGroupHidden(group *Group) bool {
}

// View renders the form.
func (f *Form) View() string {
func (f *Form) View() fmt.Stringer {
var s strings.Builder
if f.quitting {
return ""
return &s
}

return f.layout.View(f)
s.WriteString(f.layout.View(f).String())
return &s
}

// Run runs the form.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/catppuccin/go v0.2.0
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250115152509-0c83e6f4c8d3
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250115153415-046a5ddf5b4a
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250123211610-443afa6fa0c1
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20250114171829-b67eb015d607
github.com/charmbracelet/x/ansi v0.7.0
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250115152509-0c83e6f4c8d3
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250115152509-0c83e6f4c8d3/go.mod h1:QbWYWg5Y3UBTNDoTIPIgLHiGsTbBBO2D6q53lG7IR8M=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250115153415-046a5ddf5b4a h1:Wct67LI3+EoDcAs3ujwWhTp8NNZUlkBAeOLgjyc2rOE=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250115153415-046a5ddf5b4a/go.mod h1:hT2875Ank3ylgW13kqu6cjDc9XIk9sE5JsOFYdl09b8=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250123211610-443afa6fa0c1 h1:tktnM4YimEWSYd58iZlPDB3Xz25/r94VYZZsHK5zWL0=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250123211610-443afa6fa0c1/go.mod h1:hT2875Ank3ylgW13kqu6cjDc9XIk9sE5JsOFYdl09b8=
github.com/charmbracelet/colorprofile v0.1.9 h1:5JnfvX+I9D6rRNu8xK3pgIqknaBVTXHU9pGu1jkZxLw=
github.com/charmbracelet/colorprofile v0.1.9/go.mod h1:+jpmObxZl1Dab3H3IMVIPSZTsKcFpjJUv97G0dLqM60=
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20250114171829-b67eb015d607 h1:lERE4ow371r5WMqQAt7Eqlg1A4tBNA8T4RLwdXnKyBo=
Expand Down
15 changes: 8 additions & 7 deletions group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package huh

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/v2/help"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (g *Group) WithHeight(height int) *Group {
g.viewport.SetHeight(height)
g.selector.Range(func(_ int, field Field) bool {
// A field height must not exceed the form height.
if height-1 <= lipgloss.Height(field.View()) {
if height-1 <= lipgloss.Height(field.View().String()) {
field.WithHeight(height)
}
return true
Expand Down Expand Up @@ -287,7 +288,7 @@ func (g *Group) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (g *Group) fullHeight() int {
height := g.selector.Total()
g.selector.Range(func(_ int, field Field) bool {
height += lipgloss.Height(field.View())
height += lipgloss.Height(field.View().String())
return true
})
return height
Expand All @@ -301,12 +302,12 @@ func (g *Group) getContent() (int, string) {
// if the focused field is requesting it be zoomed, only show that field.
if g.selector.Selected().Zoom() {
g.selector.Selected().WithHeight(g.height - 1)
fields.WriteString(g.selector.Selected().View())
fields.WriteString(g.selector.Selected().View().String())
} else {
g.selector.Range(func(i int, field Field) bool {
fields.WriteString(field.View())
fields.WriteString(field.View().String())
if i == g.selector.Index() {
offset = lipgloss.Height(fields.String()) - lipgloss.Height(field.View())
offset = lipgloss.Height(fields.String()) - lipgloss.Height(field.View().String())
}
if i < g.selector.Total()-1 {
fields.WriteString(gap)
Expand All @@ -326,11 +327,11 @@ func (g *Group) buildView() {
}

// View renders the group.
func (g *Group) View() string {
func (g *Group) View() fmt.Stringer {
var view strings.Builder
view.WriteString(g.viewport.View())
view.WriteString(g.Footer())
return view.String()
return &view
}

// Content renders the group's content only (no footer).
Expand Down
Loading

0 comments on commit b41c21c

Please sign in to comment.