summaryrefslogtreecommitdiff
path: root/internal/tui/form.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/tui/form.go')
-rw-r--r--internal/tui/form.go42
1 files changed, 30 insertions, 12 deletions
diff --git a/internal/tui/form.go b/internal/tui/form.go
index b0ac18d..7f49659 100644
--- a/internal/tui/form.go
+++ b/internal/tui/form.go
@@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
+ "github.com/charmbracelet/x/ansi"
)
type suggestionType int
@@ -245,10 +246,35 @@ func (f Form) Update(msg tea.Msg) (Form, tea.Cmd) {
}
func (f Form) View() string {
+ renderedFields := make([]string, len(f.fields))
+ maxFieldWidth := 0
+
+ for i, field := range f.fields {
+ style := f.UnselectedStyle
+ if i == f.selIdx {
+ style = f.SelectedStyle
+ }
+
+ var fieldView string
+ if style != nil {
+ fieldView = style.Render(field.View())
+ } else {
+ fieldView = field.View()
+ }
+ renderedFields[i] = fieldView
+
+ fieldWidth := lipgloss.Width(fieldView)
+ if fieldWidth > maxFieldWidth {
+ maxFieldWidth = fieldWidth
+ }
+ }
+ constraintWidth := max(72, maxFieldWidth)
+
content := ""
if f.err != nil {
- content += errorStyle.Render(f.err.Error()) + "\n\n"
+ wrappedError := ansi.WordwrapWc(f.err.Error(), constraintWidth, " ")
+ content += errorStyle.Render(wrappedError) + "\n\n"
}
for i, field := range f.fields {
@@ -256,19 +282,11 @@ func (f Form) View() string {
content += "\n\n"
}
content += field.label + ":\n"
-
- style := f.UnselectedStyle
- if i == f.selIdx {
- style = f.SelectedStyle
- }
- if style != nil {
- content += style.Render(field.View())
- } else {
- content += field.View()
- }
+ content += renderedFields[i]
if field.Err != nil {
- content += "\n" + errorStyle.Render(field.Err.Error())
+ wrappedError := ansi.WordwrapWc(field.Err.Error(), constraintWidth, " ")
+ content += "\n" + errorStyle.Render(wrappedError)
}
}
return content