]> git.lizzy.rs Git - micro.git/blob - cmd/micro/tab.go
Fix replace cursor relocation
[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
20 func NewTabFromView(v *View) *Tab {
21         t := new(Tab)
22         t.views = append(t.views, v)
23         t.views[0].Num = 0
24         return t
25 }
26
27 func (t *Tab) SetNum(num int) {
28         for _, v := range t.views {
29                 v.TabNum = num
30         }
31 }
32
33 // CurView returns the current view
34 func CurView() *View {
35         curTab := tabs[curTab]
36         return curTab.views[curTab.curView]
37 }
38
39 func TabbarString() (string, map[int]int) {
40         str := ""
41         indicies := make(map[int]int)
42         for i, t := range tabs {
43                 if i == curTab {
44                         str += "["
45                 } else {
46                         str += " "
47                 }
48                 str += t.views[t.curView].Buf.Name
49                 if i == curTab {
50                         str += "]"
51                 } else {
52                         str += " "
53                 }
54                 indicies[len(str)-1] = i + 1
55                 str += " "
56         }
57         return str, indicies
58 }
59
60 func TabbarHandleMouseEvent(event tcell.Event) bool {
61         if len(tabs) <= 1 {
62                 return false
63         }
64
65         switch e := event.(type) {
66         case *tcell.EventMouse:
67                 button := e.Buttons()
68                 if button == tcell.Button1 {
69                         x, y := e.Position()
70                         if y != 0 {
71                                 return false
72                         }
73                         str, indicies := TabbarString()
74                         if x >= len(str) {
75                                 return false
76                         }
77                         var tabnum int
78                         var keys []int
79                         for k := range indicies {
80                                 keys = append(keys, k)
81                         }
82                         sort.Ints(keys)
83                         for _, k := range keys {
84                                 if x <= k {
85                                         tabnum = indicies[k] - 1
86                                         break
87                                 }
88                         }
89                         curTab = tabnum
90                         return true
91                 }
92         }
93
94         return false
95 }
96
97 func DisplayTabs() {
98         if len(tabs) <= 1 {
99                 return
100         }
101
102         str, _ := TabbarString()
103
104         tabBarStyle := defStyle.Reverse(true)
105         if style, ok := colorscheme["tabbar"]; ok {
106                 tabBarStyle = style
107         }
108
109         // Maybe there is a unicode filename?
110         fileRunes := []rune(str)
111         w, _ := screen.Size()
112         for x := 0; x < w; x++ {
113                 if x < len(fileRunes) {
114                         screen.SetContent(x, 0, fileRunes[x], nil, tabBarStyle)
115                 } else {
116                         screen.SetContent(x, 0, ' ', nil, tabBarStyle)
117                 }
118         }
119 }