123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package door
- import (
- "unicode"
- )
- type Line struct {
- Text string
- DefaultColor string
- RenderF func(string) string
- UpdateF func() string
- }
- func (l *Line) Update() bool {
- if l.UpdateF == nil {
- return false
- }
- NewText := l.UpdateF()
- if NewText != l.Text {
- l.Text = NewText
- return true
- }
- return false
- }
- func (l *Line) Output() string {
- if l.RenderF == nil {
- return l.DefaultColor + l.Text
- } else {
- return l.RenderF(l.Text)
- }
- }
- // Sample RenderF function
- // Upcase is BOLD Blue, everything else is Yellow
- func RenderBlueYellow(text string) string {
- var output string
- var lastColor string
- blue := ColorText("BOLD BLUE")
- yellow := ColorText("BOLD YELLOW")
- for _, letter := range text {
- if unicode.IsUpper(letter) {
- if lastColor != blue {
- lastColor = blue
- output += blue
- }
- output += string(letter)
- } else {
- if lastColor != yellow {
- lastColor = yellow
- output += yellow
- }
- output += string(letter)
- }
- }
- return output
- }
- type Render struct {
- Line string
- Result string
- Pos int
- LastColor string
- }
- func (r *Render) Append(color string, len int) {
- if color != r.LastColor {
- r.LastColor = color
- r.Result += color
- }
- r.Result += r.Line[r.Pos : r.Pos+len]
- r.Pos += len
- }
|