6 "github.com/zyedidia/tcell"
12 // This contains all the views in this tab
13 // There is generally only one view per tab, but you can have
14 // multiple views with splits
16 // This is the current view for this tab
22 // NewTabFromView creates a new tab and puts the given view in the tab
23 func NewTabFromView(v *View) *Tab {
25 t.views = append(t.views, v)
28 t.tree = new(SplitTree)
29 t.tree.kind = VerticalSplit
30 t.tree.children = []Node{NewLeafNode(t.views[0], t.tree)}
36 if globalSettings["infobar"].(bool) {
45 // SetNum sets all this tab's views to have the correct tab number
46 func (t *Tab) SetNum(num int) {
48 for _, v := range t.views {
53 func (t *Tab) Cleanup() {
57 func (t *Tab) Resize() {
62 if globalSettings["infobar"].(bool) {
68 for i, v := range t.views {
73 // CurView returns the current view
74 func CurView() *View {
75 curTab := tabs[curTab]
76 return curTab.views[curTab.CurView]
79 // TabbarString returns the string that should be displayed in the tabbar
80 // It also returns a map containing which indicies correspond to which tab number
81 // This is useful when we know that the mouse click has occurred at an x location
82 // but need to know which tab that corresponds to to accurately change the tab
83 func TabbarString() (string, map[int]int) {
85 indicies := make(map[int]int)
86 for i, t := range tabs {
92 str += t.views[t.CurView].Buf.GetName()
98 indicies[len(str)-1] = i + 1
104 // TabbarHandleMouseEvent checks the given mouse event if it is clicking on the tabbar
105 // If it is it changes the current tab accordingly
106 // This function returns true if the tab is changed
107 func TabbarHandleMouseEvent(event tcell.Event) bool {
108 // There is no tabbar displayed if there are less than 2 tabs
113 switch e := event.(type) {
114 case *tcell.EventMouse:
115 button := e.Buttons()
116 // Must be a left click
117 if button == tcell.Button1 {
122 str, indicies := TabbarString()
123 if x+tabBarOffset >= len(str) {
128 for k := range indicies {
129 keys = append(keys, k)
132 for _, k := range keys {
133 if x+tabBarOffset <= k {
134 tabnum = indicies[k] - 1
146 // DisplayTabs displays the tabbar at the top of the editor if there are multiple tabs
152 str, indicies := TabbarString()
154 tabBarStyle := defStyle.Reverse(true)
155 if style, ok := colorscheme["tabbar"]; ok {
159 // Maybe there is a unicode filename?
160 fileRunes := []rune(str)
161 w, _ := screen.Size()
162 tooWide := (w < len(fileRunes))
164 // if the entire tab-bar is longer than the screen is wide,
165 // then it should be truncated appropriately to keep the
166 // active tab visible on the UI.
168 // first we have to work out where the selected tab is
169 // out of the total length of the tab bar. this is done
170 // by extracting the hit-areas from the indicies map
171 // that was constructed by `TabbarString()`
173 for offset := range indicies {
174 keys = append(keys, offset)
176 // sort them to be in ascending order so that values will
177 // correctly reflect the displayed ordering of the tabs
179 // record the offset of each tab and the previous tab so
180 // we can find the position of the tab's hit-box.
181 previousTabOffset := 0
182 currentTabOffset := 0
183 for _, k := range keys {
184 tabIndex := indicies[k] - 1
185 if tabIndex == curTab {
189 // this is +2 because there are two padding spaces that aren't accounted
190 // for in the display. please note that this is for cosmetic purposes only.
191 previousTabOffset = k + 2
193 // get the width of the hitbox of the active tab, from there calculate the offsets
194 // to the left and right of it to approximately center it on the tab bar display.
195 centeringOffset := (w - (currentTabOffset - previousTabOffset))
196 leftBuffer := previousTabOffset - (centeringOffset / 2)
197 rightBuffer := currentTabOffset + (centeringOffset / 2)
199 // check to make sure we haven't overshot the bounds of the string,
200 // if we have, then take that remainder and put it on the left side
201 overshotRight := rightBuffer - len(fileRunes)
202 if overshotRight > 0 {
203 leftBuffer = leftBuffer + overshotRight
206 overshotLeft := leftBuffer - 0
207 if overshotLeft < 0 {
209 rightBuffer = leftBuffer + (w - 1)
211 rightBuffer = leftBuffer + (w - 2)
214 if rightBuffer > len(fileRunes)-1 {
215 rightBuffer = len(fileRunes) - 1
218 // construct a new buffer of text to put the
219 // newly formatted tab bar text into.
220 var displayText []rune
222 // if the left-side of the tab bar isn't at the start
223 // of the constructed tab bar text, then show that are
224 // more tabs to the left by displaying a "+"
226 displayText = append(displayText, '+')
228 // copy the runes in from the original tab bar text string
229 // into the new display buffer
230 for x := leftBuffer; x < rightBuffer; x++ {
231 displayText = append(displayText, fileRunes[x])
233 // if there is more text to the right of the right-most
234 // column in the tab bar text, then indicate there are more
235 // tabs to the right by displaying a "+"
236 if rightBuffer < len(fileRunes)-1 {
237 displayText = append(displayText, '+')
240 // now store the offset from zero of the left-most text
241 // that is being displayed. This is to ensure that when
242 // clicking on the tab bar, the correct tab gets selected.
243 tabBarOffset = leftBuffer
245 // use the constructed buffer as the display buffer to print
247 fileRunes = displayText
252 // iterate over the width of the terminal display and for each column,
253 // write a character into the tab display area with the appropriate style.
254 for x := 0; x < w; x++ {
255 if x < len(fileRunes) {
256 screen.SetContent(x, 0, fileRunes[x], nil, tabBarStyle)
258 screen.SetContent(x, 0, ' ', nil, tabBarStyle)