@say8425/cc-statusline 1.1.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -0
  3. package/package.json +29 -0
  4. package/src/index.ts +169 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 say8425
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # cc-statusline
2
+
3
+ Custom statusline for Claude Code.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@say8425/cc-statusline)](https://www.npmjs.com/package/@say8425/cc-statusline)
6
+ ![Bun](https://img.shields.io/badge/Bun-black?style=flat&logo=bun)
7
+ ![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?style=flat&logo=typescript&logoColor=white)
8
+
9
+ ## Preview
10
+
11
+ ![preview-1](docs/preview-1.png)
12
+
13
+ ![preview-2](docs/preview-2.png)
14
+
15
+ ## Features
16
+
17
+ - **Session Time**: Current session elapsed time
18
+ - **Context %**: Current context window usage (updates immediately)
19
+ - **Session Tokens**: Cumulative token usage
20
+ - **PR URL**: Clickable OSC 8 hyperlink
21
+ - **TrueColor**: Dynamic colors based on thresholds
22
+
23
+ ## Installation
24
+
25
+ Add the following to `~/.claude/settings.json`:
26
+
27
+ ```json
28
+ {
29
+ "statusLine": {
30
+ "type": "command",
31
+ "command": "bunx @say8425/cc-statusline",
32
+ "padding": 0
33
+ }
34
+ }
35
+ ```
36
+
37
+ ## Dependencies
38
+
39
+ - [Bun](https://bun.sh) - JavaScript runtime
40
+ - [gh](https://cli.github.com) - GitHub CLI (optional, for PR URL)
41
+
42
+ ## Color Thresholds
43
+
44
+ | Metric | Normal (white) | Warning (yellow) | Critical (red) |
45
+ |--------|----------------|------------------|----------------|
46
+ | Context % | < 50% | 50-80% | > 80% |
47
+
48
+ ## License
49
+
50
+ MIT
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@say8425/cc-statusline",
3
+ "version": "1.1.0",
4
+ "type": "module",
5
+ "description": "Custom statusline for Claude Code",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/say8425/cc-statusline.git"
9
+ },
10
+ "bin": {
11
+ "cc-statusline": "src/index.ts"
12
+ },
13
+ "files": [
14
+ "src/index.ts"
15
+ ],
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org"
18
+ },
19
+ "scripts": {
20
+ "start": "bun src/index.ts",
21
+ "lint": "biome check src/",
22
+ "typecheck": "tsc --noEmit"
23
+ },
24
+ "devDependencies": {
25
+ "@biomejs/biome": "^2.3.9",
26
+ "@types/bun": "latest",
27
+ "typescript": "^5.9.3"
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,169 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { $ } from "bun";
4
+
5
+ // 공식 Claude Code JSON input 타입 정의
6
+ interface ClaudeStatusInput {
7
+ cost: {
8
+ total_duration_ms: number;
9
+ total_cost_usd: number;
10
+ };
11
+ context_window: {
12
+ context_window_size: number;
13
+ current_usage: {
14
+ input_tokens: number;
15
+ output_tokens: number;
16
+ cache_creation_input_tokens: number;
17
+ cache_read_input_tokens: number;
18
+ };
19
+ };
20
+ workspace: {
21
+ current_dir: string;
22
+ project_dir: string;
23
+ };
24
+ }
25
+
26
+ // 캐시 구조
27
+ const cache = {
28
+ branch: { value: "", timestamp: 0 },
29
+ prUrl: { value: null as string | null, timestamp: 0 },
30
+ };
31
+
32
+ // 캐시 TTL (ms)
33
+ const CACHE_TTL = {
34
+ branch: 5000, // 5초
35
+ prUrl: 30000, // 30초
36
+ };
37
+
38
+ // TrueColor 색상 정의
39
+ const C = {
40
+ RESET: "\x1b[0m",
41
+ CYAN: "\x1b[38;2;0;255;255m",
42
+ MAGENTA: "\x1b[38;2;255;100;200m",
43
+ GREEN: "\x1b[38;2;100;255;100m",
44
+ YELLOW: "\x1b[38;2;255;220;100m",
45
+ RED: "\x1b[38;2;255;100;100m",
46
+ BLUE: "\x1b[38;2;100;150;255m",
47
+ WHITE: "\x1b[38;2;200;200;200m",
48
+ UNDERLINE: "\x1b[4m",
49
+ };
50
+
51
+ // Context 사용률에 따른 색상
52
+ function getContextColor(pct: number): string {
53
+ if (pct < 50) return C.WHITE;
54
+ if (pct < 80) return C.YELLOW;
55
+ return C.RED;
56
+ }
57
+
58
+ // 숫자 포맷팅 (천 단위 콤마)
59
+ function formatNumber(n: number): string {
60
+ return n.toLocaleString("en-US");
61
+ }
62
+
63
+ // 시간 포맷팅 (HH:MM)
64
+ function formatTime(hours: number, mins: number): string {
65
+ return `${hours.toString().padStart(2, "0")}:${mins.toString().padStart(2, "0")}`;
66
+ }
67
+
68
+ // stdin에서 JSON 읽기
69
+ async function readStdin(): Promise<string> {
70
+ const chunks: Uint8Array[] = [];
71
+ for await (const chunk of Bun.stdin.stream()) {
72
+ chunks.push(chunk);
73
+ }
74
+ return Buffer.concat(chunks).toString("utf-8");
75
+ }
76
+
77
+ // Git 브랜치 가져오기 (캐싱)
78
+ async function getBranchCached(): Promise<string> {
79
+ if (Date.now() - cache.branch.timestamp < CACHE_TTL.branch) {
80
+ return cache.branch.value;
81
+ }
82
+ try {
83
+ const result = await $`git branch --show-current 2>/dev/null`.text();
84
+ cache.branch = { value: result.trim(), timestamp: Date.now() };
85
+ return cache.branch.value;
86
+ } catch {
87
+ return cache.branch.value;
88
+ }
89
+ }
90
+
91
+ // PR URL 가져오기 (캐싱)
92
+ async function getPrUrlCached(): Promise<string | null> {
93
+ if (Date.now() - cache.prUrl.timestamp < CACHE_TTL.prUrl) {
94
+ return cache.prUrl.value;
95
+ }
96
+ try {
97
+ const result = await $`gh pr view --json url -q .url 2>/dev/null`.text();
98
+ cache.prUrl = { value: result.trim() || null, timestamp: Date.now() };
99
+ return cache.prUrl.value;
100
+ } catch {
101
+ return cache.prUrl.value;
102
+ }
103
+ }
104
+
105
+ // 메인 함수
106
+ async function main() {
107
+ // 1. stdin에서 Claude Code JSON 읽기
108
+ const claudeJson: ClaudeStatusInput = JSON.parse(await readStdin());
109
+
110
+ // 2. 폴더 이름 추출 (프로젝트 루트 디렉토리)
111
+ const folder = claudeJson.workspace?.project_dir?.split("/").pop() || "";
112
+
113
+ // 3. 세션 시간 계산
114
+ const sessionMs = claudeJson.cost?.total_duration_ms || 0;
115
+ const sessionSec = Math.floor(sessionMs / 1000);
116
+ const sessionHrs = Math.floor(sessionSec / 3600);
117
+ const sessionMins = Math.floor((sessionSec % 3600) / 60);
118
+
119
+ // 4. 비용
120
+ const costUsd = claudeJson.cost?.total_cost_usd || 0;
121
+
122
+ // 5. Context 토큰 계산
123
+ const usage = claudeJson.context_window?.current_usage;
124
+ const contextSize = claudeJson.context_window?.context_window_size || 200000;
125
+
126
+ const totalTokens = usage
127
+ ? usage.input_tokens +
128
+ usage.output_tokens +
129
+ usage.cache_creation_input_tokens +
130
+ usage.cache_read_input_tokens
131
+ : 0;
132
+
133
+ const contextPct = Math.round((totalTokens / contextSize) * 100);
134
+ const ctxColor = getContextColor(contextPct);
135
+
136
+ // 6. Git 정보 (캐싱, 병렬 실행)
137
+ const [branch, prUrl] = await Promise.all([
138
+ getBranchCached(),
139
+ getPrUrlCached(),
140
+ ]);
141
+
142
+ // 7. 출력
143
+ // 1번째 줄: 폴더 | 브랜치
144
+ let line1 = `${C.WHITE}📁 ${folder}${C.RESET}`;
145
+ if (branch) {
146
+ line1 += ` | ${C.WHITE}🌿 ${branch}${C.RESET}`;
147
+ }
148
+ console.log(line1);
149
+
150
+ // 2번째 줄: 세션 시간 | 비용 | 컨텍스트
151
+ console.log(
152
+ `${C.WHITE}⏱️ ${formatTime(sessionHrs, sessionMins)}${C.RESET} | ` +
153
+ `${C.WHITE}💰 $${costUsd.toFixed(2)}${C.RESET} | ` +
154
+ `${ctxColor}🧠 ${formatNumber(totalTokens)} (${contextPct}%)${C.RESET}`,
155
+ );
156
+
157
+ // 3번째 줄: PR URL (있을 경우만)
158
+ if (prUrl) {
159
+ const prLabel = prUrl
160
+ .replace("https://github.com/", "")
161
+ .replace("/pull/", "#");
162
+ // OSC 8 하이퍼링크
163
+ console.log(
164
+ `📎 ${C.WHITE}${C.UNDERLINE}\x1b]8;;${prUrl}\x07${prLabel}\x1b]8;;\x07${C.RESET}`,
165
+ );
166
+ }
167
+ }
168
+
169
+ main().catch(console.error);