-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathWeeklyOpusUsage.ts
More file actions
128 lines (106 loc) 路 4.65 KB
/
Copy pathWeeklyOpusUsage.ts
File metadata and controls
128 lines (106 loc) 路 4.65 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
import type { RenderContext } from '../types/RenderContext';
import type { Settings } from '../types/Settings';
import type {
CustomKeybind,
Widget,
WidgetEditorDisplay,
WidgetItem
} from '../types/Widget';
import {
getUsageErrorMessage,
resolveWeeklyOpusUsageWindow
} from '../utils/usage';
import { makeTimerProgressBar } from './shared/progress-bar';
import { formatRawOrLabeledValue } from './shared/raw-or-labeled';
import {
cycleUsageDisplayMode,
getUsageDisplayMode,
getUsageDisplayModifierText,
getUsagePercentCustomKeybinds,
getUsageProgressBarWidth,
isUsageCursorEnabled,
isUsageInverted,
isUsageProgressMode,
isUsageSliderMode,
makeSliderBar,
toggleUsageCursor,
toggleUsageInverted
} from './shared/usage-display';
const LABEL = 'Weekly Opus: ';
export class WeeklyOpusUsageWidget implements Widget {
getDefaultColor(): string { return 'brightBlue'; }
getDescription(): string { return 'Shows weekly Opus API usage percentage'; }
getDisplayName(): string { return 'Weekly Opus Usage'; }
getCategory(): string { return 'Usage'; }
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
return {
displayText: this.getDisplayName(),
modifierText: getUsageDisplayModifierText(item)
};
}
handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
if (action === 'toggle-progress') {
return cycleUsageDisplayMode(item, [], true);
}
if (action === 'toggle-invert') {
return toggleUsageInverted(item);
}
if (action === 'toggle-cursor') {
return toggleUsageCursor(item);
}
return null;
}
render(item: WidgetItem, context: RenderContext, settings: Settings): string | null {
const displayMode = getUsageDisplayMode(item);
const inverted = isUsageInverted(item);
const showCursor = isUsageCursorEnabled(item);
if (context.isPreview) {
const previewPercent = 4;
const renderedPercent = inverted ? 100 - previewPercent : previewPercent;
if (isUsageProgressMode(displayMode)) {
const width = getUsageProgressBarWidth(displayMode);
const progressBar = makeTimerProgressBar(renderedPercent, width, showCursor ? { cursorPercent: 50 } : undefined);
const progressDisplay = `[${progressBar}] ${renderedPercent.toFixed(1)}%`;
return formatRawOrLabeledValue(item, LABEL, progressDisplay);
}
if (isUsageSliderMode(displayMode)) {
const slider = makeSliderBar(renderedPercent, showCursor ? { cursorPercent: 50 } : undefined);
const sliderDisplay = displayMode === 'slider' ? `${slider} ${renderedPercent.toFixed(1)}%` : slider;
return formatRawOrLabeledValue(item, LABEL, sliderDisplay);
}
return formatRawOrLabeledValue(item, LABEL, `${previewPercent.toFixed(1)}%`);
}
const data = context.usageData ?? {};
if (data.weeklyOpusUsage === undefined) {
if (data.error)
return getUsageErrorMessage(data.error);
return null;
}
const percent = Math.max(0, Math.min(100, data.weeklyOpusUsage));
const renderedPercent = inverted ? 100 - percent : percent;
const getCursorOptions = (): { cursorPercent: number } | undefined => {
if (!showCursor) {
return undefined;
}
const window = resolveWeeklyOpusUsageWindow(data);
return window ? { cursorPercent: window.elapsedPercent } : undefined;
};
if (isUsageProgressMode(displayMode)) {
const width = getUsageProgressBarWidth(displayMode);
const progressBar = makeTimerProgressBar(renderedPercent, width, getCursorOptions());
const progressDisplay = `[${progressBar}] ${renderedPercent.toFixed(1)}%`;
return formatRawOrLabeledValue(item, LABEL, progressDisplay);
}
if (isUsageSliderMode(displayMode)) {
const slider = makeSliderBar(renderedPercent, getCursorOptions());
const sliderDisplay = displayMode === 'slider' ? `${slider} ${renderedPercent.toFixed(1)}%` : slider;
return formatRawOrLabeledValue(item, LABEL, sliderDisplay);
}
return formatRawOrLabeledValue(item, LABEL, `${percent.toFixed(1)}%`);
}
getCustomKeybinds(item?: WidgetItem): CustomKeybind[] {
return getUsagePercentCustomKeybinds(item);
}
supportsRawValue(): boolean { return true; }
supportsColors(item: WidgetItem): boolean { return true; }
}