forked from gizak/termui
/
tabpane.go
265 lines (232 loc) · 5.65 KB
/
tabpane.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package extra
import (
"unicode/utf8"
. "github.com/gizak/termui"
)
type Tab struct {
Label string
RuneLen int
Blocks []Bufferer
}
func NewTab(label string) *Tab {
return &Tab{
Label: label,
RuneLen: utf8.RuneCount([]byte(label))}
}
func (tab *Tab) AddBlocks(rs ...Bufferer) {
for _, r := range rs {
tab.Blocks = append(tab.Blocks, r)
}
}
func (tab *Tab) Buffer() Buffer {
buf := NewBuffer()
for blockNum := 0; blockNum < len(tab.Blocks); blockNum++ {
b := tab.Blocks[blockNum]
buf.Merge(b.Buffer())
}
return buf
}
type Tabpane struct {
Block
Tabs []Tab
activeTabIndex int
ActiveTabBg Attribute
posTabText []int
offTabText int
}
func NewTabpane() *Tabpane {
tp := Tabpane{
Block: *NewBlock(),
activeTabIndex: 0,
offTabText: 0,
ActiveTabBg: ThemeAttr("bg.tab.active")}
return &tp
}
func (tp *Tabpane) SetTabs(tabs ...Tab) {
tp.Tabs = make([]Tab, len(tabs))
tp.posTabText = make([]int, len(tabs)+1)
off := 0
for i := 0; i < len(tp.Tabs); i++ {
tp.Tabs[i] = tabs[i]
tp.posTabText[i] = off
off += tp.Tabs[i].RuneLen + 1 //+1 for space between tabs
}
tp.posTabText[len(tabs)] = off - 1 //total length of Tab's text
}
func (tp *Tabpane) SetActiveLeft() {
if tp.activeTabIndex == 0 {
return
}
tp.activeTabIndex -= 1
if tp.posTabText[tp.activeTabIndex] < tp.offTabText {
tp.offTabText = tp.posTabText[tp.activeTabIndex]
}
}
func (tp *Tabpane) SetActiveRight() {
if tp.activeTabIndex == len(tp.Tabs)-1 {
return
}
tp.activeTabIndex += 1
endOffset := tp.posTabText[tp.activeTabIndex] + tp.Tabs[tp.activeTabIndex].RuneLen
if endOffset+tp.offTabText > tp.InnerWidth() {
tp.offTabText = endOffset - tp.InnerWidth()
}
}
// Checks if left and right tabs are fully visible
// if only left tabs are not visible return -1
// if only right tabs are not visible return 1
// if both return 0
// use only if fitsWidth() returns false
func (tp *Tabpane) checkAlignment() int {
ret := 0
if tp.offTabText > 0 {
ret = -1
}
if tp.offTabText+tp.InnerWidth() < tp.posTabText[len(tp.Tabs)] {
ret += 1
}
return ret
}
// Checks if all tabs fits innerWidth of Tabpane
func (tp *Tabpane) fitsWidth() bool {
return tp.InnerWidth() >= tp.posTabText[len(tp.Tabs)]
}
func (tp *Tabpane) align() {
if !tp.fitsWidth() && !tp.Border {
tp.PaddingLeft += 1
tp.PaddingRight += 1
tp.Block.Align()
}
}
// bridge the old Point stuct
type point struct {
X int
Y int
Ch rune
Fg Attribute
Bg Attribute
}
func buf2pt(b Buffer) []point {
ps := make([]point, 0, len(b.CellMap))
for k, c := range b.CellMap {
ps = append(ps, point{X: k.X, Y: k.Y, Ch: c.Ch, Fg: c.Fg, Bg: c.Bg})
}
return ps
}
// Adds the point only if it is visible in Tabpane.
// Point can be invisible if concatenation of Tab's texts is widther then
// innerWidth of Tabpane
func (tp *Tabpane) addPoint(ptab []point, charOffset *int, oftX *int, points ...point) []point {
if *charOffset < tp.offTabText || tp.offTabText+tp.InnerWidth() < *charOffset {
*charOffset++
return ptab
}
for _, p := range points {
p.X = *oftX
ptab = append(ptab, p)
}
*oftX++
*charOffset++
return ptab
}
// Draws the point and redraws upper and lower border points (if it has one)
func (tp *Tabpane) drawPointWithBorder(p point, ch rune, chbord rune, chdown rune, chup rune) []point {
var addp []point
p.Ch = ch
if tp.Border {
p.Ch = chdown
p.Y = tp.InnerY() - 1
addp = append(addp, p)
p.Ch = chup
p.Y = tp.InnerY() + 1
addp = append(addp, p)
p.Ch = chbord
}
p.Y = tp.InnerY()
return append(addp, p)
}
func (tp *Tabpane) Buffer() Buffer {
if tp.Border {
tp.Height = 3
} else {
tp.Height = 1
}
if tp.Width > tp.posTabText[len(tp.Tabs)]+2 {
tp.Width = tp.posTabText[len(tp.Tabs)] + 2
}
buf := tp.Block.Buffer()
ps := []point{}
tp.align()
if tp.InnerHeight() <= 0 || tp.InnerWidth() <= 0 {
return NewBuffer()
}
oftX := tp.InnerX()
charOffset := 0
pt := point{Bg: tp.BorderBg, Fg: tp.BorderFg}
for i, tab := range tp.Tabs {
if i != 0 {
pt.X = oftX
pt.Y = tp.InnerY()
addp := tp.drawPointWithBorder(pt, ' ', VERTICAL_LINE, HORIZONTAL_DOWN, HORIZONTAL_UP)
ps = tp.addPoint(ps, &charOffset, &oftX, addp...)
}
if i == tp.activeTabIndex {
pt.Bg = tp.ActiveTabBg
}
rs := []rune(tab.Label)
for k := 0; k < len(rs); k++ {
addp := make([]point, 0, 2)
if i == tp.activeTabIndex && tp.Border {
pt.Ch = ' '
pt.Y = tp.InnerY() + 1
pt.Bg = tp.BorderBg
addp = append(addp, pt)
pt.Bg = tp.ActiveTabBg
}
pt.Y = tp.InnerY()
pt.Ch = rs[k]
addp = append(addp, pt)
ps = tp.addPoint(ps, &charOffset, &oftX, addp...)
}
pt.Bg = tp.BorderBg
if !tp.fitsWidth() {
all := tp.checkAlignment()
pt.X = tp.InnerX() - 1
pt.Ch = '*'
if tp.Border {
pt.Ch = VERTICAL_LINE
}
ps = append(ps, pt)
if all <= 0 {
addp := tp.drawPointWithBorder(pt, '<', '«', HORIZONTAL_LINE, HORIZONTAL_LINE)
ps = append(ps, addp...)
}
pt.X = tp.InnerX() + tp.InnerWidth()
pt.Ch = '*'
if tp.Border {
pt.Ch = VERTICAL_LINE
}
ps = append(ps, pt)
if all >= 0 {
addp := tp.drawPointWithBorder(pt, '>', '»', HORIZONTAL_LINE, HORIZONTAL_LINE)
ps = append(ps, addp...)
}
}
//draw tab content below the Tabpane
if i == tp.activeTabIndex {
blockPoints := buf2pt(tab.Buffer())
for i := 0; i < len(blockPoints); i++ {
blockPoints[i].Y += tp.Height + tp.Y
}
ps = append(ps, blockPoints...)
}
}
for _, v := range ps {
buf.Set(v.X, v.Y, NewCell(v.Ch, v.Fg, v.Bg))
}
buf.Sync()
return buf
}