@ravshansbox/pi-statusline 0.0.0-stage → 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 +26 -2
  3. package/index.ts +180 -0
  4. package/package.json +48 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ravshan
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 CHANGED
@@ -1,3 +1,27 @@
1
- # Temporary Holding Version
1
+ # pi-statusline
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Statusline extension for pi.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pi install npm:@ravshansbox/pi-statusline
9
+ ```
10
+
11
+ Add `-l` to install it in project settings.
12
+
13
+ ## Usage
14
+
15
+ Pi loads the extension from `./index.ts`. It replaces the built-in footer with a statusline that shows:
16
+
17
+ - the working directory, git branch and session name
18
+ - token usage, cache hit rate, cost and context usage
19
+ - the provider, model and thinking level
20
+ - statuses set by other extensions
21
+
22
+ ## Development
23
+
24
+ ```bash
25
+ npm install
26
+ npm run check
27
+ ```
package/index.ts ADDED
@@ -0,0 +1,180 @@
1
+ import { isAbsolute, relative, resolve, sep } from 'node:path';
2
+ import {
3
+ type ExtensionAPI,
4
+ type ExtensionContext,
5
+ SettingsManager,
6
+ } from '@earendil-works/pi-coding-agent';
7
+ import { truncateToWidth, visibleWidth } from '@earendil-works/pi-tui';
8
+
9
+ type Usage = {
10
+ input: number;
11
+ output: number;
12
+ cacheRead: number;
13
+ cacheWrite: number;
14
+ cost: { total: number };
15
+ };
16
+
17
+ function sanitizeStatusText(text: string): string {
18
+ return text
19
+ .replace(/[\r\n\t]/g, ' ')
20
+ .replace(/ +/g, ' ')
21
+ .trim();
22
+ }
23
+
24
+ function formatTokens(count: number): string {
25
+ if (count < 1000) return count.toString();
26
+ if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
27
+ if (count < 1000000) return `${Math.round(count / 1000)}k`;
28
+ if (count < 10000000) return `${(count / 1000000).toFixed(1)}M`;
29
+ return `${Math.round(count / 1000000)}M`;
30
+ }
31
+
32
+ function formatCwd(cwd: string, home: string | undefined): string {
33
+ if (!home) return cwd;
34
+ const rel = relative(resolve(home), resolve(cwd));
35
+ const inside = rel === '' || (rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
36
+ if (!inside) return cwd;
37
+ return rel === '' ? '~' : `~${sep}${rel}`;
38
+ }
39
+
40
+ function getUsage(ctx: ExtensionContext) {
41
+ const totals = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 };
42
+ let latestCacheHitRate: number | undefined;
43
+ const add = (u: Usage) => {
44
+ totals.input += u.input;
45
+ totals.output += u.output;
46
+ totals.cacheRead += u.cacheRead;
47
+ totals.cacheWrite += u.cacheWrite;
48
+ totals.cost += u.cost.total;
49
+ };
50
+ for (const entry of ctx.sessionManager.getEntries()) {
51
+ if (entry.type === 'usage') {
52
+ add(entry.usage);
53
+ } else if (entry.type === 'message' && entry.message.role === 'assistant') {
54
+ const u = entry.message.usage;
55
+ add(u);
56
+ const prompt = u.input + u.cacheRead + u.cacheWrite;
57
+ latestCacheHitRate = prompt > 0 ? (u.cacheRead / prompt) * 100 : undefined;
58
+ } else if (
59
+ entry.type === 'message' &&
60
+ entry.message.role === 'toolResult' &&
61
+ entry.message.usage
62
+ ) {
63
+ add(entry.message.usage);
64
+ } else if ((entry.type === 'branch_summary' || entry.type === 'compaction') && entry.usage) {
65
+ add(entry.usage);
66
+ }
67
+ }
68
+ return { totals, latestCacheHitRate };
69
+ }
70
+
71
+ export default function (pi: ExtensionAPI) {
72
+ pi.on('session_start', (_event, ctx) => {
73
+ if (ctx.mode !== 'tui') return;
74
+ const settings = SettingsManager.create(ctx.cwd);
75
+
76
+ ctx.ui.setFooter((tui, theme, footerData) => {
77
+ const unsub = footerData.onBranchChange(() => tui.requestRender());
78
+ return {
79
+ dispose: unsub,
80
+ invalidate() {},
81
+ render(width: number): string[] {
82
+ const model = ctx.model;
83
+ const { totals, latestCacheHitRate } = getUsage(ctx);
84
+ const contextUsage = ctx.getContextUsage();
85
+ const contextWindow = contextUsage?.contextWindow ?? model?.contextWindow ?? 0;
86
+ const contextPercentValue = contextUsage?.percent ?? 0;
87
+ const contextPercent =
88
+ contextUsage?.percent != null ? contextPercentValue.toFixed(1) : '?';
89
+
90
+ let pwd = formatCwd(
91
+ ctx.sessionManager.getCwd(),
92
+ process.env['HOME'] || process.env['USERPROFILE'],
93
+ );
94
+ const branch = footerData.getGitBranch();
95
+ if (branch) pwd = `${pwd} (${branch})`;
96
+ const sessionName = ctx.sessionManager.getSessionName();
97
+ if (sessionName) pwd = `${pwd} • ${sessionName}`;
98
+
99
+ const statsParts: string[] = [];
100
+ if (totals.input) statsParts.push(`↑${formatTokens(totals.input)}`);
101
+ if (totals.output) statsParts.push(`↓${formatTokens(totals.output)}`);
102
+ if (totals.cacheRead) statsParts.push(`R${formatTokens(totals.cacheRead)}`);
103
+ if (totals.cacheWrite) statsParts.push(`W${formatTokens(totals.cacheWrite)}`);
104
+ if ((totals.cacheRead > 0 || totals.cacheWrite > 0) && latestCacheHitRate !== undefined) {
105
+ statsParts.push(`CH${latestCacheHitRate.toFixed(1)}%`);
106
+ }
107
+
108
+ const usingSubscription = model
109
+ ? model.provider === 'kimi-coding' ||
110
+ (ctx.modelRegistry.isUsingOAuth(model) &&
111
+ ctx.modelRegistry.getProvider(model.provider)?.auth.oauth?.isSubscription === true)
112
+ : false;
113
+ if (totals.cost || usingSubscription) {
114
+ statsParts.push(`$${totals.cost.toFixed(3)}${usingSubscription ? ' (sub)' : ''}`);
115
+ }
116
+
117
+ const autoIndicator = settings.getCompactionEnabled() ? ' (auto)' : '';
118
+ const contextDisplay =
119
+ contextPercent === '?'
120
+ ? `?/${formatTokens(contextWindow)}${autoIndicator}`
121
+ : `${contextPercent}%/${formatTokens(contextWindow)}${autoIndicator}`;
122
+ if (contextPercentValue > 90) statsParts.push(theme.fg('error', contextDisplay));
123
+ else if (contextPercentValue > 70) statsParts.push(theme.fg('warning', contextDisplay));
124
+ else statsParts.push(contextDisplay);
125
+
126
+ if (process.env['PI_EXPERIMENTAL'] === '1') {
127
+ statsParts.push(`${theme.fg('dim', '•')} ${theme.bold(theme.fg('warning', 'xp'))}`);
128
+ }
129
+
130
+ let statsLeft = statsParts.join(' ');
131
+ let statsLeftWidth = visibleWidth(statsLeft);
132
+ if (statsLeftWidth > width) {
133
+ statsLeft = truncateToWidth(statsLeft, width, '...');
134
+ statsLeftWidth = visibleWidth(statsLeft);
135
+ }
136
+
137
+ const minPadding = 2;
138
+ const modelName = model?.id || 'no-model';
139
+ let right = modelName;
140
+ if (model?.reasoning) {
141
+ const level = ctx.thinkingLevel || 'off';
142
+ right = level === 'off' ? `${modelName} • thinking off` : `${modelName} • ${level}`;
143
+ }
144
+
145
+ const rightWidth = visibleWidth(right);
146
+ let statsLine: string;
147
+ if (statsLeftWidth + minPadding + rightWidth <= width) {
148
+ statsLine = statsLeft + ' '.repeat(width - statsLeftWidth - rightWidth) + right;
149
+ } else {
150
+ const available = width - statsLeftWidth - minPadding;
151
+ if (available > 0) {
152
+ const truncatedRight = truncateToWidth(right, available, '');
153
+ const padding = ' '.repeat(
154
+ Math.max(0, width - statsLeftWidth - visibleWidth(truncatedRight)),
155
+ );
156
+ statsLine = statsLeft + padding + truncatedRight;
157
+ } else {
158
+ statsLine = statsLeft;
159
+ }
160
+ }
161
+
162
+ const remainder = statsLine.slice(statsLeft.length);
163
+ const pwdLine = truncateToWidth(theme.fg('dim', pwd), width, theme.fg('dim', '...'));
164
+ const lines = [pwdLine, theme.fg('dim', statsLeft) + theme.fg('dim', remainder)];
165
+
166
+ const statuses = footerData.getExtensionStatuses();
167
+ if (statuses.size > 0) {
168
+ const line = Array.from(statuses.entries())
169
+ .sort(([a], [b]) => a.localeCompare(b))
170
+ .map(([, text]) => sanitizeStatusText(text))
171
+ .join(' ');
172
+ lines.push(truncateToWidth(line, width, theme.fg('dim', '...')));
173
+ }
174
+
175
+ return lines;
176
+ },
177
+ };
178
+ });
179
+ });
180
+ }
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@ravshansbox/pi-statusline",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.1.0",
4
+ "description": "Statusline extension for pi",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ravshansbox/pi-statusline.git"
10
+ },
11
+ "homepage": "https://github.com/ravshansbox/pi-statusline#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/ravshansbox/pi-statusline/issues"
14
+ },
15
+ "keywords": [
16
+ "pi-package",
17
+ "pi-extension",
18
+ "statusline"
19
+ ],
20
+ "pi": {
21
+ "extensions": [
22
+ "./index.ts"
23
+ ]
24
+ },
25
+ "scripts": {
26
+ "typecheck": "tsc --noEmit",
27
+ "lint": "oxlint --deny-warnings",
28
+ "lint:fix": "oxlint --fix",
29
+ "format": "oxfmt",
30
+ "format:check": "oxfmt --check",
31
+ "test": "node --test",
32
+ "check": "npm run typecheck && npm run lint && npm run format:check && npm test"
33
+ },
34
+ "peerDependencies": {
35
+ "@earendil-works/pi-coding-agent": "*",
36
+ "@earendil-works/pi-tui": "*"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^26.6.5",
40
+ "oxfmt": "^0.74.0",
41
+ "oxlint": "^1.89.0",
42
+ "typescript": "^7.0.2"
43
+ },
44
+ "files": [
45
+ "index.ts"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ }
50
+ }