Refactor truncate string
This commit is contained in:
parent
0176058eb0
commit
c704dcd9d4
@ -141,12 +141,8 @@ func formatTexts(o []string, max int, prefix, suffix string) (new []string, widt
|
|||||||
} else if x > width {
|
} else if x > width {
|
||||||
x := runewidth.Truncate(o[i], width, shortenSuffix)
|
x := runewidth.Truncate(o[i], width, shortenSuffix)
|
||||||
// When calling runewidth.Truncate("您好xxx您好xxx", 11, "...") returns "您好xxx..."
|
// When calling runewidth.Truncate("您好xxx您好xxx", 11, "...") returns "您好xxx..."
|
||||||
// But the length of this result is 10. So we need to insert a space.
|
// But the length of this result is 10. So we need fill right using runewidth.FillRight.
|
||||||
if l := runewidth.StringWidth(x); l < width {
|
n[i] = prefix + runewidth.FillRight(x, width) + suffix
|
||||||
spaces := strings.Repeat(" ", width - l)
|
|
||||||
x += spaces
|
|
||||||
}
|
|
||||||
n[i] = prefix + x + suffix
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return n, lenPrefix + width + lenSuffix
|
return n, lenPrefix + width + lenSuffix
|
||||||
|
Loading…
Reference in New Issue
Block a user