Skip to content

Commit f91892b

Browse files
committed
fix truncation
1 parent 72bce20 commit f91892b

File tree

3 files changed

+88
-28
lines changed

3 files changed

+88
-28
lines changed

pkg/utils/formatting.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,19 @@ func getPadWidths(stringArrays [][]string) []int {
6666
}
6767
return padWidths
6868
}
69+
70+
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
71+
func TruncateWithEllipsis(str string, limit int) string {
72+
if runewidth.StringWidth(str) > limit && limit <= 3 {
73+
return strings.Repeat(".", limit)
74+
}
75+
return runewidth.Truncate(str, limit, "...")
76+
}
77+
78+
func SafeTruncate(str string, limit int) string {
79+
if len(str) > limit {
80+
return str[0:limit]
81+
} else {
82+
return str
83+
}
84+
}

pkg/utils/formatting_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func TestGetPaddedDisplayStrings(t *testing.T) {
5858
}
5959
}
6060

61-
// TestGetPadWidths is a function.
6261
func TestGetPadWidths(t *testing.T) {
6362
type scenario struct {
6463
input [][]string
@@ -91,3 +90,75 @@ func TestGetPadWidths(t *testing.T) {
9190
}
9291
}
9392
}
93+
94+
func TestTruncateWithEllipsis(t *testing.T) {
95+
// will need to check chinese characters as well
96+
// important that we have a three dot ellipsis within the limit
97+
type scenario struct {
98+
str string
99+
limit int
100+
expected string
101+
}
102+
103+
scenarios := []scenario{
104+
{
105+
"hello world !",
106+
1,
107+
".",
108+
},
109+
{
110+
"hello world !",
111+
2,
112+
"..",
113+
},
114+
{
115+
"hello world !",
116+
3,
117+
"...",
118+
},
119+
{
120+
"hello world !",
121+
4,
122+
"h...",
123+
},
124+
{
125+
"hello world !",
126+
5,
127+
"he...",
128+
},
129+
{
130+
"hello world !",
131+
12,
132+
"hello wor...",
133+
},
134+
{
135+
"hello world !",
136+
13,
137+
"hello world !",
138+
},
139+
{
140+
"hello world !",
141+
14,
142+
"hello world !",
143+
},
144+
{
145+
"大大大大",
146+
5,
147+
"大...",
148+
},
149+
{
150+
"大大大大",
151+
2,
152+
"..",
153+
},
154+
{
155+
"大大大大",
156+
0,
157+
"",
158+
},
159+
}
160+
161+
for _, s := range scenarios {
162+
assert.EqualValues(t, s.expected, TruncateWithEllipsis(s.str, s.limit))
163+
}
164+
}

pkg/utils/utils.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,6 @@ func ModuloWithWrap(n, max int) int {
6868
}
6969
}
7070

71-
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
72-
func TruncateWithEllipsis(str string, limit int) string {
73-
if limit == 1 && len(str) > 1 {
74-
return "."
75-
}
76-
77-
if limit == 2 && len(str) > 2 {
78-
return ".."
79-
}
80-
81-
ellipsis := "..."
82-
if len(str) <= limit {
83-
return str
84-
}
85-
86-
remainingLength := limit - len(ellipsis)
87-
return str[0:remainingLength] + "..."
88-
}
89-
90-
func SafeTruncate(str string, limit int) string {
91-
if len(str) > limit {
92-
return str[0:limit]
93-
} else {
94-
return str
95-
}
96-
}
97-
9871
func FindStringSubmatch(str string, regexpStr string) (bool, []string) {
9972
re := regexp.MustCompile(regexpStr)
10073
match := re.FindStringSubmatch(str)

0 commit comments

Comments
 (0)