Repository navigation
Expand file tree
/
Copy pathstatusline.patch
More file actions
275 lines (265 loc) · 7.98 KB
/
Copy pathstatusline.patch
File metadata and controls
275 lines (265 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
--- a/internal/display/statusline.go
+++ b/internal/display/statusline.go
@@ -1,7 +1,14 @@
+// patched internal/display/statusline.go
+// Drop this file into micro/internal/display/ and run: go build ./...
+//
+// Changes vs upstream master:
+// - Add $(#colorname) style-switch directive to the format string grammar.
+// - Replace the flat []byte rendering pass with a span-based pipeline so
+// each segment can carry its own tcell.Style.
+// - Backward-compatible: format strings without $(#...) behave identically.
package display
import (
- "bytes"
"fmt"
"regexp"
"strconv"
@@ -15,6 +22,7 @@
ulua "github.com/micro-editor/micro/v2/internal/lua"
"github.com/micro-editor/micro/v2/internal/screen"
"github.com/micro-editor/micro/v2/internal/util"
+ "github.com/micro-editor/tcell/v2"
lua "github.com/yuin/gopher-lua"
)
@@ -28,6 +36,55 @@
win *BufWindow
}
+// statusSpan is a run of resolved text that shares a single tcell style.
+type statusSpan struct {
+ text string
+ style tcell.Style
+}
+
+// spanIterator walks a []statusSpan, yielding one decoded rune at a time along
+// with its combining runes and the style of the span it came from.
+type spanIterator struct {
+ spans []statusSpan
+ spanIdx int
+ data []byte
+}
+
+func newSpanIterator(spans []statusSpan) *spanIterator {
+ it := &spanIterator{spans: spans}
+ if len(spans) > 0 {
+ it.data = []byte(spans[0].text)
+ }
+ return it
+}
+
+// Next returns the next rune, its combining runes, and its style.
+// ok is false when the iterator is exhausted.
+func (it *spanIterator) Next() (r rune, combc []rune, style tcell.Style, ok bool) {
+ for it.spanIdx < len(it.spans) {
+ if len(it.data) > 0 {
+ r, combc, size := util.DecodeCharacter(it.data)
+ it.data = it.data[size:]
+ return r, combc, it.spans[it.spanIdx].style, true
+ }
+ it.spanIdx++
+ if it.spanIdx < len(it.spans) {
+ it.data = []byte(it.spans[it.spanIdx].text)
+ }
+ }
+ return 0, nil, config.DefStyle, false
+}
+
+// calcSpansWidth returns the total terminal display-column width of a span slice.
+func calcSpansWidth(spans []statusSpan) int {
+ total := 0
+ for _, sp := range spans {
+ b := []byte(sp.text)
+ total += util.StringWidth(b, util.CharacterCount(b), 1)
+ }
+ return total
+}
+
var statusInfo = map[string]func(*buffer.Buffer) string{
"filename": func(b *buffer.Buffer) string {
return b.GetName()
@@ -105,15 +162,96 @@
var formatParser = regexp.MustCompile(`\$\(.+?\)`)
+// resolveFormat expands a statusline format string into a slice of styled text
+// spans. Standard $(token) placeholders are supported as before. The new
+// $(#colorname) directive switches the active render style to
+// config.Colorscheme[colorname] for all subsequent text. $(#) resets to
+// baseStyle. Unrecognised colornames are silently ignored (style unchanged).
+func (s *StatusLine) resolveFormat(format string, baseStyle tcell.Style) []statusSpan {
+ var spans []statusSpan
+ currentStyle := baseStyle
+
+ // appendText adds text under the current style, merging into the last span
+ // when its style matches to avoid unnecessary allocations.
+ appendText := func(text string) {
+ if text == "" {
+ return
+ }
+ if len(spans) > 0 && spans[len(spans)-1].style == currentStyle {
+ spans[len(spans)-1].text += text
+ } else {
+ spans = append(spans, statusSpan{text: text, style: currentStyle})
+ }
+ }
+
+ indices := formatParser.FindAllStringIndex(format, -1)
+ prevEnd := 0
+
+ for _, idx := range indices {
+ start, end := idx[0], idx[1]
+
+ // Literal text that precedes this token
+ if start > prevEnd {
+ appendText(format[prevEnd:start])
+ }
+
+ token := format[start+2 : end-1] // strip "$(" prefix and ")" suffix
+
+ switch {
+ case strings.HasPrefix(token, "#"):
+ // Style-switch directive — emits no text.
+ name := token[1:]
+ if name == "" {
+ currentStyle = baseStyle
+ } else if style, ok := config.Colorscheme[name]; ok {
+ currentStyle = style
+ }
+ // Unknown names are intentionally ignored so typos don't crash.
+
+ case strings.HasPrefix(token, "opt:"):
+ appendText(fmt.Sprint(s.FindOpt(token[4:])))
+
+ case strings.HasPrefix(token, "bind:"):
+ binding := token[5:]
+ found := false
+ for k, v := range config.Bindings["buffer"] {
+ if v == binding {
+ appendText(k)
+ found = true
+ break
+ }
+ }
+ if !found {
+ appendText("null")
+ }
+
+ default:
+ if fn, ok := statusInfo[token]; ok {
+ appendText(fn(s.win.Buf))
+ }
+ }
+
+ prevEnd = end
+ }
+
+ // Trailing literal text after the last token
+ if prevEnd < len(format) {
+ appendText(format[prevEnd:])
+ }
+
+ return spans
+}
+
// Display draws the statusline to the screen
func (s *StatusLine) Display() {
- // We'll draw the line at the lowest line in the window
+ // Draw at the lowest row of the window
y := s.win.Height + s.win.Y - 1
winX := s.win.X
b := s.win.Buf
- // autocomplete suggestions (for the buffer, not for the infowindow)
+
+ // Autocomplete suggestions (for the buffer, not for the info window)
if b.HasSuggestions && len(b.Suggestions) > 1 {
statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline.suggestions"]; ok {
@@ -148,52 +286,32 @@
return
}
- formatter := func(match []byte) []byte {
- name := match[2 : len(match)-1]
- if bytes.HasPrefix(name, []byte("opt")) {
- option := name[4:]
- return fmt.Append(nil, s.FindOpt(string(option)))
- } else if bytes.HasPrefix(name, []byte("bind")) {
- binding := string(name[5:])
- for k, v := range config.Bindings["buffer"] {
- if v == binding {
- return []byte(k)
- }
- }
- return []byte("null")
- } else {
- if fn, ok := statusInfo[string(name)]; ok {
- return []byte(fn(s.win.Buf))
- }
- return []byte{}
- }
- }
-
- leftText := []byte(s.win.Buf.Settings["statusformatl"].(string))
- leftText = formatParser.ReplaceAllFunc(leftText, formatter)
- rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
- rightText = formatParser.ReplaceAllFunc(rightText, formatter)
-
- statusLineStyle := config.DefStyle.Reverse(true)
+ // Determine the base style for the active or inactive window.
+ baseStyle := config.DefStyle.Reverse(true)
if s.win.IsActive() {
if style, ok := config.Colorscheme["statusline"]; ok {
- statusLineStyle = style
+ baseStyle = style
}
} else {
if style, ok := config.Colorscheme["statusline.inactive"]; ok {
- statusLineStyle = style
+ baseStyle = style
} else if style, ok := config.Colorscheme["statusline"]; ok {
- statusLineStyle = style
+ baseStyle = style
}
}
- leftLen := util.StringWidth(leftText, util.CharacterCount(leftText), 1)
- rightLen := util.StringWidth(rightText, util.CharacterCount(rightText), 1)
+ leftSpans := s.resolveFormat(b.Settings["statusformatl"].(string), baseStyle)
+ rightSpans := s.resolveFormat(b.Settings["statusformatr"].(string), baseStyle)
+
+ leftLen := calcSpansWidth(leftSpans)
+ rightLen := calcSpansWidth(rightSpans)
+
+ leftIt := newSpanIterator(leftSpans)
+ rightIt := newSpanIterator(rightSpans)
for x := 0; x < s.win.Width; x++ {
if x < leftLen {
- r, combc, size := util.DecodeCharacter(leftText)
- leftText = leftText[size:]
+ r, combc, style, _ := leftIt.Next()
rw := runewidth.RuneWidth(r)
for j := 0; j < rw; j++ {
c := r
@@ -202,11 +320,10 @@
combc = nil
x++
}
- screen.SetContent(winX+x, y, c, combc, statusLineStyle)
+ screen.SetContent(winX+x, y, c, combc, style)
}
} else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen {
- r, combc, size := util.DecodeCharacter(rightText)
- rightText = rightText[size:]
+ r, combc, style, _ := rightIt.Next()
rw := runewidth.RuneWidth(r)
for j := 0; j < rw; j++ {
c := r
@@ -215,10 +332,10 @@
combc = nil
x++
}
- screen.SetContent(winX+x, y, c, combc, statusLineStyle)
+ screen.SetContent(winX+x, y, c, combc, style)
}
} else {
- screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
+ screen.SetContent(winX+x, y, ' ', nil, baseStyle)
}
}
}