mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-25 12:24:47 +02:00
fix truncation
This commit is contained in:
parent
72bce201df
commit
f91892b8f1
@ -66,3 +66,19 @@ func getPadWidths(stringArrays [][]string) []int {
|
|||||||
}
|
}
|
||||||
return padWidths
|
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) {
|
func TestGetPadWidths(t *testing.T) {
|
||||||
type scenario struct {
|
type scenario struct {
|
||||||
input [][]string
|
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) {
|
func FindStringSubmatch(str string, regexpStr string) (bool, []string) {
|
||||||
re := regexp.MustCompile(regexpStr)
|
re := regexp.MustCompile(regexpStr)
|
||||||
match := re.FindStringSubmatch(str)
|
match := re.FindStringSubmatch(str)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user