Skip to content

Commit

Permalink
🐛 Fix reflow occuring unexpectedly in body
Browse files Browse the repository at this point in the history
Reflow when entering text in the body was inconsistent. The following
test case shows the issue (has padding of 1 on both sides):

+------------+
| 1234567890 |
| 1 2 3 4 5  |
| 6          |
+------------+

The 10th character on the second line should be 6 however as there is a
space it is immediately reflowed to the next line. The cursor never being
allowed in the 11th character position creates this edge case.

This bug has further details in the GitHub issue:
charmbracelet/bubbles#333

The textarea width is set to 72 characters however any line with a space
contained in it will reflow at 71 characters. To workaround this issue
reflow has been set to 71 characters.

Test cases have been added to detect this problem.
  • Loading branch information
mikelorant committed Feb 19, 2023
1 parent 8efac8f commit 3e57285
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/ui/body/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions internal/ui/body/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/ui/body/testdata/reflow_combination.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

┌──────────────────────────────────────────────────────────────────────────┐
│ 1234567890 │
│ 1 2 3 4 │
│ 56 │
└──────────────────────────────────────────────────────────────────────────┘
6 changes: 6 additions & 0 deletions internal/ui/body/testdata/reflow_multiple_words.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

┌──────────────────────────────────────────────────────────────────────────┐
│ 1 2 3 4 │
│ 56 │
│ │
└──────────────────────────────────────────────────────────────────────────┘
6 changes: 6 additions & 0 deletions internal/ui/body/testdata/reflow_single_world.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

┌──────────────────────────────────────────────────────────────────────────┐
│ 1234567890 │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────┘

0 comments on commit 3e57285

Please sign in to comment.