forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel.go
142 lines (121 loc) · 3.05 KB
/
label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package clui
import (
xs "github.com/huandu/xstrings"
)
/*
Label is a decorative control that can display text in horizontal
or vertical direction. Other available text features are alignment
and multi-line ability. Text can be single- or multi-colored with
tags inside the text. Multi-colored strings have limited support
of alignment feature: if text is longer than Label width the text
is always left aligned
*/
type Label struct {
BaseControl
direction Direction
multiline bool
}
/*
NewLabel creates a new label.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
w and h - are minimal size of the control.
title - is Label title.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
c := new(Label)
if w == AutoSize {
w = xs.Len(title)
}
if h == AutoSize {
h = 1
}
c.parent = parent
c.SetTitle(title)
c.SetSize(w, h)
c.SetConstraints(w, h)
c.SetScale(scale)
c.tabSkip = true
if parent != nil {
parent.AddChild(c)
}
return c
}
// Direction returns direction of text output: vertical or horizontal
func (l *Label) Direction() Direction {
return l.direction
}
// SetDirection sets the text output direction
func (l *Label) SetDirection(dir Direction) {
l.direction = dir
}
func (l *Label) Draw() {
if l.hidden {
return
}
PushAttributes()
defer PopAttributes()
fg, bg := RealColor(l.fg, ColorText), RealColor(l.bg, ColorBack)
if !l.Enabled() {
fg = RealColor(l.fg, ColorDisabledText)
}
SetTextColor(fg)
SetBackColor(bg)
FillRect(l.x, l.y, l.width, l.height, ' ')
if l.title == "" {
return
}
if l.multiline {
parser := NewColorParser(l.title, ColorWhite, ColorBlack)
elem := parser.NextElement()
xx, yy := l.x, l.y
for elem.Type != ElemEndOfText {
if xx >= l.x+l.width || yy >= l.y+l.height {
break
}
if elem.Type == ElemLineBreak {
xx = l.x
yy += 1
} else if elem.Type == ElemPrintable {
SetTextColor(elem.Fg)
SetBackColor(elem.Bg)
putCharUnsafe(xx, yy, elem.Ch)
if l.direction == Horizontal {
xx += 1
if xx >= l.x+l.width {
xx = l.x
yy += 1
}
} else {
yy += 1
if yy >= l.y+l.height {
yy = l.y
xx += 1
}
}
}
elem = parser.NextElement()
}
} else {
if l.direction == Horizontal {
shift, str := AlignColorizedText(l.title, l.width, l.align)
DrawText(l.x+shift, l.y, str)
} else {
shift, str := AlignColorizedText(l.title, l.height, l.align)
DrawTextVertical(l.x, l.y+shift, str)
}
}
}
// Multiline returns if text is displayed on several lines if the
// label title is longer than label width or title contains
// line breaks
func (l *Label) Multiline() bool {
return l.multiline
}
// SetMultiline sets if the label should output text as one line
// or automatically display it in several lines
func (l *Label) SetMultiline(multi bool) {
l.multiline = multi
}