Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: process Carriage-Return codes correctly #291

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading