X Tutup
Skip to content

Commit 61ff5d7

Browse files
committed
Fix crash in issue/pr list for narrow terminals
If the available column width is smaller than 3, don't try to truncate with ellipsis (`...`). Instead, just truncate to available width.
1 parent 48ffd5a commit 61ff5d7

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

utils/table_printer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ func (t *tsvTablePrinter) Render() error {
176176

177177
func truncate(maxLength int, title string) string {
178178
if len(title) > maxLength {
179-
return title[0:maxLength-3] + "..."
179+
if maxLength > 3 {
180+
return title[0:maxLength-3] + "..."
181+
}
182+
return title[0:maxLength]
180183
}
181184
return title
182185
}

utils/table_printer_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func Test_ttyTablePrinter_truncate(t *testing.T) {
9+
buf := bytes.Buffer{}
10+
tp := &ttyTablePrinter{
11+
out: &buf,
12+
maxWidth: 5,
13+
}
14+
15+
tp.AddField("1", nil, nil)
16+
tp.AddField("hello", nil, nil)
17+
tp.EndRow()
18+
tp.AddField("2", nil, nil)
19+
tp.AddField("world", nil, nil)
20+
tp.EndRow()
21+
22+
err := tp.Render()
23+
if err != nil {
24+
t.Fatalf("unexpected error: %v", err)
25+
}
26+
27+
expected := "1 he\n2 wo\n"
28+
if buf.String() != expected {
29+
t.Errorf("expected: %q, got: %q", expected, buf.String())
30+
}
31+
}

0 commit comments

Comments
 (0)
X Tutup