@@ -19,7 +19,7 @@ func IsLikelyEllipsisLeftPart(s string) bool {
19
19
return strings .HasSuffix (s , utf8Ellipsis ) || strings .HasSuffix (s , asciiEllipsis )
20
20
}
21
21
22
- func ellipsisGuessDisplayWidth (r rune ) int {
22
+ func ellipsisDisplayGuessWidth (r rune ) int {
23
23
// To make the truncated string as long as possible,
24
24
// CJK/emoji chars are considered as 2-ASCII width but not 3-4 bytes width.
25
25
// Here we only make the best guess (better than counting them in bytes),
@@ -48,13 +48,17 @@ func ellipsisGuessDisplayWidth(r rune) int {
48
48
// It appends "…" or "..." at the end of truncated string.
49
49
// It guarantees the length of the returned runes doesn't exceed the limit.
50
50
func EllipsisDisplayString (str string , limit int ) string {
51
- s , _ , _ , _ := ellipsisDisplayString (str , limit )
51
+ s , _ , _ , _ := ellipsisDisplayString (str , limit , ellipsisDisplayGuessWidth )
52
52
return s
53
53
}
54
54
55
55
// EllipsisDisplayStringX works like EllipsisDisplayString while it also returns the right part
56
56
func EllipsisDisplayStringX (str string , limit int ) (left , right string ) {
57
- left , offset , truncated , encounterInvalid := ellipsisDisplayString (str , limit )
57
+ return ellipsisDisplayStringX (str , limit , ellipsisDisplayGuessWidth )
58
+ }
59
+
60
+ func ellipsisDisplayStringX (str string , limit int , widthGuess func (rune ) int ) (left , right string ) {
61
+ left , offset , truncated , encounterInvalid := ellipsisDisplayString (str , limit , widthGuess )
58
62
if truncated {
59
63
right = str [offset :]
60
64
r , _ := utf8 .DecodeRune (UnsafeStringToBytes (right ))
@@ -68,7 +72,7 @@ func EllipsisDisplayStringX(str string, limit int) (left, right string) {
68
72
return left , right
69
73
}
70
74
71
- func ellipsisDisplayString (str string , limit int ) (res string , offset int , truncated , encounterInvalid bool ) {
75
+ func ellipsisDisplayString (str string , limit int , widthGuess func ( rune ) int ) (res string , offset int , truncated , encounterInvalid bool ) {
72
76
if len (str ) <= limit {
73
77
return str , len (str ), false , false
74
78
}
@@ -81,7 +85,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
81
85
for i , r := range str {
82
86
encounterInvalid = encounterInvalid || r == utf8 .RuneError
83
87
pos = i
84
- runeWidth := ellipsisGuessDisplayWidth (r )
88
+ runeWidth := widthGuess (r )
85
89
if used + runeWidth + 3 > limit {
86
90
break
87
91
}
@@ -96,7 +100,7 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
96
100
if nextCnt >= 4 {
97
101
break
98
102
}
99
- nextWidth += ellipsisGuessDisplayWidth (r )
103
+ nextWidth += widthGuess (r )
100
104
nextCnt ++
101
105
}
102
106
if nextCnt <= 3 && used + nextWidth <= limit {
@@ -114,6 +118,10 @@ func ellipsisDisplayString(str string, limit int) (res string, offset int, trunc
114
118
return str [:offset ] + ellipsis , offset , true , encounterInvalid
115
119
}
116
120
121
+ func EllipsisTruncateRunes (str string , limit int ) (left , right string ) {
122
+ return ellipsisDisplayStringX (str , limit , func (r rune ) int { return 1 })
123
+ }
124
+
117
125
// TruncateRunes returns a truncated string with given rune limit,
118
126
// it returns input string if its rune length doesn't exceed the limit.
119
127
func TruncateRunes (str string , limit int ) string {
0 commit comments