-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
348 lines (309 loc) · 8.92 KB
/
main.go
File metadata and controls
348 lines (309 loc) · 8.92 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"os"
"regexp"
"strings"
"unicode/utf8"
"github.com/dotcommander/statusline/internal/config"
"github.com/dotcommander/statusline/internal/gitutil"
"github.com/mattn/go-runewidth"
)
// ─── Segment ───────────────────────────────────────────────────────────────
// Segment holds a single breadcrumb segment's content and foreground color.
type Segment struct {
Content string
FgAnsi string
}
// slot holds a deferred segment builder with its target line for declarative layout.
type slot struct {
line int // which output line (1, 2, ...)
width int // plain-text width for collapse calc
build func(active bool) Segment // lazy builder
noSep bool // skip separator before this slot
styleOverride string // pre-parsed ANSI from per-token style config
}
// ─── Segment Builders ──────────────────────────────────────────────────────
func dirSegment(dirName string, active bool) Segment {
fg := pal.muted
if active {
fg = pal.active
}
return Segment{
Content: pal.git + strings.ToLower(dirName),
FgAnsi: fg,
}
}
func gitSegment(git *gitutil.StatusResult, active bool, maxLen int) Segment {
fg := pal.muted
if active {
fg = pal.active
}
if git == nil {
return Segment{}
}
isClean := git.Staged == 0 && git.Unstaged == 0 && git.Untracked == 0
branchColor := pal.red
if isClean {
branchColor = pal.green
}
branchTextFg := pal.soft
if active {
branchTextFg = pal.active
}
branch := strings.ToLower(git.Branch)
if maxLen > 0 {
runes := []rune(branch)
if len(runes) > maxLen {
if maxLen > 3 {
branch = string(runes[:maxLen-3]) + "..."
} else {
branch = string(runes[:maxLen])
}
}
}
return Segment{
Content: branchColor + symBranch + " " + branchTextFg + branch,
FgAnsi: fg,
}
}
func projectSegment(project ProjectInfo, active bool) Segment {
fg := pal.soft
if active {
fg = pal.active
}
display := project.Badge
if project.Version != "" {
display = project.Badge + " " + project.Version
}
return Segment{
Content: fg + strings.ToLower(display),
FgAnsi: fg,
}
}
var (
claudeModelRe = regexp.MustCompile(`(?i)claude|opus|sonnet|haiku`)
opusRe = regexp.MustCompile(`(?i)opus`)
)
func modelSegment(modelName string, active bool) Segment {
fg := pal.muted
if active {
fg = pal.active
}
nameColor := fg
if claudeModelRe.MatchString(modelName) {
nameColor = pal.orange
if opusRe.MatchString(modelName) {
nameColor = pal.paleOrange
}
}
return Segment{
Content: nameColor + strings.ToLower(modelName),
FgAnsi: fg,
}
}
func contextSegment(ctx *ContextWindowData, maxOutputTokens int, active bool, ctxCfg *config.ContextConfig) (*Segment, ContextHealth) {
if ctx == nil || ctx.ContextWindowSize == nil || *ctx.ContextWindowSize == 0 {
return nil, HealthNone
}
used, rawPct := getEffectiveTokens(ctx, maxOutputTokens)
if used == 0 {
return nil, HealthNone
}
// Clamp to 0–100
remainingPct := max(min(rawPct, 100), 0)
health := contextHealth(remainingPct, ctxCfg)
isCritical := remainingPct <= ctxCfg.CriticalPct
isWarning := remainingPct <= ctxCfg.WarningPct
color := pal.green
if isCritical {
color = pal.red
} else if isWarning {
color = pal.yellow
}
fg := pal.muted
if active {
fg = pal.active
}
var statusText string
if remainingPct == 0 {
statusText = fg + "ctx:" + ansiBold + pal.red + formatTokens(used) + " !!!" + ansiReset
} else {
statusText = fg + "ctx:" + color + formatTokens(used)
}
return &Segment{
Content: statusText,
FgAnsi: fg,
}, health
}
func labelSegment(version string, active bool) Segment {
fg := pal.muted
if active {
fg = pal.active
}
content := pal.paleOrange + ansiBold + "cc"
if version != "" {
content += " " + pal.muted + version
}
content += ansiReset
return Segment{Content: content, FgAnsi: fg}
}
// ─── Collapse ──────────────────────────────────────────────────────────────
// plainLen returns the visible cell width of a string (excluding ANSI escapes).
// Uses go-runewidth so CJK and East Asian Ambiguous glyphs (●, ›, ⎇) are
// counted as the cells they actually occupy in the terminal.
func plainLen(s string) int {
n := 0
inEsc := false
for i := 0; i < len(s); {
if s[i] == '\x1b' {
inEsc = true
i++
continue
}
if inEsc {
if s[i] == 'm' {
inEsc = false
}
i++
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
n += runewidth.RuneWidth(r)
i += size
}
return n
}
// collapseBreadcrumbs drops slots from the right until the total
// width fits within termWidth. The first slot is always kept.
func collapseBreadcrumbs(slots []slot, termWidth, separatorPlainWidth, dotWidth int) []slot {
if len(slots) == 0 {
return slots
}
totalWidth := func(ss []slot) int {
w := dotWidth
for i, s := range ss {
w += s.width
if i > 0 && !s.noSep {
w += separatorPlainWidth
}
}
return w
}
for len(slots) > 1 && totalWidth(slots) > termWidth {
slots = slots[:len(slots)-1]
}
return slots
}
// ─── Renderer ──────────────────────────────────────────────────────────────
// truncateVisible truncates s so its visible cell width (excluding ANSI escapes)
// does not exceed maxWidth. Preserves ANSI codes that precede kept characters.
// A wide rune that would overflow is dropped entirely — no half-glyph output.
func truncateVisible(s string, maxWidth int) string {
var b strings.Builder
visible := 0
for i := 0; i < len(s); {
if s[i] == '\x1b' {
// Copy the entire escape sequence unconditionally.
j := i + 1
for j < len(s) && s[j] != 'm' {
j++
}
if j < len(s) {
j++ // include 'm'
}
b.WriteString(s[i:j])
i = j
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
w := runewidth.RuneWidth(r)
if visible+w > maxWidth {
break
}
b.WriteString(s[i : i+size])
visible += w
i += size
}
return b.String()
}
// renderLines groups slots by line, collapses line 1, builds segments, and joins.
func renderLines(dot string, slots []slot, termWidth int, separator string) string {
sepPlainWidth := plainLen(separator)
dotWidth := plainLen(dot) + 1 // +1 for the trailing space
// Group slots by line, preserving order within each line.
lineMap := map[int][]slot{}
var lineNums []int
for _, s := range slots {
if _, exists := lineMap[s.line]; !exists {
lineNums = append(lineNums, s.line)
}
lineMap[s.line] = append(lineMap[s.line], s)
}
var lines []string
for _, num := range lineNums {
group := lineMap[num]
var lb strings.Builder
// Line 1 only: collapse breadcrumbs and prepend dot.
if num == 1 {
group = collapseBreadcrumbs(group, termWidth-1, sepPlainWidth, dotWidth)
lb.WriteString(dot)
lb.WriteString(" ")
}
// Build segments — last in line gets active=true.
for i, s := range group {
active := i == len(group)-1
seg := s.build(active)
if s.styleOverride != "" {
seg.Content = s.styleOverride + stripAnsi(seg.Content)
seg.FgAnsi = s.styleOverride
}
lb.WriteString(seg.FgAnsi)
lb.WriteString(seg.Content)
if i+1 < len(group) && !group[i+1].noSep {
lb.WriteString(pal.separator)
lb.WriteString(separator)
}
}
lines = append(lines, lb.String())
}
var b strings.Builder
for i, line := range lines {
isLast := i+1 == len(lines)
// Non-last lines must stay below termWidth: when the cursor lands
// in column termWidth the terminal's automatic-margin wraps it,
// and the subsequent \n then consumes an extra row, pushing line 2
// off the status area. The last line needs no such reservation.
limit := termWidth
if !isLast {
// Reserve 2 cells: one for the auto-margin column the terminal
// refuses to print into without wrapping, plus one for residual
// width-miscount slack (terminal-emulator ambiguous-width quirks).
limit = termWidth - 2
}
if plainLen(line) > limit {
line = truncateVisible(line, limit)
}
b.WriteString(line)
if i+1 < len(lines) {
b.WriteString(ansiReset)
b.WriteString("\n")
}
}
b.WriteString(ansiReset)
return b.String()
}
// ─── Main ──────────────────────────────────────────────────────────────────
func main() {
if len(os.Args) == 1 && stdinIsPiped() {
Run()
return
}
executeRoot()
}
func stdinIsPiped() bool {
fi, err := os.Stdin.Stat()
if err != nil {
return false
}
return fi.Mode()&os.ModeCharDevice == 0
}