tui/utils.go
2022-04-03 16:29:01 +02:00

116 lines
3.0 KiB
Go

package tui
import (
"strings"
"github.com/mattn/go-runewidth"
)
// WriteString writes a whole string to the buffer at position (x,y)
// no word wrap is applied at all. If the string does not fit, it will be truncated
func WriteString(b *ViewBuffer, str string, style Style, x, y int) (width int) {
dx := x
for _, r := range str {
if dx >= b.Width() {
return
}
b.Set(dx, y, Rune{r, style})
dx += runeWidth(r)
}
return dx - x
}
// WriteMultiLineString writes a multi-line string to the buffer at position (x,y)
// no word wrap is applied at all. If a line does not fit horizontally, it will be truncated
// All lines which do not fit vertically will be truncated as well
func WriteMultiLineString(b *ViewBuffer, str string, style Style, x, y int) (maxLineWidth, lineCount int) {
lines := strings.Split(str, "\n")
for dy, line := range lines {
if dy >= b.Height() {
return
}
lineWidth := WriteString(b, line, style, x, y+dy)
maxLineWidth = max(maxLineWidth, lineWidth)
}
return maxLineWidth, len(lines)
}
// MeasureString measures how much horizontal space str consumes when drawn to a buffer
func MeasureString(str string) (width int) {
dx := 0
for _, r := range str {
dx += runeWidth(r)
}
return dx
}
// MeasureString measures how much horizontal and vertical space str consumes when drawn to a buffer
func MeasureMultiLineString(str string) (maxLineWidth, lineCount int) {
lines := strings.Split(str, "\n")
for _, line := range lines {
lineWidth := MeasureString(line)
maxLineWidth = max(maxLineWidth, lineWidth)
}
return maxLineWidth, len(lines)
}
func runeWidth(r rune) int {
return runewidth.RuneWidth(r)
//fmt.Println(r, width.LookupRune(r).Kind())
// switch width.LookupRune(r).Kind() {
// case width.EastAsianFullwidth:
// fallthrough
// case width.EastAsianWide:
// return 2
// default:
// return 1
// }
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func iff[T any](condition bool, trueValue, falseValue T) T {
if condition {
return trueValue
}
return falseValue
}
func ConstrainBufferToAnchor(buf *ViewBuffer, anchor Anchor, width, height int) *ViewBuffer {
switch anchor {
default:
fallthrough
case AnchorTopLeft:
return buf.Sub(0, 0, width, height)
case AnchorTop:
return buf.Sub(buf.Width()/2-width/2, 0, width, height)
case AnchorTopRight:
return buf.Sub(buf.Width()-width, 0, width, height)
case AnchorLeft:
return buf.Sub(0, buf.Height()/2-height/2, width, height)
case AnchorCenter:
return buf.Sub(buf.Width()/2-width/2, buf.Height()/2-height/2, width, height)
case AnchorRight:
return buf.Sub(buf.Width()-width, buf.Height()/2-height/2, width, height)
case AnchorBottomLeft:
return buf.Sub(0, buf.Height()-height, width, height)
case AnchorBottom:
return buf.Sub(buf.Width()/2-width/2, buf.Height()-height, width, height)
case AnchorBottomRight:
return buf.Sub(buf.Width()-width, buf.Height()-height, width, height)
}
}