mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-26 13:49:01 +08:00
37 lines
843 B
Go
37 lines
843 B
Go
package clui
|
|
|
|
import (
|
|
//"fmt"
|
|
xs "github.com/huandu/xstrings"
|
|
)
|
|
|
|
// Trancates text to maxWidth by replacing long
|
|
// substring in the middle with ellipsis and keeping
|
|
// the beginning and ending of the string untouched.
|
|
// If maxWidth is less than 5 then no ellipsis is
|
|
// added, the text is just truncated from the right.
|
|
func Ellipsize(str string, maxWidth int) string {
|
|
ln := xs.Len(str)
|
|
if ln <= maxWidth {
|
|
return str
|
|
}
|
|
|
|
if maxWidth < 5 {
|
|
return xs.Slice(str, 0, maxWidth)
|
|
}
|
|
|
|
left := int((maxWidth - 3) / 2)
|
|
right := maxWidth - left - 3
|
|
return xs.Slice(str, 0, left) + "..." + xs.Slice(str, ln-right, -1)
|
|
}
|
|
|
|
// Make a text no longer than maxWidth
|
|
func CutText(str string, maxWidth int) string {
|
|
ln := xs.Len(str)
|
|
if ln <= maxWidth {
|
|
return str
|
|
}
|
|
|
|
return xs.Slice(str, 0, maxWidth)
|
|
}
|