commit fb47f06

delthas  ·  2025-05-30 13:53:55 +0000 UTC
parent 727ebfa
Fix very rarely inserting empty new lines or truncating lines

This could happen for messages containing a very long word with
exactly the width of the timeline.

Also kudos to taiite for making line.NewLines, no bugs found for
5 years... until now ;-)
2 files changed,  +4, -1
+1, -1
1@@ -132,7 +132,7 @@ func (l *Line) NewLines(vx *Vaxis, width int) []int {
2 			// Some word occupies the width of the terminal, lets place a
3 			// newline at the PREVIOUS split point (i-2, which is whitespace)
4 			// ONLY if there isn't already one.
5-			if 1 < i && 0 < len(l.newLines) && l.newLines[len(l.newLines)-1] != l.splitPoints[i-2].I {
6+			if 1 < i && (0 == len(l.newLines) || (l.newLines[len(l.newLines)-1] != l.splitPoints[i-2].I && l.newLines[len(l.newLines)-1] != l.splitPoints[i-1].I)) {
7 				l.newLines = append(l.newLines, l.splitPoints[i-2].I)
8 			}
9 			// and also place a newline after the word.
+3, -0
1@@ -141,4 +141,7 @@ func TestRenderedHeight(t *testing.T) {
2 	assertNewLines(t, "have a good day!", 17, 1) // |have a good day! |
3 
4 	assertNewLines(t, "cc en direct du word wrapping des familles le tests ça v a va va v a va", 46, 2)
5+
6+	assertNewLines(t, "take cares", 5, 2) // |take |cares|
7+	assertNewLines(t, "tak cares", 5, 2)  // |tak  |cares|
8 }