Add WrapText option to Paragraph

This commit is contained in:
Caleb Bassi 2019-01-26 02:47:47 -08:00
parent a5828eb2da
commit 5b78b896c9
5 changed files with 97 additions and 59 deletions

View File

@ -2,7 +2,7 @@ package termui
const ( const (
DOT = '•' DOT = '•'
DOTS = '…' ELLIPSES = '…'
) )
var ( var (

125
utils.go
View File

@ -30,31 +30,43 @@ func InterfaceSlice(slice interface{}) []interface{} {
return ret return ret
} }
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
// TrimString trims a string to a max length and adds '…' to the end if it was trimmed. // TrimString trims a string to a max length and adds '…' to the end if it was trimmed.
func TrimString(s string, w int) string { func TrimString(s string, w int) string {
if w <= 0 { if w <= 0 {
return "" return ""
} }
if rw.StringWidth(s) > w { if rw.StringWidth(s) > w {
return rw.Truncate(s, w, string(DOTS)) return rw.Truncate(s, w, string(ELLIPSES))
} }
return s return s
} }
func SelectColor(colors []Color, index int) Color {
return colors[index%len(colors)]
}
func SelectStyle(styles []Style, index int) Style {
return styles[index%len(styles)]
}
// Math ------------------------------------------------------------------------
func SumIntSlice(slice []int) int {
sum := 0
for _, val := range slice {
sum += val
}
return sum
}
func SumFloat64Slice(data []float64) float64 {
sum := 0.0
for _, v := range data {
sum += v
}
return sum
}
func GetMaxIntFromSlice(slice []int) (int, error) { func GetMaxIntFromSlice(slice []int) (int, error) {
if len(slice) == 0 { if len(slice) == 0 {
return 0, fmt.Errorf("cannot get max value from empty slice") return 0, fmt.Errorf("cannot get max value from empty slice")
@ -96,42 +108,10 @@ func GetMaxFloat64From2dSlice(slices [][]float64) (float64, error) {
return max, nil return max, nil
} }
func SelectColor(colors []Color, index int) Color {
return colors[index%len(colors)]
}
func SelectStyle(styles []Style, index int) Style {
return styles[index%len(styles)]
}
func CellsToString(cells []Cell) string {
runes := make([]rune, len(cells))
for i, cell := range cells {
runes[i] = cell.Rune
}
return string(runes)
}
func RoundFloat64(x float64) float64 { func RoundFloat64(x float64) float64 {
return math.Floor(x + 0.5) return math.Floor(x + 0.5)
} }
func SumIntSlice(slice []int) int {
sum := 0
for _, val := range slice {
sum += val
}
return sum
}
func SumFloat64Slice(data []float64) float64 {
sum := 0.0
for _, v := range data {
sum += v
}
return sum
}
func AbsInt(x int) int { func AbsInt(x int) int {
if x >= 0 { if x >= 0 {
return x return x