clui/text_utils.go
2015-09-21 20:54:39 -07:00

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)
}