@say8425/cc-statusline 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,295 @@
1
+ #!/usr/bin/env bun
2
+ // @bun
3
+
4
+ // src/cli.ts
5
+ function parseCliArgs(args) {
6
+ const showUsage = args.includes("--show-usage");
7
+ return { showUsage };
8
+ }
9
+
10
+ // src/git/branch.ts
11
+ var {$ } = globalThis.Bun;
12
+
13
+ // src/cache.ts
14
+ var cache = {
15
+ branch: { value: "", timestamp: 0 },
16
+ gitChanges: { files: 0, insertions: 0, deletions: 0, timestamp: 0 },
17
+ prUrl: { value: null, timestamp: 0 },
18
+ blockUsage: {
19
+ value: null,
20
+ timestamp: 0
21
+ },
22
+ accessToken: { value: null, timestamp: 0 }
23
+ };
24
+ var CACHE_TTL = {
25
+ branch: 5000,
26
+ gitChanges: 0,
27
+ prUrl: 30000,
28
+ blockUsage: 120000,
29
+ accessToken: 300000
30
+ };
31
+
32
+ // src/git/branch.ts
33
+ async function getBranchCached() {
34
+ if (Date.now() - cache.branch.timestamp < CACHE_TTL.branch) {
35
+ return cache.branch.value;
36
+ }
37
+ try {
38
+ const result = await $`git branch --show-current 2>/dev/null`.text();
39
+ cache.branch = { value: result.trim(), timestamp: Date.now() };
40
+ return cache.branch.value;
41
+ } catch {
42
+ return cache.branch.value;
43
+ }
44
+ }
45
+ // src/git/changes.ts
46
+ var {$: $2 } = globalThis.Bun;
47
+ async function getGitChangesCached() {
48
+ if (Date.now() - cache.gitChanges.timestamp < CACHE_TTL.gitChanges) {
49
+ return cache.gitChanges;
50
+ }
51
+ try {
52
+ const [diff, staged] = await Promise.all([
53
+ $2`git diff --shortstat 2>/dev/null`.text(),
54
+ $2`git diff --cached --shortstat 2>/dev/null`.text()
55
+ ]);
56
+ const combined = `${diff}
57
+ ${staged}`;
58
+ const [files, insertions, deletions] = [
59
+ /(\d+) files?/g,
60
+ /(\d+) insertions?/g,
61
+ /(\d+) deletions?/g
62
+ ].map((regex) => (combined.match(regex) || []).reduce((sum, m) => sum + Number.parseInt(m, 10), 0));
63
+ cache.gitChanges = { files, insertions, deletions, timestamp: Date.now() };
64
+ return cache.gitChanges;
65
+ } catch {
66
+ return cache.gitChanges;
67
+ }
68
+ }
69
+ // src/git/pr.ts
70
+ var {$: $3 } = globalThis.Bun;
71
+ async function getPrUrlCached() {
72
+ if (Date.now() - cache.prUrl.timestamp < CACHE_TTL.prUrl) {
73
+ return cache.prUrl.value;
74
+ }
75
+ try {
76
+ const result = await $3`gh pr view --json url -q .url 2>/dev/null`.text();
77
+ cache.prUrl = { value: result.trim() || null, timestamp: Date.now() };
78
+ return cache.prUrl.value;
79
+ } catch {
80
+ return cache.prUrl.value;
81
+ }
82
+ }
83
+ // src/colors.ts
84
+ var C = {
85
+ RESET: "\x1B[0m",
86
+ CYAN: "\x1B[38;2;0;255;255m",
87
+ MAGENTA: "\x1B[38;2;255;100;200m",
88
+ GREEN: "\x1B[38;2;100;255;100m",
89
+ YELLOW: "\x1B[38;2;255;220;100m",
90
+ RED: "\x1B[38;2;255;100;100m",
91
+ BLUE: "\x1B[38;2;100;150;255m",
92
+ WHITE: "\x1B[38;2;200;200;200m",
93
+ UNDERLINE: "\x1B[4m"
94
+ };
95
+ function getUsageColor(pct) {
96
+ if (pct < 50)
97
+ return C.WHITE;
98
+ if (pct < 80)
99
+ return C.YELLOW;
100
+ return C.RED;
101
+ }
102
+
103
+ // src/format/formatNumber.ts
104
+ function formatNumber(n) {
105
+ return n.toLocaleString("en-US");
106
+ }
107
+ // src/format/formatResetDate.ts
108
+ function formatResetDate(date) {
109
+ const month = (date.getMonth() + 1).toString().padStart(2, "0");
110
+ const day = date.getDate().toString().padStart(2, "0");
111
+ const hours = date.getHours().toString().padStart(2, "0");
112
+ const minutes = date.getMinutes().toString().padStart(2, "0");
113
+ return `${month}/${day} ${hours}:${minutes}`;
114
+ }
115
+ // src/format/formatTime.ts
116
+ function formatTime(hours, mins) {
117
+ return `${hours.toString().padStart(2, "0")}:${mins.toString().padStart(2, "0")}`;
118
+ }
119
+ // src/render.ts
120
+ function renderStatusLine(ctx) {
121
+ const lines = [];
122
+ const folder = ctx.claudeJson.workspace?.project_dir?.split("/").pop() || "";
123
+ const sessionMs = ctx.claudeJson.cost?.total_duration_ms || 0;
124
+ const sessionSec = Math.floor(sessionMs / 1000);
125
+ const sessionHrs = Math.floor(sessionSec / 3600);
126
+ const sessionMins = Math.floor(sessionSec % 3600 / 60);
127
+ const costUsd = ctx.claudeJson.cost?.total_cost_usd || 0;
128
+ const usage = ctx.claudeJson.context_window?.current_usage;
129
+ const contextSize = ctx.claudeJson.context_window?.context_window_size || 200000;
130
+ const totalTokens = usage ? usage.input_tokens + usage.output_tokens + usage.cache_creation_input_tokens + usage.cache_read_input_tokens : 0;
131
+ const contextPct = Math.round(totalTokens / contextSize * 100);
132
+ const ctxColor = getUsageColor(contextPct);
133
+ let line1 = `${C.WHITE}\uD83D\uDCC1 ${folder}${C.RESET}`;
134
+ if (ctx.branch) {
135
+ line1 += ` | ${C.WHITE}\uD83C\uDF3F ${ctx.branch}${C.RESET}`;
136
+ }
137
+ lines.push(line1);
138
+ const line2 = `${C.WHITE}\u23F1\uFE0F ${formatTime(sessionHrs, sessionMins)}${C.RESET}` + ` | ${C.WHITE}\uD83D\uDCB0 $${costUsd.toFixed(2)}${C.RESET}` + ` | ${ctxColor}\uD83E\uDDE0 ${formatNumber(totalTokens)} (${contextPct}%)${C.RESET}`;
139
+ lines.push(line2);
140
+ if (ctx.showUsage && ctx.blockUsage) {
141
+ const parts = [];
142
+ if (ctx.blockUsage.resetTime) {
143
+ const h = ctx.blockUsage.resetTime.getHours();
144
+ const m = ctx.blockUsage.resetTime.getMinutes();
145
+ parts.push(`${C.WHITE}\u23F3 ${formatTime(h, m)}${C.RESET}`);
146
+ }
147
+ const usageColor = getUsageColor(ctx.blockUsage.utilization);
148
+ parts.push(`${usageColor}\uD83D\uDCCA ${Math.round(ctx.blockUsage.utilization)}/100${C.RESET}`);
149
+ if (ctx.blockUsage.sevenDayResetTime) {
150
+ parts.push(`${C.WHITE}\u23F0 ${formatResetDate(ctx.blockUsage.sevenDayResetTime)}${C.RESET}`);
151
+ }
152
+ if (ctx.blockUsage.sevenDayUtilization !== null) {
153
+ const weekColor = getUsageColor(ctx.blockUsage.sevenDayUtilization);
154
+ parts.push(`${weekColor}\uD83D\uDCC5 ${Math.round(ctx.blockUsage.sevenDayUtilization)}/100${C.RESET}`);
155
+ }
156
+ if (parts.length > 0) {
157
+ lines.push(parts.join(" | "));
158
+ }
159
+ }
160
+ const hasGitChanges = ctx.gitChanges.files > 0 || ctx.gitChanges.insertions > 0 || ctx.gitChanges.deletions > 0;
161
+ if (hasGitChanges || ctx.prUrl) {
162
+ let line4 = "";
163
+ if (hasGitChanges) {
164
+ line4 += `\u270F\uFE0F ${C.WHITE}${ctx.gitChanges.files} files${C.RESET} ${C.GREEN}+${ctx.gitChanges.insertions}${C.RESET} ${C.RED}-${ctx.gitChanges.deletions}${C.RESET}`;
165
+ }
166
+ if (ctx.prUrl) {
167
+ const prLabel = ctx.prUrl.replace(/^https?:\/\/[^/]+\//, "").replace("/pull/", "#");
168
+ if (line4)
169
+ line4 += " | ";
170
+ line4 += `\uD83D\uDCCE ${C.WHITE}${C.UNDERLINE}\x1B]8;;${ctx.prUrl}\x07${prLabel}\x1B]8;;\x07${C.RESET}`;
171
+ }
172
+ lines.push(line4);
173
+ }
174
+ return lines;
175
+ }
176
+
177
+ // src/stdin.ts
178
+ async function readStdin() {
179
+ const chunks = [];
180
+ for await (const chunk of Bun.stdin.stream()) {
181
+ chunks.push(chunk);
182
+ }
183
+ return Buffer.concat(chunks).toString("utf-8");
184
+ }
185
+
186
+ // src/usage/token.ts
187
+ var {$: $4 } = globalThis.Bun;
188
+ async function getAccessToken() {
189
+ if (process.platform !== "darwin")
190
+ return null;
191
+ try {
192
+ const result = await $4`security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null`.text();
193
+ const creds = JSON.parse(result);
194
+ return creds?.claudeAiOauth?.accessToken ?? null;
195
+ } catch {
196
+ return null;
197
+ }
198
+ }
199
+ async function getAccessTokenCached() {
200
+ if (Date.now() - cache.accessToken.timestamp < CACHE_TTL.accessToken) {
201
+ return cache.accessToken.value;
202
+ }
203
+ const token = await getAccessToken();
204
+ cache.accessToken = { value: token, timestamp: Date.now() };
205
+ return token;
206
+ }
207
+
208
+ // src/usage/api.ts
209
+ async function fetchUsageFromAPI(accessToken) {
210
+ try {
211
+ const controller = new AbortController;
212
+ const timeout = setTimeout(() => controller.abort(), 5000);
213
+ const response = await fetch("https://api.anthropic.com/api/oauth/usage", {
214
+ headers: {
215
+ Authorization: `Bearer ${accessToken}`,
216
+ "anthropic-beta": "oauth-2025-04-20"
217
+ },
218
+ signal: controller.signal
219
+ });
220
+ clearTimeout(timeout);
221
+ if (!response.ok)
222
+ return null;
223
+ return await response.json();
224
+ } catch {
225
+ return null;
226
+ }
227
+ }
228
+ function usageResponseToBlockUsage(data) {
229
+ const result = {
230
+ resetTime: null,
231
+ utilization: 0,
232
+ sevenDayUtilization: null,
233
+ sevenDayResetTime: null
234
+ };
235
+ if (data.five_hour) {
236
+ result.utilization = data.five_hour.utilization;
237
+ if (data.five_hour.resets_at) {
238
+ result.resetTime = new Date(data.five_hour.resets_at);
239
+ }
240
+ }
241
+ if (data.seven_day) {
242
+ result.sevenDayUtilization = data.seven_day.utilization;
243
+ if (data.seven_day.resets_at) {
244
+ result.sevenDayResetTime = new Date(data.seven_day.resets_at);
245
+ }
246
+ }
247
+ return result;
248
+ }
249
+ async function getUsageCached() {
250
+ if (Date.now() - cache.blockUsage.timestamp < CACHE_TTL.blockUsage) {
251
+ return cache.blockUsage.value;
252
+ }
253
+ const token = await getAccessTokenCached();
254
+ if (!token) {
255
+ cache.blockUsage = { value: null, timestamp: Date.now() };
256
+ return null;
257
+ }
258
+ const data = await fetchUsageFromAPI(token);
259
+ if (!data) {
260
+ cache.blockUsage = { value: null, timestamp: Date.now() };
261
+ return null;
262
+ }
263
+ const blockUsage = usageResponseToBlockUsage(data);
264
+ cache.blockUsage = { value: blockUsage, timestamp: Date.now() };
265
+ return blockUsage;
266
+ }
267
+ // src/index.ts
268
+ async function main(cliArgs) {
269
+ const args = cliArgs ?? process.argv.slice(2);
270
+ const { showUsage } = parseCliArgs(args);
271
+ const claudeJson = JSON.parse(await readStdin() || "{}");
272
+ const [branch, gitChanges, prUrl, blockUsage] = await Promise.all([
273
+ getBranchCached(),
274
+ getGitChangesCached(),
275
+ getPrUrlCached(),
276
+ showUsage ? getUsageCached() : Promise.resolve(null)
277
+ ]);
278
+ const lines = renderStatusLine({
279
+ claudeJson,
280
+ branch,
281
+ gitChanges,
282
+ prUrl,
283
+ blockUsage,
284
+ showUsage
285
+ });
286
+ for (const line of lines) {
287
+ console.log(line);
288
+ }
289
+ }
290
+ if (import.meta.main) {
291
+ main().catch(console.error);
292
+ }
293
+ export {
294
+ main
295
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@say8425/cc-statusline",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/say8425/cc-statusline.git"
@@ -11,12 +11,11 @@
11
11
  "typescript": "^5.9.3"
12
12
  },
13
13
  "bin": {
14
- "cc-statusline": "src/index.ts"
14
+ "cc-statusline": "dist/index.js"
15
15
  },
16
16
  "description": "Custom statusline for Claude Code",
17
17
  "files": [
18
- "src/index.ts",
19
- "src/lib.ts"
18
+ "dist/index.js"
20
19
  ],
21
20
  "publishConfig": {
22
21
  "registry": "https://registry.npmjs.org"
package/src/index.ts DELETED
@@ -1,48 +0,0 @@
1
- #!/usr/bin/env bun
2
-
3
- import { parseCliArgs } from "./cli.ts";
4
- import {
5
- getBranchCached,
6
- getGitChangesCached,
7
- getPrUrlCached,
8
- } from "./git/index.ts";
9
- import { renderStatusLine } from "./render.ts";
10
- import { readStdin } from "./stdin.ts";
11
- import type { ClaudeStatusInput } from "./types.ts";
12
- import { getUsageCached } from "./usage/index.ts";
13
-
14
- // 메인 함수
15
- export async function main(cliArgs?: string[]): Promise<void> {
16
- // CLI 인자 파싱
17
- const args = cliArgs ?? process.argv.slice(2);
18
- const { showUsage } = parseCliArgs(args);
19
-
20
- // 1. stdin에서 Claude Code JSON 읽기 (empty stdin 처리)
21
- const claudeJson: ClaudeStatusInput = JSON.parse((await readStdin()) || "{}");
22
-
23
- // 2. Git 정보 + 사용량 (캐싱, 병렬 실행)
24
- const [branch, gitChanges, prUrl, blockUsage] = await Promise.all([
25
- getBranchCached(),
26
- getGitChangesCached(),
27
- getPrUrlCached(),
28
- showUsage ? getUsageCached() : Promise.resolve(null),
29
- ]);
30
-
31
- // 3. 렌더링 및 출력
32
- const lines = renderStatusLine({
33
- claudeJson,
34
- branch,
35
- gitChanges,
36
- prUrl,
37
- blockUsage,
38
- showUsage,
39
- });
40
-
41
- for (const line of lines) {
42
- console.log(line);
43
- }
44
- }
45
-
46
- if (import.meta.main) {
47
- main().catch(console.error);
48
- }