Skip to content

Commit

Permalink
table: process Carriage-Return codes correctly; fixes #284
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Jan 8, 2024
1 parent 50f17e4 commit 9755509
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
4 changes: 3 additions & 1 deletion list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"strings"
"unicode/utf8"

"github.com/jedib0t/go-pretty/v6/text"
)

const (
Expand Down Expand Up @@ -102,7 +104,7 @@ func (l *List) Style() *Style {
func (l *List) analyzeAndStringify(item interface{}) *listItem {
itemStr := fmt.Sprint(item)
itemStr = strings.ReplaceAll(itemStr, "\t", " ")
itemStr = strings.ReplaceAll(itemStr, "\r", "")
itemStr = text.ProcessCRLF(itemStr)
return &listItem{
Level: l.level,
Text: itemStr,
Expand Down
2 changes: 1 addition & 1 deletion progress/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (p *Progress) renderPinnedMessages(out *strings.Builder) {
func (p *Progress) renderTracker(out *strings.Builder, t *Tracker, hint renderHint) {
message := t.message()
message = strings.ReplaceAll(message, "\t", " ")
message = strings.ReplaceAll(message, "\r", "")
message = strings.ReplaceAll(message, "\r", "") // replace with text.ProcessCRLF?
if p.lengthMessage > 0 {
messageLen := text.RuneWidthWithoutEscSequences(message)
if messageLen < p.lengthMessage {
Expand Down
2 changes: 1 addition & 1 deletion table/render_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (t *Table) analyzeAndStringifyColumn(colIdx int, col interface{}, hint rend
colStr = fmt.Sprint(col)
}
colStr = strings.ReplaceAll(colStr, "\t", " ")
colStr = strings.ReplaceAll(colStr, "\r", "")
colStr = text.ProcessCRLF(colStr)
return fmt.Sprintf("%s%s", t.style.Format.Direction.Modifier(), colStr)
}

Expand Down
21 changes: 21 additions & 0 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,27 @@ func TestTable_Render_ColumnConfigs(t *testing.T) {
)
}

func TestTable_Render_CRLF(t *testing.T) {
tw := NewWriter()
tw.AppendHeader(testHeader)
tw.AppendRows(testRows)
tw.AppendRow(Row{5000, "Night", "King", 10000, "Was once a\r\nMortal \rMan"})
tw.AppendFooter(testFooter)

compareOutput(t, tw.Render(), `
+------+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+------+------------+-----------+--------+-----------------------------+
| 1 | Arya | Stark | 3000 | |
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
| 300 | Tyrion | Lannister | 5000 | |
| 5000 | Night | King | 10000 | Was once a |
| | | | | Man |
+------+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+------+------------+-----------+--------+-----------------------------+`)
}

func TestTable_Render_Empty(t *testing.T) {
tw := NewWriter()
assert.Empty(t, tw.Render())
Expand Down
24 changes: 24 additions & 0 deletions text/string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package text

import (
"regexp"
"strings"
"unicode/utf8"

Expand Down Expand Up @@ -107,6 +108,29 @@ func Pad(str string, maxLen int, paddingChar rune) string {
return str
}

var (
reCarriageReturn = regexp.MustCompile(`(.*)\r`)
)

// ProcessCRLF converts "\r\n" to "\n", and erases everything preceding a lone
// "\r" in each line of the string.
func ProcessCRLF(str string) string {
str = strings.ReplaceAll(str, "\r\n", "\n")

// process \r by erasing everything preceding it in the line

This comment has been minimized.

Copy link
@jievince

jievince Jan 8, 2024

Simply deleting the characters before \r does not seem to be correct. \r is used to move the cursor back to the beginning of a line, for example "abc\rde", \r will move the cursor back in front of character 'a', so 'd' will override 'a', 'e' will override 'b', and 'c' will remain, therefore it should print "dec" instead of "de".

if strings.Contains(str, "\r") {
lines := strings.Split(str, "\n")
for idx := range lines {
for reCarriageReturn.MatchString(lines[idx]) {
lines[idx] = reCarriageReturn.ReplaceAllString(lines[idx], "")
}
}
str = strings.Join(lines, "\n")
}

return str
}

// RepeatAndTrim repeats the given string until it is as long as maxRunes.
// For ex.:
//
Expand Down
22 changes: 22 additions & 0 deletions text/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func TestPad(t *testing.T) {
assert.Equal(t, "\x1b]8;;http://example.com\x1b\\Ghost\x1b]8;;\x1b\\.....", Pad("\x1b]8;;http://example.com\x1b\\Ghost\x1b]8;;\x1b\\", 10, '.'))
}

func ExampleProcessCRLF() {
fmt.Printf("%#v\n", ProcessCRLF("abc"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi\njkl"))
fmt.Printf("%#v\n", ProcessCRLF("abc\r\ndef\rghi\njkl\r"))

// Output: "abc"
// "abc\ndef"
// "abc\nghi"
// "abc\nghi\njkl"
// "abc\nghi\n"
}

func TestProcessCRLF(t *testing.T) {
assert.Equal(t, "abc", ProcessCRLF("abc"))
assert.Equal(t, "abc\ndef", ProcessCRLF("abc\r\ndef"))
assert.Equal(t, "abc\nghi", ProcessCRLF("abc\r\ndef\rghi"))
assert.Equal(t, "abc\nghi\njkl", ProcessCRLF("abc\r\ndef\rghi\njkl"))
assert.Equal(t, "abc\nghi\n", ProcessCRLF("abc\r\ndef\rghi\njkl\r"))
}

func ExampleRepeatAndTrim() {
fmt.Printf("RepeatAndTrim(\"\", 5): %#v\n", RepeatAndTrim("", 5))
fmt.Printf("RepeatAndTrim(\"Ghost\", 0): %#v\n", RepeatAndTrim("Ghost", 0))
Expand Down

0 comments on commit 9755509

Please sign in to comment.