]> git.lizzy.rs Git - micro.git/blob - cmd/micro/tab.go
Add infobar option to disable the message line
[micro.git] / cmd / micro / tab.go
1 package main
2
3 import (
4         "sort"
5
6         "github.com/zyedidia/tcell"
7 )
8
9 type Tab struct {
10         // This contains all the views in this tab
11         // There is generally only one view per tab, but you can have
12         // multiple views with splits
13         views []*View
14         // This is the current view for this tab
15         curView int
16         // Generally this is the name of the current view's buffer
17         name string
18
19         tree *SplitTree
20 }
21
22 // NewTabFromView creates a new tab and puts the given view in the tab
23 func NewTabFromView(v *View) *Tab {
24         t := new(Tab)
25         t.views = append(t.views, v)
26         t.views[0].Num = 0
27
28         t.tree = new(SplitTree)
29         t.tree.kind = VerticalSplit
30         t.tree.children = []Node{NewLeafNode(t.views[0], t.tree)}
31
32         w, h := screen.Size()
33         t.tree.width = w
34         t.tree.height = h
35
36         if globalSettings["infobar"].(bool) {
37                 t.tree.height--
38         }
39
40         return t
41 }
42
43 // SetNum sets all this tab's views to have the correct tab number
44 func (t *Tab) SetNum(num int) {
45         for _, v := range t.views {
46                 v.TabNum = num
47         }
48 }
49
50 func (t *Tab) Cleanup() {
51         t.tree.Cleanup()
52 }
53
54 func (t *Tab) Resize() {
55         w, h := screen.Size()
56         t.tree.width = w
57         t.tree.height = h
58
59         if globalSettings["infobar"].(bool) {
60                 t.tree.height--
61         }
62
63         t.tree.ResizeSplits()
64 }
65
66 // CurView returns the current view
67 func CurView() *View {
68         curTab := tabs[curTab]
69         return curTab.views[curTab.curView]
70 }
71
72 // TabbarString returns the string that should be displayed in the tabbar
73 // It also returns a map containing which indicies correspond to which tab number
74 // This is useful when we know that the mouse click has occurred at an x location
75 // but need to know which tab that corresponds to to accurately change the tab
76 func TabbarString() (string, map[int]int) {
77         str := ""
78         indicies := make(map[int]int)
79         for i, t := range tabs {
80                 if i == curTab {
81                         str += "["
82                 } else {
83                         str += " "
84                 }
85                 str += t.views[t.curView].Buf.Name
86                 if i == curTab {
87                         str += "]"
88                 } else {
89                         str += " "
90                 }
91                 indicies[len(str)-1] = i + 1
92                 str += " "
93         }
94         return str, indicies
95 }
96
97 // TabbarHandleMouseEvent checks the given mouse event if it is clicking on the tabbar
98 // If it is it changes the current tab accordingly
99 // This function returns true if the tab is changed
100 func TabbarHandleMouseEvent(event tcell.Event) bool {
101         // There is no tabbar displayed if there are less than 2 tabs
102         if len(tabs) <= 1 {
103                 return false
104         }
105
106         switch e := event.(type) {
107         case *tcell.EventMouse:
108                 button := e.Buttons()
109                 // Must be a left click
110                 if button == tcell.Button1 {
111                         x, y := e.Position()
112                         if y != 0 {
113                                 return false
114                         }
115                         str, indicies := TabbarString()
116                         if x >= len(str) {
117                                 return false
118                         }
119                         var tabnum int
120                         var keys []int
121                         for k := range indicies {
122                                 keys = append(keys, k)
123                         }
124                         sort.Ints(keys)
125                         for _, k := range keys {
126                                 if x <= k {
127                                         tabnum = indicies[k] - 1
128                                         break
129                                 }
130                         }
131                         curTab = tabnum
132                         return true
133                 }
134         }
135
136         return false
137 }
138
139 // DisplayTabs displays the tabbar at the top of the editor if there are multiple tabs
140 func DisplayTabs() {
141         if len(tabs) <= 1 {
142                 return
143         }
144
145         str, _ := TabbarString()
146
147         tabBarStyle := defStyle.Reverse(true)
148         if style, ok := colorscheme["tabbar"]; ok {
149                 tabBarStyle = style
150         }
151
152         // Maybe there is a unicode filename?
153         fileRunes := []rune(str)
154         w, _ := screen.Size()
155         for x := 0; x < w; x++ {
156                 if x < len(fileRunes) {
157                         screen.SetContent(x, 0, fileRunes[x], nil, tabBarStyle)
158                 } else {
159                         screen.SetContent(x, 0, ' ', nil, tabBarStyle)
160                 }
161         }
162 }