Skip to content

Commit

Permalink
Improved row clearing to avoid the first raw row staying
Browse files Browse the repository at this point in the history
  • Loading branch information
baalimago committed Aug 1, 2024
1 parent c7fa64c commit a14675b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
24 changes: 14 additions & 10 deletions internal/utils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ func countNewLines(msg string, termWidth int) int {
// UpdateMessageTerminalMetadata updates the terminal metadata. Meaning the lineCount, to eventually
// clear the terminal
func UpdateMessageTerminalMetadata(msg string, line *string, lineCount *int, termWidth int) {
msgSplit := strings.Split(*line+msg, "\n")
newlineSplit := strings.Split(*line+msg, "\n")
amNewlines := 0
for _, line := range msgSplit {
for _, line := range newlineSplit {
amNewlines += countNewLines(line, termWidth)
}
if amNewlines == 0 {
amNewlines = 1

amNewlineChars := len(newlineSplit)
if amNewlineChars == 1 {
amNewlineChars = 0
}
amNewlines += len(msgSplit) - 1

*lineCount += amNewlines
lastToken := msgSplit[len(msgSplit)-1]
if len(lastToken) > termWidth {
lastTokenWords := strings.Split(lastToken, " ")
*lineCount += amNewlines + amNewlineChars
if *lineCount == 0 {
*lineCount = 1
}
lastNewline := newlineSplit[len(newlineSplit)-1]
if len(lastNewline) > termWidth {
lastTokenWords := strings.Split(lastNewline, " ")
lastWord := lastTokenWords[len(lastTokenWords)-1]
if len(lastWord) > termWidth {
trimmedWord := lastWord[termWidth:]
Expand All @@ -56,7 +60,7 @@ func UpdateMessageTerminalMetadata(msg string, line *string, lineCount *int, ter
*line = lastWord
}
} else {
*line = lastToken
*line = lastNewline
}
}

Expand Down
93 changes: 88 additions & 5 deletions internal/utils/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,31 @@ func TestUpdateMessageTerminalMetadata(t *testing.T) {
expectedLineCount: 1,
},
{
name: "It should handle very large blocks of text lines",
msg: "One oneone two twotwo threethree. Four four five five six\nSeven",
name: "It should handle multiple termwidth overflows",
msg: "1111 2222 3333 4444",
line: "",
lineCount: 0,
termWidth: 10,
expectedLine: "Seven",
expectedLineCount: 6,
termWidth: 5,
expectedLine: "4444",
expectedLineCount: 3,
},
{
name: "It should handle multiple termwidth overflows + newlines",
msg: "1111 22\n3333 4444",
line: "",
lineCount: 0,
termWidth: 5,
expectedLine: "4444",
expectedLineCount: 4,
},
{
name: "It should handle multiple termwidth overflows + newlines",
msg: "11 22 33 44 55 66",
line: "",
lineCount: 0,
termWidth: 3,
expectedLine: "66",
expectedLineCount: 5,
},
}

Expand All @@ -76,3 +94,68 @@ func TestUpdateMessageTerminalMetadata(t *testing.T) {
})
}
}

func Test_countNewLines(t *testing.T) {
testCases := []struct {
desc string
given struct {
msg string
termWidth int
}
want int
}{
{
desc: "it should count spaces when splitting into new lines",
given: struct {
msg string
termWidth int
}{
msg: " ",
termWidth: 5,
},
want: 2,
},
{
desc: "it should count spaces when splitting into new lines, test 2",
given: struct {
msg string
termWidth int
}{
msg: "1 2 3 ",
termWidth: 2,
},
want: 3,
},
{
desc: "it should count spaces when splitting into new lines, test 3",
given: struct {
msg string
termWidth int
}{
msg: "1 2 3 4",
termWidth: 2,
},
want: 3,
},
{
desc: "it should sometimes treat space as newline, maybe",
given: struct {
msg string
termWidth int
}{
msg: "Hello World",
termWidth: 5,
},
want: 2,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
got := countNewLines(tC.given.msg, tC.given.termWidth)
want := tC.want
if got != want {
t.Fatalf("expected: %v, got: %v", want, got)
}
})
}
}

0 comments on commit a14675b

Please sign in to comment.