tui/rune.go
2022-04-01 20:10:51 +02:00

61 lines
1.3 KiB
Go

package tui
import "regexp"
type Rune struct {
Rn rune
Style Style
}
var DefaultRune = Rune{' ', StyleDefault}
// Text represents a string with rune-based styling
type Text struct {
str string
style []Style
}
// Txt returns a Text containing the given str
// using the default style
func Txt(str string) *Text {
styles := make([]Style, 0, len(str))
for range str {
styles = append(styles, StyleDefault)
}
return &Text{str: str, style: styles}
}
// Len returns the amount of runes in t
func (t *Text) Len() int {
return len(t.style)
}
// AppendString appends str with default styling to t
func (t *Text) AppendString(str string) {
t.AppendText(Txt(str))
}
// PrependString prepends str with default styling to t
func (t *Text) PrependString(str string) {
newTxt := Txt(str)
newTxt.AppendText(t)
*t = *newTxt
}
// AppendText appends txt to t
func (t *Text) AppendText(txt *Text) {
t.str += txt.str
t.style = append(t.style, txt.style...)
}
// Style applies the given style to the part of t between startIndex (inclusive) and endIndex (exclusive)
func (t *Text) Style(style Style, startIndex, endIndex int) {
for i := startIndex; i < endIndex; i++ {
t.style[i] = style
}
}
func (t *Text) StyleRegex(style Style, pattern *regexp.Regexp) {
// TODO
}