2015-09-21 20:54:39 -07:00
|
|
|
package clui
|
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
import (
|
2015-10-20 09:43:56 -07:00
|
|
|
xs "github.com/huandu/xstrings"
|
2015-10-16 10:27:43 -07:00
|
|
|
term "github.com/nsf/termbox-go"
|
2015-10-21 13:59:23 -07:00
|
|
|
"strings"
|
2015-10-16 10:27:43 -07:00
|
|
|
)
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
type Label struct {
|
|
|
|
ControlBase
|
2015-10-26 11:33:11 -07:00
|
|
|
direction Direction
|
|
|
|
multiline bool
|
|
|
|
multicolor bool
|
2015-09-21 20:54:39 -07:00
|
|
|
}
|
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
func NewLabel(view View, parent Control, w, h int, title string, scale int) *Label {
|
|
|
|
c := new(Label)
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-20 09:43:56 -07:00
|
|
|
if w == AutoSize {
|
|
|
|
w = xs.Len(title)
|
|
|
|
}
|
|
|
|
if h == AutoSize {
|
|
|
|
h = 1
|
|
|
|
}
|
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
c.view = view
|
|
|
|
c.parent = parent
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
c.SetTitle(title)
|
|
|
|
c.SetSize(w, h)
|
|
|
|
c.SetConstraints(w, h)
|
|
|
|
c.tabSkip = true
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
if parent != nil {
|
|
|
|
parent.AddChild(c, scale)
|
2015-09-21 20:54:39 -07:00
|
|
|
}
|
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
return c
|
2015-09-21 20:54:39 -07:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:51:18 -07:00
|
|
|
func (l *Label) Direction() Direction {
|
|
|
|
return l.direction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) SetDirection(dir Direction) {
|
|
|
|
l.direction = dir
|
|
|
|
}
|
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
func (l *Label) Repaint() {
|
|
|
|
canvas := l.view.Canvas()
|
|
|
|
tm := l.view.Screen().Theme()
|
2015-10-19 12:05:43 -07:00
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
fg, bg := RealColor(tm, l.fg, ColorText), RealColor(tm, l.bg, ColorBack)
|
2015-10-19 12:05:43 -07:00
|
|
|
if !l.Enabled() {
|
|
|
|
fg = RealColor(tm, l.fg, ColorDisabledText)
|
|
|
|
}
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-16 10:27:43 -07:00
|
|
|
canvas.FillRect(l.x, l.y, l.width, l.height, term.Cell{Ch: ' ', Fg: fg, Bg: bg})
|
2015-09-21 20:54:39 -07:00
|
|
|
|
2015-10-21 13:59:23 -07:00
|
|
|
if l.multiline {
|
|
|
|
lineCnt, lineLen := l.height, l.width
|
|
|
|
if l.direction == Vertical {
|
|
|
|
lineCnt, lineLen = l.width, l.height
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(l.title, "\n")
|
|
|
|
|
|
|
|
var realLines []string
|
|
|
|
for _, s := range lines {
|
|
|
|
curr := s
|
|
|
|
for xs.Len(curr) > lineLen {
|
|
|
|
realLines = append(realLines, xs.Slice(curr, 0, lineLen))
|
|
|
|
curr = xs.Slice(curr, lineLen, -1)
|
|
|
|
}
|
|
|
|
realLines = append(realLines, curr)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
for idx < lineCnt && idx < len(realLines) {
|
|
|
|
if l.direction == Horizontal {
|
|
|
|
shift, text := AlignText(realLines[idx], l.width, l.align)
|
|
|
|
canvas.PutText(l.x+shift, l.y+idx, text, fg, bg)
|
|
|
|
} else {
|
|
|
|
shift, text := AlignText(realLines[idx], l.height, l.align)
|
|
|
|
canvas.PutVerticalText(l.x+idx, l.y+shift, text, fg, bg)
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
2015-10-19 14:51:18 -07:00
|
|
|
} else {
|
2015-10-26 11:33:11 -07:00
|
|
|
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)
|
2015-10-21 13:59:23 -07:00
|
|
|
} else {
|
2015-10-26 11:33:11 -07:00
|
|
|
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)
|
|
|
|
}
|
2015-10-21 13:59:23 -07:00
|
|
|
}
|
2015-10-19 14:51:18 -07:00
|
|
|
}
|
2015-09-21 20:54:39 -07:00
|
|
|
}
|
2015-10-21 13:59:23 -07:00
|
|
|
|
|
|
|
func (l *Label) Multiline() bool {
|
|
|
|
return l.multiline
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) SetMultiline(multi bool) {
|
|
|
|
l.multiline = multi
|
|
|
|
}
|
2015-10-26 11:33:11 -07:00
|
|
|
|
|
|
|
func (l *Label) MultiColored() bool {
|
|
|
|
return l.multicolor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) SetMultiColored(multi bool) {
|
|
|
|
l.multicolor = multi
|
|
|
|
}
|