-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
235 lines (198 loc) · 7.92 KB
/
Copy pathextension.js
File metadata and controls
235 lines (198 loc) · 7.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
const { GObject, St, Gio, GLib, Clutter } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Extension = Me.imports.extension.Extension;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const _ = imports.gettext.domain('gnome-shell-extensions').gettext;
const ClaudeUsageIndicator = GObject.registerClass(
class ClaudeUsageIndicator extends PanelMenu.Button {
_init() {
super._init(0.0, _('Claude Code Usage'));
this._extensionPath = Me.path;
this._scriptPath = GLib.build_filenamev([this._extensionPath, 'get-usage.py']);
this._icon = new St.Label({
text: '🤔',
y_align: Clutter.ActorAlign.CENTER,
style_class: 'system-status-icon',
});
this.add_child(this._icon);
this._usageLabel = new St.Label({
text: _('...'),
y_align: Clutter.ActorAlign.CENTER,
style_class: 'claude-usage-label'
});
this.add_child(this._usageLabel);
// Refresh button
this._refreshItem = new PopupMenu.PopupMenuItem(_('Refresh'));
this._refreshItem.connect('activate', () => {
this._fetchUsage();
});
this.menu.addMenuItem(this._refreshItem);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// Title
let titleBox = new St.BoxLayout({
style: 'padding: 6px 12px;'
});
let titleLabel = new St.Label({
text: 'Claude Usage',
style: 'font-weight: bold;'
});
titleBox.add_child(titleLabel);
let titleItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false,
});
titleItem.actor.add_child(titleBox);
this.menu.addMenuItem(titleItem);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// Session info - plain text, no underlines
let sessionBox = new St.BoxLayout({
vertical: true,
style: 'padding: 8px 12px;'
});
this._sessionLabel = new St.Label({
text: 'Session: Loading...',
style: 'font-family: monospace;'
});
this._sessionLabel.clutter_text.set_use_markup(false);
this._sessionLabel.clutter_text.set_selectable(false);
sessionBox.add_child(this._sessionLabel);
let sessionItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false,
activate: false,
});
sessionItem.actor.add_child(sessionBox);
this.menu.addMenuItem(sessionItem);
// Weekly info - plain text, no underlines
let weeklyBox = new St.BoxLayout({
vertical: true,
style: 'padding: 8px 12px;'
});
this._weeklyLabel = new St.Label({
text: 'Weekly: Loading...',
style: 'font-family: monospace;'
});
this._weeklyLabel.clutter_text.set_use_markup(false);
this._weeklyLabel.clutter_text.set_selectable(false);
weeklyBox.add_child(this._weeklyLabel);
let weeklyItem = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false,
activate: false,
});
weeklyItem.actor.add_child(weeklyBox);
this.menu.addMenuItem(weeklyItem);
this._fetchUsage();
this._startAutoRefresh();
}
_fetchUsage() {
this._usageLabel.set_text('...');
try {
const proc = Gio.Subprocess.new(
['python3', this._scriptPath],
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
const [, stdout, stderr] = proc.communicate_utf8_finish(res);
if (proc.get_successful()) {
this._parseUsageData(stdout);
} else {
this._usageLabel.set_text('ERR');
this._sonnetItem.label.set_text('Failed to fetch usage');
logError(new Error(stderr), 'Claude usage script failed');
}
} catch (e) {
logError(e, 'Failed to parse usage data');
this._usageLabel.set_text('ERR');
}
});
} catch (e) {
logError(e, 'Failed to run usage script');
this._usageLabel.set_text('ERR');
}
}
_parseUsageData(jsonData) {
try {
const data = JSON.parse(jsonData);
if (data.error) {
this._usageLabel.set_text('ERR');
this._sessionLabel.set_text(`Error: ${data.error}`);
this._weeklyLabel.set_text('');
this._icon.set_text('✗'); // Error icon
return;
}
const weeklyPercent = data.weekly_percent || 0;
const sessionPercent = data.session_percent || 0;
// Change icon based on session usage
if (sessionPercent >= 100) {
this._icon.set_text('ʕノ•ᴥ•ʔノ ︵ ┻━┻'); // Disapproval look when maxed out
// Or other options:
// this._icon.set_text('💀'); // Skull
// this._icon.set_text('🔥'); // Fire
// this._icon.set_text('⚠️'); // Warning
// this._icon.set_text('😱'); // Scared face
} else if (sessionPercent >= 80) {
this._icon.set_text('ಠ_ಠ'); // Warning look
} else {
this._icon.set_text('🥳'); // Happy look
// Or other options:
// this._icon.set_text('🤖'); // Robot
// this._icon.set_text('✓'); // Check mark
// this._icon.set_text('◕_◕'); // Neutral
}
this._usageLabel.set_text(`${weeklyPercent}%`);
const formatBar = (percent) => {
const barLength = 20;
const filled = Math.round((percent / 100) * barLength);
const bar = '●'.repeat(filled) + '○'.repeat(barLength - filled);
// const bar = '▰'.repeat(filled) + '▱'.repeat(barLength - filled);
return `${bar}`;
};
this._sessionLabel.set_text(
`Session: ${sessionPercent}% used\n${formatBar(sessionPercent)}\nResets: ${data.session_reset_time}`
);
this._weeklyLabel.set_text(
`Weekly: ${weeklyPercent}% used\n${formatBar(weeklyPercent)}\nResets: ${data.weekly_reset_date}`
);
} catch (e) {
logError(e, 'Failed to parse JSON usage data');
this._usageLabel.set_text('ERR');
this._sessionLabel.set_text('Failed to parse data');
this._icon.set_text('✗');
}
}
_startAutoRefresh() {
if (this._timeoutId) {
GLib.Source.remove(this._timeoutId);
}
this._timeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 300, () => {
this._fetchUsage();
return GLib.SOURCE_CONTINUE;
});
}
destroy() {
if (this._timeoutId) {
GLib.Source.remove(this._timeoutId);
this._timeoutId = null;
}
super.destroy();
}
});
let claudeUsageIndicator;
function init() {
// Initialization logic if needed
}
function enable() {
claudeUsageIndicator = new ClaudeUsageIndicator();
Main.panel.addToStatusArea('claude-usage', claudeUsageIndicator);
}
function disable() {
if (claudeUsageIndicator) {
claudeUsageIndicator.destroy();
claudeUsageIndicator = null;
}
}