From 3ce3506dcb6c8e281bd84f1e091d14c2402439fc Mon Sep 17 00:00:00 2001 From: Florian Rey Date: Tue, 28 May 2024 10:19:14 +0200 Subject: [PATCH] fix: form and group styles --- form.go | 15 +++++++++++++-- group.go | 16 +++++++++++++++- theme.go | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/form.go b/form.go index 76e2788..7bdba6b 100644 --- a/form.go +++ b/form.go @@ -11,6 +11,7 @@ import ( "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/paginator" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" ) const defaultWidth = 80 @@ -65,6 +66,7 @@ type Form struct { // options width int height int + theme *Theme keymap *KeyMap timeout time.Duration teaOptions []tea.ProgramOption @@ -240,9 +242,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) } @@ -575,13 +578,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.layout.View(f) + return f.style().Render(f.layout.View(f)) } // Run runs the form. diff --git a/group.go b/group.go index d9ef2c0..29f5ba9 100644 --- a/group.go +++ b/group.go @@ -38,6 +38,7 @@ type Group struct { // group options width int height int + theme *Theme keymap *KeyMap hide func() bool active bool @@ -92,6 +93,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) @@ -265,6 +270,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) getContent() (int, string) { var fields strings.Builder offset := 0 @@ -320,8 +333,9 @@ func (g *Group) Footer() 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()) } diff --git a/theme.go b/theme.go index 32982a1..7db054a 100644 --- a/theme.go +++ b/theme.go @@ -74,6 +74,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().