]> git.lizzy.rs Git - micro.git/blob - cmd/micro/tab.go
Update docs and readme
[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         t.Resize()
41
42         return t
43 }
44
45 // SetNum sets all this tab's views to have the correct tab number
46 func (t *Tab) SetNum(num int) {
47         t.tree.tabNum = num
48         for _, v := range t.views {
49                 v.TabNum = num
50         }
51 }
52
53 func (t *Tab) Cleanup() {
54         t.tree.Cleanup()
55 }
56
57 func (t *Tab) Resize() {
58         w, h := screen.Size()
59         t.tree.width = w
60         t.tree.height = h
61
62         if globalSettings["infobar"].(bool) {
63                 t.tree.height--
64         }
65
66         t.tree.ResizeSplits()
67 }
68
69 // CurView returns the current view
70 func CurView() *View {
71         curTab := tabs[curTab]
72         return curTab.views[curTab.curView]
73 }
74
75 // TabbarString returns the string that should be displayed in the tabbar
76 // It also returns a map containing which indicies correspond to which tab number
77 // This is useful when we know that the mouse click has occurred at an x location
78 // but need to know which tab that corresponds to to accurately change the tab
79 func TabbarString() (string, map[int]int) {
80         str := ""
81         indicies := make(map[int]int)
82         for i, t := range tabs {
83                 if i == curTab {
84                         str += "["
85                 } else {
86                         str += " "
87                 }
88                 str += t.views[t.curView].Buf.Name
89                 if i == curTab {
90                         str += "]"
91                 } else {
92                         str += " "
93                 }
94                 indicies[len(str)-1] = i + 1
95                 str += " "
96         }
97         return str, indicies
98 }
99
100 // TabbarHandleMouseEvent checks the given mouse event if it is clicking on the tabbar
101 // If it is it changes the current tab accordingly
102 // This function returns true if the tab is changed
103 func TabbarHandleMouseEvent(event tcell.Event) bool {
104         // There is no tabbar displayed if there are less than 2 tabs
105         if len(tabs) <= 1 {
106                 return false
107         }
108
109         switch e := event.(type) {
110         case *tcell.EventMouse:
111                 button := e.Buttons()
112                 // Must be a left click
113                 if button == tcell.Button1 {
114                         x, y := e.Position()
115                         if y != 0 {
116                                 return false
117                         }
118                         str, indicies := TabbarString()
119                         if x >= len(str) {
120                                 return false
121                         }
122                         var tabnum int
123                         var keys []int
124                         for k := range indicies {
125                                 keys = append(keys, k)
126                         }
127                         sort.Ints(keys)
128                         for _, k := range keys {
129                                 if x <= k {
130                                         tabnum = indicies[k] - 1
131                                         break
132                                 }
133                         }
134                         curTab = tabnum
135                         return true
136                 }
137         }
138
139         return false
140 }
141
142 // DisplayTabs displays the tabbar at the top of the editor if there are multiple tabs
143 func DisplayTabs() {
144         if len(tabs) <= 1 {
145                 return
146         }
147
148         str, _ := TabbarString()
149
150         tabBarStyle := defStyle.Reverse(true)
151         if style, ok := colorscheme["tabbar"]; ok {
152                 tabBarStyle = style
153         }
154
155         // Maybe there is a unicode filename?
156         fileRunes := []rune(str)
157         w, _ := screen.Size()
158         for x := 0; x < w; x++ {
159                 if x < len(fileRunes) {
160                         screen.SetContent(x, 0, fileRunes[x], nil, tabBarStyle)
161                 } else {
162                         screen.SetContent(x, 0, ' ', nil, tabBarStyle)
163                 }
164         }
165 }