123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package door
- import (
- "log"
- "strings"
- "unicode"
- )
- type Line struct {
- Text string
- DefaultColor string
- RenderF func(string) string // Render function (displays string with colors)
- UpdateF func() string // Update function updates the text
- Width int
- }
- func (l *Line) Update() bool {
- if l.UpdateF == nil {
- return false
- }
- var NewText string = l.UpdateF()
- l.LineLength(&NewText)
- if NewText != l.Text {
- l.Text = NewText
- return true
- }
- return false
- }
- func (l *Line) LineLength(text *string) {
- var length int
- if l.Width == 0 {
- return
- }
- if Unicode {
- length = len([]rune(*text))
- } else {
- length = len([]byte(*text))
- }
- if length > l.Width {
- log.Printf("ERROR: Line Width %d: Have %d\n", l.Width, length)
- } else {
- *text += strings.Repeat(" ", l.Width-length)
- }
- }
- func (l *Line) Output() string {
- l.LineLength(&l.Text)
- if l.RenderF == nil {
- return l.DefaultColor + l.Text
- } else {
- return l.RenderF(l.Text)
- }
- }
- 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
- }
- if Unicode {
-
- r.Result += string([]rune(r.Line)[r.Pos : r.Pos+len])
- } else {
- r.Result += r.Line[r.Pos : r.Pos+len]
- }
- r.Pos += len
- }
- func RenderBlueYellow(text string) string {
- var r Render = Render{Line: text}
- var blue string = ColorText("BOLD BLUE")
- var yellow string = ColorText("BOLD YELLOW")
- for _, letter := range text {
- if unicode.IsUpper(letter) {
- r.Append(blue, 1)
- } else {
- r.Append(yellow, 1)
- }
- }
- return r.Result
- }
|