closes #9 - basic limited label colorful output

This commit is contained in:
Vladimir Markelov 2015-10-26 11:33:11 -07:00
parent ee61615b56
commit ffebeed1fd
4 changed files with 64 additions and 8 deletions

View File

@ -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

View File

@ -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}

View File

@ -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)

View File

@ -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
}