Skip to content

Commit

Permalink
fix: form and group styles
Browse files Browse the repository at this point in the history
  • Loading branch information
nervo committed May 28, 2024
1 parent 3b08395 commit e845022
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
15 changes: 13 additions & 2 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

const defaultWidth = 80
Expand Down Expand Up @@ -60,6 +61,7 @@ type Form struct {
// options
width int
height int
theme *Theme
keymap *KeyMap
teaOptions []tea.ProgramOption
}
Expand Down Expand Up @@ -231,9 +233,10 @@ func (f *Form) WithShowErrors(v bool) *Form {
// can be applied to each group and field individually for more granular
// control.
func (f *Form) WithTheme(theme *Theme) *Form {
if theme == nil {
if f.theme != nil {
return f
}
f.theme = theme
for _, group := range f.groups {
group.WithTheme(theme)
}
Expand Down Expand Up @@ -539,13 +542,21 @@ func (f *Form) isGroupHidden(page int) bool {
return hide()
}

func (f *Form) style() lipgloss.Style {
theme := f.theme
if theme == nil {
theme = ThemeCharm()
}
return theme.Form
}

// View renders the form.
func (f *Form) View() string {
if f.quitting {
return ""
}

return f.groups[f.paginator.Page].View()
return f.style().Render(f.groups[f.paginator.Page].View())
}

// Run runs the form.
Expand Down
16 changes: 15 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Group struct {
// group options
width int
height int
theme *Theme
keymap *KeyMap
hide func() bool
}
Expand Down Expand Up @@ -90,6 +91,10 @@ func (g *Group) WithShowErrors(show bool) *Group {

// WithTheme sets the theme on a group.
func (g *Group) WithTheme(t *Theme) *Group {
if g.theme != nil {
return g
}
g.theme = t
g.help.Styles = t.Help
for _, field := range g.fields {
field.WithTheme(t)
Expand Down Expand Up @@ -261,6 +266,14 @@ func (g *Group) fullHeight() int {
return height
}

func (g *Group) style() lipgloss.Style {
theme := g.theme
if theme == nil {
theme = ThemeCharm()
}
return theme.Group
}

func (g *Group) buildView() {
var fields strings.Builder
offset := 0
Expand Down Expand Up @@ -297,8 +310,9 @@ func (g *Group) View() string {
}
if g.showErrors {
for _, err := range errors {
// !!! Non
view.WriteString(ThemeCharm().Focused.ErrorMessage.Render(err.Error()))
}
}
return view.String()
return g.style().Render(view.String())
}
2 changes: 2 additions & 0 deletions theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const (
func ThemeBase() *Theme {
var t Theme

t.Form = lipgloss.NewStyle()
t.Group = lipgloss.NewStyle()
t.FieldSeparator = lipgloss.NewStyle().SetString("\n\n")

button := lipgloss.NewStyle().
Expand Down

0 comments on commit e845022

Please sign in to comment.