Repository navigation
Expand file tree
/
Copy pathformat.go
More file actions
148 lines (129 loc) · 4.68 KB
/
Copy pathformat.go
File metadata and controls
148 lines (129 loc) · 4.68 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
package main
import (
"fmt"
"strings"
"time"
)
// formatTimeRemaining returns a human-readable duration until resetTime.
// Returns "unknown" if resetTime is nil, "now" if already past.
func formatTimeRemaining(resetTime *time.Time) string {
if resetTime == nil {
return "unknown"
}
delta := time.Until(*resetTime)
if delta < 0 {
return "now"
}
totalSec := int(delta.Seconds())
hours := totalSec / 3600
minutes := (totalSec % 3600) / 60
if hours > 0 {
return fmt.Sprintf("%dh %dm", hours, minutes)
}
return fmt.Sprintf("%dm", minutes)
}
// formatResetDate formats a reset time as local "Day HH:MM".
func formatResetDate(resetTime *time.Time) string {
if resetTime == nil {
return ""
}
local := resetTime.Local()
return local.Format("Mon 15:04")
}
// formatClockLine returns "<label>: HH:MM:SS" in local time, or "<label>: --"
// when the time is unknown. Absolute (not relative) so the tray only needs to
// refresh it on a poll, not tick every second.
func formatClockLine(label string, t *time.Time) string {
if t == nil {
return label + ": --"
}
return label + ": " + t.Local().Format("15:04:05")
}
// formatSaturationLine returns a formatted saturation line, or "" if nil.
func formatSaturationLine(saturation *time.Time) string {
if saturation == nil {
return ""
}
remaining := formatTimeRemaining(saturation)
date := formatResetDate(saturation)
return fmt.Sprintf(" - saturates in %s, %s", remaining, date)
}
// formatProjectionLine returns a formatted projection line, or "" if nil.
func formatProjectionLine(projected *float64) string {
if projected == nil {
return ""
}
return fmt.Sprintf(" - projected ~%.0f%% at reset", *projected)
}
// windowLabel renders a quota window as "5h" / "7d"; zero falls back to the
// assumed label so older snapshots and tests keep their wording.
func windowLabel(d time.Duration, fallback string) string {
switch {
case d <= 0:
return fallback
case d%(24*time.Hour) == 0:
return fmt.Sprintf("%dd", int(d/(24*time.Hour)))
case d%time.Hour == 0:
return fmt.Sprintf("%dh", int(d/time.Hour))
default:
return d.String()
}
}
func primaryLabel(state QuotaState) string { return windowLabel(state.FiveHourWindow, "5h") }
func secondaryLabel(state QuotaState) string { return windowLabel(state.SevenDayWindow, "7d") }
// hasSecondary reports whether the secondary row should be shown: hidden only
// when the provider returned a primary bucket without a secondary one (Codex
// plans with a single weekly window).
func hasSecondary(state QuotaState) bool {
return state.SevenDay != nil || state.FiveHour == nil
}
// formatQuotaLine formats a single quota line with remaining time and date.
func formatQuotaLine(label string, utilization *float64, resets *time.Time) string {
if utilization == nil {
return fmt.Sprintf("%s: --", label)
}
remaining := formatTimeRemaining(resets)
date := formatResetDate(resets)
if date != "" {
return fmt.Sprintf("%s: %.0f%% (resets in %s, %s)", label, *utilization, remaining, date)
}
return fmt.Sprintf("%s: %.0f%%", label, *utilization)
}
func formatDryRunSummary(provider Provider, credentialsPath string, state QuotaState) string {
var lines []string
lines = append(lines, fmt.Sprintf("Provider: %s", provider))
lines = append(lines, fmt.Sprintf("Credentials: %s", credentialsPath))
if state.Error != "" {
lines = append(lines, fmt.Sprintf("Error: %s", state.Error))
return strings.Join(lines, "\n")
}
lines = append(lines, formatQuotaLine(primaryLabel(state), state.FiveHour, state.FiveHourResets))
if line := formatProjectionLine(state.FiveHourProjected); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
if line := formatSaturationLine(state.FiveHourSaturation); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
if hasSecondary(state) {
lines = append(lines, formatQuotaLine(secondaryLabel(state), state.SevenDay, state.SevenDayResets))
if line := formatProjectionLine(state.SevenDayProjected); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
if line := formatSaturationLine(state.SevenDaySaturation); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
}
if state.SevenDaySonnet != nil {
lines = append(lines, formatQuotaLine(extraQuotaLabel(state), state.SevenDaySonnet, state.SevenDaySonnetResets))
if line := formatProjectionLine(state.SevenDaySonnetProjected); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
if line := formatSaturationLine(state.SevenDaySonnetSaturation); line != "" {
lines = append(lines, strings.TrimSpace(line))
}
}
if state.LastUpdate != nil {
lines = append(lines, fmt.Sprintf("Updated: %s", state.LastUpdate.Local().Format(time.RFC3339)))
}
return strings.Join(lines, "\n")
}