Debian Codegraph @

internal/display/tabwindow.go

raw
1 package display
2
3 import (
4 runewidth "github.com/mattn/go-runewidth"
5 "github.com/micro-editor/tcell/v2"
6 "github.com/zyedidia/micro/v2/internal/buffer"
7 "github.com/zyedidia/micro/v2/internal/config"
8 "github.com/zyedidia/micro/v2/internal/screen"
9 "github.com/zyedidia/micro/v2/internal/util"
10 )
11
12 type TabWindow struct {
13 Names []string
14 active int
15 Y int
16 Width int
17 hscroll int
18 }
19
20 func NewTabWindow(w int, y int) *TabWindow {
21 tw := new(TabWindow)
22 tw.Width = w
23 tw.Y = y
24 return tw
25 }
26
27 func (w *TabWindow) Resize(width, height int) {
28 w.Width = width
29 }
30
31 func (w *TabWindow) LocFromVisual(vloc buffer.Loc) int {
32 x := -w.hscroll
33
34 for i, n := range w.Names {
35 x++
36 s := util.CharacterCountInString(n)
37 if vloc.Y == w.Y && vloc.X < x+s {
38 return i
39 }
40 x += s
41 x += 3
42 if x >= w.Width {
43 break
44 }
45 }
46 return -1
47 }
48
49 func (w *TabWindow) Scroll(amt int) {
50 w.hscroll += amt
51 s := w.TotalSize()
52 w.hscroll = util.Clamp(w.hscroll, 0, s-w.Width)
53
54 if s-w.Width <= 0 {
55 w.hscroll = 0
56 }
57 }
58
59 func (w *TabWindow) TotalSize() int {
60 sum := 2
61 for _, n := range w.Names {
62 sum += runewidth.StringWidth(n) + 4
63 }
64 return sum - 4
65 }
66
67 func (w *TabWindow) Active() int {
68 return w.active
69 }
70
71 func (w *TabWindow) SetActive(a int) {
72 w.active = a
73 x := 2
74 s := w.TotalSize()
75
76 for i, n := range w.Names {
77 c := util.CharacterCountInString(n)
78 if i == a {
79 if x+c >= w.hscroll+w.Width {
80 w.hscroll = util.Clamp(x+c+1-w.Width, 0, s-w.Width)
81 } else if x < w.hscroll {
82 w.hscroll = util.Clamp(x-4, 0, s-w.Width)
83 }
84 break
85 }
86 x += c + 4
87 }
88
89 if s-w.Width <= 0 {
90 w.hscroll = 0
91 }
92 }
93
94 func (w *TabWindow) Display() {
95 x := -w.hscroll
96 done := false
97
98 globalTabReverse := config.GetGlobalOption("tabreverse").(bool)
99 globalTabHighlight := config.GetGlobalOption("tabhighlight").(bool)
100
101 // xor of reverse and tab highlight to get tab character (as in filename and surrounding characters) reverse state
102 tabCharHighlight := (globalTabReverse || globalTabHighlight) && !(globalTabReverse && globalTabHighlight)
103
104 reverseStyles := func(reverse bool) (tcell.Style, tcell.Style) {
105 tabBarStyle := config.DefStyle.Reverse(reverse)
106 if style, ok := config.Colorscheme["tabbar"]; ok {
107 tabBarStyle = style
108 }
109 tabBarActiveStyle := tabBarStyle
110 if style, ok := config.Colorscheme["tabbar.active"]; ok {
111 tabBarActiveStyle = style
112 }
113 return tabBarStyle, tabBarActiveStyle
114 }
115
116 draw := func(r rune, n int, active bool, reversed bool) {
117 tabBarStyle, tabBarActiveStyle := reverseStyles(reversed)
118
119 style := tabBarStyle
120 if active {
121 style = tabBarActiveStyle
122 }
123 for i := 0; i < n; i++ {
124 rw := runewidth.RuneWidth(r)
125 for j := 0; j < rw; j++ {
126 c := r
127 if j > 0 {
128 c = ' '
129 }
130 if x == w.Width-1 && !done {
131 screen.SetContent(w.Width-1, w.Y, '>', nil, tabBarStyle)
132 x++
133 break
134 } else if x == 0 && w.hscroll > 0 {
135 screen.SetContent(0, w.Y, '<', nil, tabBarStyle)
136 } else if x >= 0 && x < w.Width {
137 screen.SetContent(x, w.Y, c, nil, style)
138 }
139 x++
140 }
141 }
142 }
143
144 for i, n := range w.Names {
145 if i == w.active {
146 draw('[', 1, true, tabCharHighlight)
147 } else {
148 draw(' ', 1, false, tabCharHighlight)
149 }
150
151 for _, c := range n {
152 draw(c, 1, i == w.active, tabCharHighlight)
153 }
154
155 if i == len(w.Names)-1 {
156 done = true
157 }
158
159 if i == w.active {
160 draw(']', 1, true, tabCharHighlight)
161 draw(' ', 2, true, globalTabReverse)
162 } else {
163 draw(' ', 1, false, tabCharHighlight)
164 draw(' ', 2, false, globalTabReverse)
165 }
166
167 if x >= w.Width {
168 break
169 }
170 }
171
172 if x < w.Width {
173 draw(' ', w.Width-x, false, globalTabReverse)
174 }
175 }