diff --git a/internal/ui/body/body.go b/internal/ui/body/body.go index b320327..6bf62ee 100644 --- a/internal/ui/body/body.go +++ b/internal/ui/body/body.go @@ -29,6 +29,7 @@ const ( func New(state *commit.State, h int) Model { m := Model{ Height: h, + Width: defaultWidth, state: state, styles: defaultStyles(state.Theme), textArea: newTextArea(state.Placeholders.Body, defaultWidth, state), @@ -74,6 +75,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.textArea.SetHeight(m.Height) + m.textArea.SetWidth(m.Width) switch { case m.focus && !m.textArea.Focused(): @@ -110,9 +112,19 @@ func (m Model) Focused() bool { } func (m Model) Value() string { - txt := wordwrap.String(m.textArea.Value(), 73) + w := wordwrap.WordWrap{ + // Further details for the text reflow issue: + // https://github.com/charmbracelet/bubbles/issues/333 + Limit: m.Width - 1, + Breakpoints: []rune{'-'}, + Newline: []rune{'\n'}, + KeepNewlines: true, + } + + w.Write([]byte(m.textArea.Value())) + w.Close() - return strings.TrimSpace(txt) + return strings.TrimSpace(w.String()) } func (m Model) RawValue() string { diff --git a/internal/ui/body/body_test.go b/internal/ui/body/body_test.go index 0334dff..f99520a 100644 --- a/internal/ui/body/body_test.go +++ b/internal/ui/body/body_test.go @@ -150,6 +150,45 @@ func TestModel(t *testing.T) { }, }, }, + { + name: "reflow_single_world", + args: args{ + model: func(m body.Model) body.Model { + m.Height = 3 + m.Width = 10 + + m.SetValue("1234567890") + m, _ = body.ToModel(m.Update(m)) + return m + }, + }, + }, + { + name: "reflow_multiple_words", + args: args{ + model: func(m body.Model) body.Model { + m.Height = 3 + m.Width = 10 + + m.SetValue("1 2 3 4 56") + m, _ = body.ToModel(m.Update(m)) + return m + }, + }, + }, + { + name: "reflow_combination", + args: args{ + model: func(m body.Model) body.Model { + m.Height = 3 + m.Width = 10 + + m.SetValue("12345678901 2 3 4 56") + m, _ = body.ToModel(m.Update(m)) + return m + }, + }, + }, } for _, tt := range tests { diff --git a/internal/ui/body/testdata/reflow_combination.golden b/internal/ui/body/testdata/reflow_combination.golden new file mode 100644 index 0000000..ce19686 --- /dev/null +++ b/internal/ui/body/testdata/reflow_combination.golden @@ -0,0 +1,6 @@ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │ 1234567890 │ + │ 1 2 3 4 │ + │ 56 │ + └──────────────────────────────────────────────────────────────────────────┘ diff --git a/internal/ui/body/testdata/reflow_multiple_words.golden b/internal/ui/body/testdata/reflow_multiple_words.golden new file mode 100644 index 0000000..17d5c86 --- /dev/null +++ b/internal/ui/body/testdata/reflow_multiple_words.golden @@ -0,0 +1,6 @@ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │ 1 2 3 4 │ + │ 56 │ + │ │ + └──────────────────────────────────────────────────────────────────────────┘ diff --git a/internal/ui/body/testdata/reflow_single_world.golden b/internal/ui/body/testdata/reflow_single_world.golden new file mode 100644 index 0000000..644b2dc --- /dev/null +++ b/internal/ui/body/testdata/reflow_single_world.golden @@ -0,0 +1,6 @@ + + ┌──────────────────────────────────────────────────────────────────────────┐ + │ 1234567890 │ + │ │ + │ │ + └──────────────────────────────────────────────────────────────────────────┘