From ffebeed1fd9a1792c5d8acbb037523c2847be1af Mon Sep 17 00:00:00 2001 From: Vladimir Markelov Date: Mon, 26 Oct 2015 11:33:11 -0700 Subject: [PATCH] closes #9 - basic limited label colorful output --- canvas.go | 31 +++++++++++++++++++++++++++++++ colorparse.go | 9 ++++++++- interface.go | 1 + label.go | 31 ++++++++++++++++++++++++------- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/canvas.go b/canvas.go index e3959f4..87dffa5 100644 --- a/canvas.go +++ b/canvas.go @@ -174,6 +174,37 @@ func (fb *FrameBuffer) PutTextVertical(x, y int, text string, fg, bg term.Attrib } } +func (fb *FrameBuffer) PutColorizedText(x, y, max int, text string, fg, bg term.Attribute, dir Direction, align Align) { + // file, _ := os.OpenFile("debugui.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + // logger := log.New(file, "", log.Ldate|log.Ltime|log.Lshortfile) + + sReal := UnColorizeText(text) + var dx, dy int + if dir == Horizontal { + delta, _ := AlignText(sReal, max, align) + x += delta + dx = 1 + } else { + delta, _ := AlignText(sReal, max, align) + y += delta + dy = 1 + } + + parser := NewColorParser(text, fg, bg) + elem := parser.NextElement() + for elem.Type != ElemEndOfText && max > 0 { + // logger.Printf("ELEM: %v", elem) + if elem.Type == ElemPrintable { + fb.PutChar(x, y, elem.Ch, elem.Fg, elem.Bg) + x += dx + y += dy + max-- + } + + elem = parser.NextElement() + } +} + func (fb *FrameBuffer) DrawFrame(x, y, w, h int, fg, bg term.Attribute, frameChars string) { if h < 1 || w < 1 { return diff --git a/colorparse.go b/colorparse.go index 20d0b90..3fd252d 100644 --- a/colorparse.go +++ b/colorparse.go @@ -53,7 +53,8 @@ func (p *ColorParser) parseColor() (term.Attribute, TextElementType, bool) { cText string attr term.Attribute t TextElementType - step int + step int = StepType + done bool ) for { @@ -92,6 +93,7 @@ func (p *ColorParser) parseColor() (term.Attribute, TextElementType, bool) { } else { attr = StringToColor(cText) } + done = true break } else { if c != ' ' || cText != "" { @@ -100,6 +102,10 @@ func (p *ColorParser) parseColor() (term.Attribute, TextElementType, bool) { newIdx++ } } + + if done || !ok { + break + } } return attr, t, ok @@ -121,6 +127,7 @@ func (p *ColorParser) NextElement() TextElement { } attr, atype, ok := p.parseColor() + // logger.Printf("PARSED: %v, %v, %v (at %v)", attr, atype, ok, p.index) if !ok { p.index++ return TextElement{Type: ElemPrintable, Ch: p.text[p.index-1], Fg: p.currText, Bg: p.currBack} diff --git a/interface.go b/interface.go index 2b92070..3bc2d45 100644 --- a/interface.go +++ b/interface.go @@ -19,6 +19,7 @@ type Canvas interface { PutSymbol(int, int, term.Cell) bool PutText(int, int, string, term.Attribute, term.Attribute) PutVerticalText(int, int, string, term.Attribute, term.Attribute) + PutColorizedText(int, int, int, string, term.Attribute, term.Attribute, Direction, Align) Symbol(int, int) (term.Cell, bool) Clear(term.Attribute) FillRect(int, int, int, int, term.Cell) diff --git a/label.go b/label.go index 61be234..4a2fb08 100644 --- a/label.go +++ b/label.go @@ -9,8 +9,9 @@ import ( type Label struct { ControlBase - direction Direction - multiline bool + direction Direction + multiline bool + multicolor bool } func NewLabel(view View, parent Control, w, h int, title string, scale int) *Label { @@ -87,12 +88,20 @@ func (l *Label) Repaint() { idx++ } } else { - if l.direction == Horizontal { - shift, text := AlignText(l.title, l.width, l.align) - canvas.PutText(l.x+shift, l.y, text, fg, bg) + if l.multicolor { + max := l.width + if l.direction == Vertical { + max = l.height + } + canvas.PutColorizedText(l.x, l.y, max, l.title, fg, bg, l.direction, l.align) } else { - shift, text := AlignText(l.title, l.height, l.align) - canvas.PutVerticalText(l.x, l.y+shift, text, fg, bg) + if l.direction == Horizontal { + shift, text := AlignText(l.title, l.width, l.align) + canvas.PutText(l.x+shift, l.y, text, fg, bg) + } else { + shift, text := AlignText(l.title, l.height, l.align) + canvas.PutVerticalText(l.x, l.y+shift, text, fg, bg) + } } } } @@ -104,3 +113,11 @@ func (l *Label) Multiline() bool { func (l *Label) SetMultiline(multi bool) { l.multiline = multi } + +func (l *Label) MultiColored() bool { + return l.multicolor +} + +func (l *Label) SetMultiColored(multi bool) { + l.multicolor = multi +}