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