From 49935b4e33eaef65cbdb887c9f33846ada31882c 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 8173c7c..e23995f 100644 --- a/form.go +++ b/form.go @@ -2,6 +2,7 @@ package huh import ( "errors" + "github.com/charmbracelet/lipgloss" "io" "os" @@ -60,6 +61,7 @@ type Form struct { // options width int height int + theme *Theme keymap *KeyMap teaOptions []tea.ProgramOption } @@ -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) } @@ -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. diff --git a/group.go b/group.go index ee97fa3..e90f8f2 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 } @@ -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) @@ -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 @@ -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()) } diff --git a/theme.go b/theme.go index 7541a07..d300f22 100644 --- a/theme.go +++ b/theme.go @@ -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().