-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.go
441 lines (390 loc) · 9.63 KB
/
tab.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"github.com/hanspr/tcell"
)
const (
bufDirty string = "✶"
tabOpen string = "|"
tabClose string = "|"
tabMenuSymbol string = "ɱ "
)
var tabBarOffset int
var toolBarOffset int
// -------------------------------------------
// Toolbar
// -------------------------------------------
// ToolBar structure for a toolbar
type ToolBar struct {
active bool
icons []rune
callback []func()
luacallback string
}
// NewToolBar create a new toolbar with predefined icons
func NewToolBar() *ToolBar {
t := new(ToolBar)
t.AddIcon('⌹', t.DirView, "")
t.AddIcon('↴', t.Save, "")
t.AddIcon('◨', t.VSplit, "")
t.AddIcon('⬓', t.HSplit, "")
t.AddIcon('🔎', t.Find, "")
t.AddIcon('℁', t.Replace, "")
t.AddIcon('↑', t.CloudUpload, "")
t.AddIcon('↓', t.CloudDownload, "")
t.AddIcon('❎', t.Quit, "")
t.active = true
return t
}
// AddIcon adds an icon to the toolbar
func (t *ToolBar) AddIcon(icon rune, cb func(), luacallback string) {
if cb == nil {
if luacallback == "" {
return
}
n := len(t.icons) - 1
cb = t.PluginCallBack
t.icons = append(t.icons, ' ')
copy(t.icons[n+1:], t.icons[n:])
t.icons[n] = icon
t.callback = append(t.callback, nil)
copy(t.callback[n+1:], t.callback[n:])
t.callback[n] = cb
t.luacallback = luacallback
} else {
t.icons = append(t.icons, icon)
t.callback = append(t.callback, cb)
}
}
// Runes convert toolbar into runes
func (t *ToolBar) Runes() []rune {
var str []rune
toolBarOffset = len(t.icons)*3 + 3
for _, ic := range t.icons {
str = append(str, ' ', ic, ' ')
}
str = append([]rune(tabMenuSymbol), str...)
return str
}
// PluginCallBack call back function for plugins
func (t *ToolBar) PluginCallBack() {
Call(t.luacallback)
}
// Save Save icon callback
func (t *ToolBar) Save() {
CurView().Buf.Save()
}
// VSplit icon callback
func (t *ToolBar) VSplit() {
CurView().VSplitBinding(true)
}
// HSplit icon callback
func (t *ToolBar) HSplit() {
CurView().HSplitBinding(true)
}
// Find icon callback
func (t *ToolBar) Find() {
CurView().FindDialog(true)
}
// Replace icon callback
func (t *ToolBar) Replace() {
CurView().SearchDialog(true)
}
// DirView icon callback
func (t *ToolBar) DirView() {
micromenu.DirTreeView()
}
// Void unimplemented
func (t *ToolBar) Void() {
}
// Quit icon callback
func (t *ToolBar) Quit() {
CurView().Quit(true)
}
// CloudUpload icon callback
func (t *ToolBar) CloudUpload() {
CurView().UploadToCloud(false)
}
// CloudDownload icon callback
func (t *ToolBar) CloudDownload() {
CurView().DownloadFromCloud(false)
}
// ToolbarHandleMouseEvent Handle ToolBarClick
func (t *ToolBar) ToolbarHandleMouseEvent(x int) {
var pos int
if !t.active {
return
}
pos = x / 3
t.callback[pos-1]()
}
// FixTabsIconArea fix the icon area filling with black background to avoid wide chars leaving transparent cells
func (t *ToolBar) FixTabsIconArea() {
// prefill the length with with black background spaces
var tStyle tcell.Style
if !t.active {
return
}
tStyle = StringToStyle("normal #000000,#000000")
for x := 0; x < toolBarOffset; x++ {
screen.SetContent(x, 0, '.', nil, tStyle)
}
screen.Show()
}
// -------------------------------------------
// Tabs
// -------------------------------------------
// A Tab holds an array of views and a splitTree to determine how the
// views should be arranged
type Tab struct {
// This contains all the views in this tab
// There is generally only one view per tab, but you can have
// multiple views with splits
Views []*View
// This is the current view for this tab
CurView int
tree *SplitTree
}
// NewTabFromView creates a new tab and puts the given view in the tab
func NewTabFromView(v *View) *Tab {
t := new(Tab)
t.Views = append(t.Views, v)
t.Views[0].Num = 0
t.tree = new(SplitTree)
t.tree.kind = VerticalSplit
t.tree.children = []Node{NewLeafNode(t.Views[0], t.tree)}
w, h := screen.Size()
t.tree.width = w
t.tree.height = h - 1
//t.tree.height--
t.Resize()
return t
}
// SetNum sets all this tab's views to have the correct tab number
func (t *Tab) SetNum(num int) {
t.tree.tabNum = num
for _, v := range t.Views {
v.TabNum = num
}
}
// Cleanup cleans up the tree (for example if views have closed)
func (t *Tab) Cleanup() {
t.tree.Cleanup()
}
// Resize handles a resize event from the terminal and resizes
// all child views correctly
func (t *Tab) Resize() {
w, h := screen.Size()
t.tree.width = w
t.tree.height = h - 1
//t.tree.height--
t.tree.ResizeSplits()
for i, v := range t.Views {
v.Num = i
if v.Type == vtTerm {
v.term.Resize(v.Width, v.Height)
}
}
MicroToolBar.FixTabsIconArea()
}
// CurView returns the current view
func CurView() *View {
if len(tabs) == 0 {
return nil
}
curTab := tabs[curTab]
return curTab.Views[curTab.CurView]
}
// TabbarHandleMouseEvent checks the given mouse event if it is clicking on the tabbar
// If it is it changes the current tab accordingly
// This function returns true if the tab is changed
func TabbarHandleMouseEvent(event tcell.Event) {
var tabnum int
toffset := toolBarOffset
if !MicroToolBar.active {
toffset = 0
}
if Mouse.Button == 1 || Mouse.Button == 3 {
x := Mouse.Pos.X
if toffset > 0 {
if x < 3 {
// Click on Menu Icon
micromenu.Menu()
return
}
if x < toffset {
// Click on Toolbar
if Mouse.Button == 1 {
MicroToolBar.ToolbarHandleMouseEvent(x)
}
return
}
}
// Get the indices to know the hotspot for each tab
str, indicies := TabbarString(toffset)
// ignore if past last tab
if x >= Count(str)+toffset {
return
}
// Find which tab was clicked
for i := range tabs {
if x+tabBarOffset < indicies[i]+toffset {
tabnum = i
break
}
}
// Ignore on current tab and not to close
if Mouse.Button == 1 && curTab == tabnum {
return
}
// Change tab
if Mouse.Button == 1 {
// Left click = Select tab and display
curTab = tabnum
return
} else if Mouse.Button == 3 {
// We allow to close only the current Tab, so user knows what he is doing
if curTab == tabnum {
// Right click = Close selected tab
CurView().Unsplit(false)
CurView().Quit(false)
} else {
// If not current, make current so he can click again
curTab = tabnum
return
}
}
return
}
}
// -------------------------------------------
// Tabbar drawing
// -------------------------------------------
// TabbarString returns the string that should be displayed in the tabbar
// It also returns a map containing which indicies correspond to which tab number
// This is useful when we know that the mouse click has occurred at an x location
func TabbarString(toffset int) (string, map[int]int) {
var cv int
w, _ := screen.Size()
str := ""
middle := 0
tabIndex := 0 // Reference to the first tab being displayed
curTabDisplayed := false
indicies := make(map[int]int)
for i, t := range tabs {
buf := t.Views[t.CurView].Buf
name := buf.fname
if Count(str)+Count(name)+toffset+10 > w {
// Tabbar is longer than screen width
if middle == 0 {
middle = (i - tabIndex) / 2
}
if curTab > middle {
// We are past the midfdle of the tabbar, we have to shift everything to the left
// to leave some root to see next available tabs
offset := indicies[tabIndex]
olen := Count(str)
str = "←" + str[offset+1:]
diff := olen - Count(str)
for a := range tabs {
if a < tabIndex {
indicies[a] = 0
continue
} else if a > i {
break
}
indicies[a] -= diff
}
tabIndex++
}
if curTabDisplayed && curTab-tabIndex <= middle {
// We have the current tab on view and in the middle, no need to do more processing
str = str + "→ "
indicies[i] = Count(str)
break
}
}
cv = t.CurView
if i == curTab {
str += tabOpen + " "
curTabDisplayed = true
} else {
str += " "
}
str += buf.fname
if t.Views[cv].Type.Kind == 0 && buf.Modified() {
str += bufDirty
} else {
str += " "
}
if i == curTab {
str += tabClose
} else {
str += " "
}
indicies[i] = Count(str)
}
return str, indicies
}
// DisplayTabs Display a Menu, Icons ToolBar, and Tabs
func DisplayTabs() {
var tStyle tcell.Style
var tabActive = false
// Display ToolBar
w, _ := screen.Size()
toffset := 0
if w > 60 {
MicroToolBar.active = true
toffset = toolBarOffset
toolbarRunes := MicroToolBar.Runes()
for x := 0; x < len(toolbarRunes); x++ {
if x < 3 {
// Menu icon
if x == 0 {
tStyle = StringToStyle("bold #ffd700,#121212")
}
} else if x < toffset {
// Toolbar icons
if x == 3 {
tStyle = StringToStyle("#ffffff")
}
}
screen.SetContent(x, 0, toolbarRunes[x], nil, tStyle)
}
} else {
MicroToolBar.active = false
}
// Get the current Tabs to display
str, _ := TabbarString(toffset)
tabBarStyle := defStyle.Reverse(true)
if style, ok := colorscheme["tabbar"]; ok {
tabBarStyle = style
}
tabsRunes := []rune(str)
// Display Tabs
for x := 0; x < w; x++ {
if x < len(tabsRunes) {
if string(tabsRunes[x]) == tabOpen && !tabActive {
// Hightlight the current tab
tStyle = StringToStyle("bold #ffd700,#000087")
tabActive = true
} else if tabActive {
tStyle = StringToStyle("bold #ffd700,#000087")
if string(tabsRunes[x]) == bufDirty {
tStyle = tStyle.Foreground(tcell.ColorRed)
}
if string(tabsRunes[x]) == tabClose {
// Hightlight off, end of current tab
tabActive = false
}
} else {
tStyle = tabBarStyle
if string(tabsRunes[x]) == bufDirty {
tStyle = tStyle.Foreground(tcell.ColorRed)
}
}
screen.SetContent(x+toffset, 0, tabsRunes[x], nil, tStyle)
} else {
screen.SetContent(x+toffset, 0, ' ', nil, tabBarStyle)
}
}
}