-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokens.go
More file actions
107 lines (94 loc) · 2.93 KB
/
Copy pathtokens.go
File metadata and controls
107 lines (94 loc) · 2.93 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
package main
import (
"fmt"
"strconv"
"github.com/dotcommander/statusline/internal/config"
)
// ─── Token Math and Health ─────────────────────────────────────────────────
// getEffectiveTokens calculates used tokens and remaining percentage.
// Exact port of the TS implementation with all 4 priority paths.
func getEffectiveTokens(ctx *ContextWindowData, maxOutputTokens int) (used, remainingPct int) {
if ctx == nil || ctx.ContextWindowSize == nil || *ctx.ContextWindowSize == 0 {
return 0, 100
}
total := *ctx.ContextWindowSize
reserved := maxOutputTokens
available := max(total-reserved, 0)
calcPct := func(u int) int {
if available > 0 {
remaining := max(available-u, 0)
return int(float64(remaining)/float64(available)*100 + 0.5)
}
return int(float64(total-u)/float64(total)*100 + 0.5)
}
// Priority 1: used_percentage
if ctx.UsedPercentage != nil && *ctx.UsedPercentage > 0 {
u := int(*ctx.UsedPercentage/100*float64(total) + 0.5)
return u, calcPct(u)
}
// Priority 2: remaining_percentage
if ctx.RemainingPercentage != nil && *ctx.RemainingPercentage > 0 && *ctx.RemainingPercentage < 100 {
u := int((100-*ctx.RemainingPercentage)/100*float64(total) + 0.5)
return u, calcPct(u)
}
// Priority 3: current_usage
if ctx.CurrentUsage != nil {
cu := ctx.CurrentUsage
cuTotal := cu.InputTokens + cu.CacheCreationInputTokens + cu.CacheReadInputTokens
if cuTotal > 0 {
u := min(cuTotal, total)
return u, calcPct(u)
}
}
// Priority 4: cumulative totals
cumulative := 0
if ctx.TotalInputTokens != nil {
cumulative += *ctx.TotalInputTokens
}
if ctx.TotalOutputTokens != nil {
cumulative += *ctx.TotalOutputTokens
}
if cumulative == 0 {
return 0, 100
}
u := min(cumulative, total)
return u, calcPct(u)
}
// contextHealth determines health level from remaining percentage.
func contextHealth(remainingPct int, cfg *config.ContextConfig) ContextHealth {
switch {
case remainingPct > cfg.WarningPct:
return HealthGreen
case remainingPct > cfg.CriticalPct:
return HealthYellow
default:
return HealthRed
}
}
// dotColor returns the ANSI foreground color for the mode dot based on context health.
// Uses Catppuccin Mocha colors: green (#a6e3a1), yellow (#f9e2af), red (#f38ba8), orange/peach (#fab387).
func dotColor(h ContextHealth) string {
switch h {
case HealthGreen:
return pal.green
case HealthYellow:
return pal.yellow
case HealthRed:
return pal.red
default: // HealthNone
return pal.orange
}
}
// formatTokens formats a token count as a human-readable string.
func formatTokens(tokens int) string {
switch {
case tokens >= 1_000_000:
return fmt.Sprintf("%.1fM", float64(tokens)/1_000_000)
case tokens >= 10_000:
return fmt.Sprintf("%dk", (tokens+500)/1_000)
case tokens >= 1_000:
return fmt.Sprintf("%.1fk", float64(tokens)/1_000)
default:
return strconv.Itoa(tokens)
}
}