mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-04 03:48:07 +02:00
fix truncation
This commit is contained in:
parent
72bce201df
commit
f91892b8f1
@ -66,3 +66,19 @@ func getPadWidths(stringArrays [][]string) []int {
|
||||
}
|
||||
return padWidths
|
||||
}
|
||||
|
||||
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
|
||||
func TruncateWithEllipsis(str string, limit int) string {
|
||||
if runewidth.StringWidth(str) > limit && limit <= 3 {
|
||||
return strings.Repeat(".", limit)
|
||||
}
|
||||
return runewidth.Truncate(str, limit, "...")
|
||||
}
|
||||
|
||||
func SafeTruncate(str string, limit int) string {
|
||||
if len(str) > limit {
|
||||
return str[0:limit]
|
||||
} else {
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,6 @@ func TestGetPaddedDisplayStrings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetPadWidths is a function.
|
||||
func TestGetPadWidths(t *testing.T) {
|
||||
type scenario struct {
|
||||
input [][]string
|
||||
@ -91,3 +90,75 @@ func TestGetPadWidths(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateWithEllipsis(t *testing.T) {
|
||||
// will need to check chinese characters as well
|
||||
// important that we have a three dot ellipsis within the limit
|
||||
type scenario struct {
|
||||
str string
|
||||
limit int
|
||||
expected string
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
"hello world !",
|
||||
1,
|
||||
".",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
2,
|
||||
"..",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
3,
|
||||
"...",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
4,
|
||||
"h...",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
5,
|
||||
"he...",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
12,
|
||||
"hello wor...",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
13,
|
||||
"hello world !",
|
||||
},
|
||||
{
|
||||
"hello world !",
|
||||
14,
|
||||
"hello world !",
|
||||
},
|
||||
{
|
||||
"大大大大",
|
||||
5,
|
||||
"大...",
|
||||
},
|
||||
{
|
||||
"大大大大",
|
||||
2,
|
||||
"..",
|
||||
},
|
||||
{
|
||||
"大大大大",
|
||||
0,
|
||||
"",
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
assert.EqualValues(t, s.expected, TruncateWithEllipsis(s.str, s.limit))
|
||||
}
|
||||
}
|
||||
|
@ -68,33 +68,6 @@ func ModuloWithWrap(n, max int) int {
|
||||
}
|
||||
}
|
||||
|
||||
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
|
||||
func TruncateWithEllipsis(str string, limit int) string {
|
||||
if limit == 1 && len(str) > 1 {
|
||||
return "."
|
||||
}
|
||||
|
||||
if limit == 2 && len(str) > 2 {
|
||||
return ".."
|
||||
}
|
||||
|
||||
ellipsis := "..."
|
||||
if len(str) <= limit {
|
||||
return str
|
||||
}
|
||||
|
||||
remainingLength := limit - len(ellipsis)
|
||||
return str[0:remainingLength] + "..."
|
||||
}
|
||||
|
||||
func SafeTruncate(str string, limit int) string {
|
||||
if len(str) > limit {
|
||||
return str[0:limit]
|
||||
} else {
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
func FindStringSubmatch(str string, regexpStr string) (bool, []string) {
|
||||
re := regexp.MustCompile(regexpStr)
|
||||
match := re.FindStringSubmatch(str)
|
||||
|
Loading…
Reference in New Issue
Block a user