@jack-yang/pi-tps-meter 0.2.0 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.0] - 2026-11-04
4
+
5
+ - New default display `footer`: the meter sits at the right end of pi's footer line with the
6
+ working directory, in line with the model name below it, without taking an extra line. pi's
7
+ own `FooterComponent` still renders the whole footer (the stats and status lines are
8
+ byte-for-byte pi's); the meter is merged into its first line. On a narrow terminal the
9
+ directory is shortened first, so the reading stays whole.
10
+ - If pi's footer cannot be wrapped (for example a future pi changes its internals), pi's own
11
+ footer is restored and the meter falls back to `right`, with a one-time notice.
12
+ - `/tps` cycles footer → right → status → widget → off; a saved `mode` still wins.
13
+
3
14
  ## [0.2.0] - 2026-11-03
4
15
 
5
16
  - New default display `right`: one line below the editor, right-aligned so it ends in the last
package/README.md CHANGED
@@ -12,9 +12,9 @@
12
12
  ⠔⠀⠈⠀⠤⠔ TPS 42.5 | AVG 38.2 | TTFT 0.8s
13
13
  ```
14
14
 
15
- 默认显示在编辑器下方、右对齐(`/tps right`),紧贴 footer,和下方的模型名右侧对齐;位置固定,不会被其他扩展的 footer 状态(比如 pi-lens 的 `LSP Inactive`)挤开。下图是模型思考时的样子,读数左边是思考粒子流:
15
+ 默认显示在 footer 的目录那一行右端(`/tps footer`),和下一行的模型名右对齐,不额外占行;其他扩展的 footer 状态(比如 pi-lens 的 `LSP Inactive`)也挤不动它。下图是模型思考时的样子,读数左边是思考粒子流:
16
16
 
17
- ![编辑器下方右对齐:思考中,粒子流 + TPS 26.0 / AVG 33.7 / TTFT 3.9s](https://raw.githubusercontent.com/yangwudong/pi-tps-meter/main/docs/screenshot-right.png)
17
+ ![footer 目录行右端:思考中,粒子流 + TPS 38.2 / AVG 62.2 / TTFT 1.9s](https://raw.githubusercontent.com/yangwudong/pi-tps-meter/main/docs/screenshot-footer.png)
18
18
 
19
19
  - **TPS**:流式输出时的实时速度,基于 5 秒滑动窗口,约 10Hz 刷新;超过 1.5 秒没有新 token 时显示 `--`。
20
20
  - **AVG**:会话级累计平均。所有已完成消息的权威 token 数之和,除以各消息生成时长之和。
@@ -62,15 +62,16 @@ pi -e npm:@jack-yang/pi-tps-meter # 只在本次启动试用,不写
62
62
 
63
63
  | 命令 | 作用 |
64
64
  |---|---|
65
- | `/tps` | 循环切换 right → status → widget → off |
66
- | `/tps right` | 编辑器下方、右对齐(默认):紧贴 footer,位置固定 |
65
+ | `/tps` | 循环切换 footer → right → status → widget → off |
66
+ | `/tps footer` | footer 的目录那一行右端(默认),不额外占行 |
67
+ | `/tps right` | 编辑器下方单独一行,右对齐,紧贴 footer |
67
68
  | `/tps status` | footer 的扩展状态行,和其他扩展共用一行,按扩展名排序 |
68
69
  | `/tps widget` | 编辑器上方的单行组件 |
69
70
  | `/tps off` | 完全隐藏。计量仍在后台进行,切回来立即显示当前读数 |
70
71
 
71
72
  模式写入配置文件,下次启动沿用。
72
73
 
73
- 为什么右对齐是单独一行,而不是放进 footer 状态行的右端:pi 把所有扩展的状态按名字排序、左对齐拼成一行,扩展没法指定位置;要放到右端只能整个替换 pi 的 footer,不值得。编辑器下方的组件是自己渲染的,可以按终端宽度右对齐,窗口缩放时也贴着右边。
74
+ `footer` 模式是怎么做的:pi 的 footer 没有给扩展留定位的口子(扩展状态只能左对齐拼在最后一行),所以这里用 `setFooter` 挂了一个包装层:里面跑的就是 pi 自己导出的 `FooterComponent`,统计行、状态行都由 pi 自己渲染,实测和原装 footer 逐字符、连颜色都一致;只是在目录那一行右端补上读数。终端变窄时先截短目录(和 pi 一样用 `...`),读数保持完整;中文目录名按双宽字符计算。如果将来某个 pi 版本改了 footer 内部导致包装失败,会自动恢复 pi 原装 footer、退回 `right` 模式并提示一次,不会把 footer 弄坏。
74
75
 
75
76
  如果也嫌 pi-lens 的 `LSP Inactive` 占地方,pi-lens 自己有开关:在 `~/.pi-lens/config.json` 里设 `ui.compactLspStatus: true`(压成一个图标)或 `ui.hideLspStatus: true`(不显示)。
76
77
 
@@ -80,7 +81,7 @@ pi -e npm:@jack-yang/pi-tps-meter # 只在本次启动试用,不写
80
81
 
81
82
  ```json
82
83
  {
83
- "mode": "right",
84
+ "mode": "footer",
84
85
  "tiers": {
85
86
  "tps": { "slow": 20, "normal": 50, "fast": 100 },
86
87
  "ttft": { "fast": 10000, "ok": 20000 }
@@ -91,7 +92,7 @@ pi -e npm:@jack-yang/pi-tps-meter # 只在本次启动试用,不写
91
92
 
92
93
  | 键 | 默认值 | 说明 |
93
94
  |---|---|---|
94
- | `mode` | `"right"` | `right` / `status` / `widget` / `off`。`/tps` 只改写这个键,其他内容原样保留 |
95
+ | `mode` | `"footer"` | `footer` / `right` / `status` / `widget` / `off`。`/tps` 只改写这个键,其他内容原样保留 |
95
96
  | `tiers.tps` | `{ slow: 20, normal: 50, fast: 100 }` | TPS 与 AVG 的分档阈值(tok/s) |
96
97
  | `tiers.ttft` | `{ fast: 10000, ok: 20000 }` | TTFT 分档阈值(毫秒) |
97
98
  | `spinner.theme` | `"tech"` | 粒子流配色:`"tech"`(青 → 蓝 → 紫)、`"red"`(KITT 风格红);`"none"` 关闭粒子流 |
@@ -153,7 +154,7 @@ TTFT 从 `turn_start` 开始计时(在 pi 里,一个 turn 就是一次模型
153
154
 
154
155
  - **不流式输出的推理**:有些模型按推理 token 计费,但不把推理内容流式发出来。这时校准比率会顶到 3.0 上限,AVG 也会偏高(原版同样如此)。
155
156
  - **一次性返回工具参数的 provider**(如 Gemini):消息末尾会出现一个短暂的 TPS 尖峰,消息结束后立即清除。
156
- - **`status` 模式依赖 pi 的默认 footer**:如果另一个扩展换掉了 footer,并且不渲染扩展状态(比如 awesome-pi-themes 的 look-pack),用默认的 `right` 或 `widget`。
157
+ - **`footer` 和其他替换 footer 的扩展互斥**:pi 同一时间只有一个 footer,后设置的生效。如果还开着 awesome-pi-themes 的 look-pack 这类扩展,改用 `/tps right`。`status` 模式也依赖 pi 的默认 footer(look-pack 不渲染扩展状态)。
157
158
  - **AVG 和校准只在当前会话内累计**:`/new`、`/resume`、`/fork`、`/reload` 会重新开始;不会从历史记录里重建。
158
159
  - **TTFT 包含客户端准备时间**:包括请求前的上下文处理(实测约 7ms)和 provider 层的自动重试等待,也就是用户实际等待的时间。
159
160
  - **时间戳取自 pi 把事件派发给扩展的时刻**:其他扩展的慢 handler 会推迟派发,从而影响读数。
@@ -2,12 +2,13 @@
2
2
  //
3
3
  // Port of opencode-tps-meter: the measurement core (src/core.ts), the formatting (src/format.ts)
4
4
  // and the thinking particle stream (src/spinner.ts) are the original's. This file is the replaced
5
- // host layer: pi events in; a right-aligned line below the editor, a footer status, or a line
6
- // above the editor out.
5
+ // host layer: pi events in; the meter out on pi's footer line (by wrapping pi's own footer), a
6
+ // right-aligned line below the editor, a footer status, or a line above the editor.
7
7
 
8
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"
8
+ import type { ExtensionAPI, ExtensionContext, ReadonlyFooterDataProvider } from "@earendil-works/pi-coding-agent"
9
9
  import { DISPLAY_MODES, isDisplayMode, loadConfig, nextMode, resolveConfig, saveMode, type DisplayMode, type MeterConfig } from "../src/config.ts"
10
10
  import { abandonMessage, completeMessage, createMeterState, readMeter, recordDelta, startMessage, startTurn, type DeltaKind, type MeterState } from "../src/core.ts"
11
+ import { mergeFooterLine } from "../src/footer.ts"
11
12
  import { alignRight, renderLine, withSpinner } from "../src/format.ts"
12
13
  import { advanceSpinner, createSpinner, spinnerCells, type Rgb, type SpinnerState } from "../src/spinner.ts"
13
14
 
@@ -21,12 +22,61 @@ const REFRESH_MS = 100
21
22
  const SPINNER_MUTED: Rgb = { r: 128, g: 132, b: 142 }
22
23
 
23
24
  type UI = ExtensionContext["ui"]
25
+ type Model = NonNullable<ExtensionContext["model"]>
26
+
27
+ // The part of pi's AgentSession that pi's FooterComponent reads, rebuilt from what an extension
28
+ // can see. pi does not hand extensions its session, but its footer only needs these members.
29
+ export type SessionView = {
30
+ readonly sessionManager: ExtensionContext["sessionManager"]
31
+ readonly model: ExtensionContext["model"]
32
+ readonly state: { model: ExtensionContext["model"]; thinkingLevel: string }
33
+ readonly routedModel: { model: Model; thinkingLevel?: string } | undefined
34
+ readonly modelRuntime: { isUsingSubscription(provider: string): boolean }
35
+ getContextUsage(): ReturnType<ExtensionContext["getContextUsage"]>
36
+ }
37
+
38
+ export type FooterLike = { render(width: number): string[]; invalidate(): void; dispose?(): void; setAutoCompactEnabled(enabled: boolean): void }
39
+
40
+ // pi runtime pieces the footer mode needs; loaded on demand (and injected in tests, which run on
41
+ // plain Node without pi).
42
+ export type FooterKit = {
43
+ createFooter(session: SessionView, footerData: ReadonlyFooterDataProvider): FooterLike
44
+ visibleWidth(text: string): number
45
+ truncateToWidth(text: string, width: number, ellipsis?: string): string
46
+ }
47
+
48
+ export type Deps = { loadFooterKit?: () => Promise<FooterKit> }
49
+
50
+ async function loadPiFooterKit(): Promise<FooterKit> {
51
+ const [core, tuiLib] = await Promise.all([import("@earendil-works/pi-coding-agent"), import("@earendil-works/pi-tui")])
52
+ type Session = ConstructorParameters<typeof core.FooterComponent>[0]
53
+ return {
54
+ createFooter: (session, footerData) => {
55
+ // SAFETY: FooterComponent is typed to take pi's whole AgentSession, which extensions never
56
+ // get. In pi 1.5.0 (dist/modes/interactive/components/footer.js) it reads exactly the members
57
+ // SessionView provides: sessionManager.{getCwd, getEntries, getEntryCount, getLeafId,
58
+ // getSessionId, getSessionName}, model, state.{model, thinkingLevel}, routedModel,
59
+ // modelRuntime.isUsingSubscription and getContextUsage(). If a later pi reads anything else,
60
+ // render() throws, the wrapper catches it, and fallBack() restores pi's own footer.
61
+ const agentSession = session as unknown as Session
62
+ return new core.FooterComponent(agentSession, footerData)
63
+ },
64
+ visibleWidth: tuiLib.visibleWidth,
65
+ truncateToWidth: tuiLib.truncateToWidth,
66
+ }
67
+ }
24
68
 
25
69
  export default function tpsMeter(pi: ExtensionAPI): void {
70
+ createTpsMeter(pi)
71
+ }
72
+
73
+ export function createTpsMeter(pi: ExtensionAPI, deps: Deps = {}): void {
74
+ const loadFooterKit = deps.loadFooterKit ?? loadPiFooterKit
26
75
  let state: MeterState = createMeterState()
27
76
  // Defaults until session_start reads the file: the factory may run in invocations that never
28
77
  // start a session, so it does no IO and starts no timers.
29
78
  let config: MeterConfig = resolveConfig(undefined)
79
+ let ctxRef: ExtensionContext | undefined
30
80
  let ui: UI | undefined
31
81
  let tui = false
32
82
  let ticker: ReturnType<typeof setInterval> | undefined
@@ -34,18 +84,22 @@ export default function tpsMeter(pi: ExtensionAPI): void {
34
84
  // What is on screen right now, so unchanged readings cost no re-render.
35
85
  let shownMode: DisplayMode | undefined
36
86
  let shownLine: string | undefined
37
- // The mounted right-aligned component's host, told to redraw when the line changes.
38
- let rightHost: { requestRender(): void } | undefined
87
+ // The mounted component's host (widget or footer), told to redraw when the line changes.
88
+ let host: { requestRender(): void } | undefined
89
+ // Bumped on every mode change, so a footer mount still loading for an older mode is dropped.
90
+ let mountEpoch = 0
39
91
 
40
92
  // Handlers re-bind from their own ctx; the ticker only ever uses the latest binding.
41
93
  const bind = (ctx: ExtensionContext): void => {
94
+ ctxRef = ctx
42
95
  ui = ctx.hasUI ? ctx.ui : undefined
43
96
  // The TUI renders ANSI and components; RPC clients get plain text lines.
44
97
  tui = ctx.mode === "tui"
45
98
  }
46
99
 
47
100
  // The particle stream exists only while reasoning streams, and only in the TUI (in RPC it would
48
- // just be noise at 10Hz). A fresh stream starts each time thinking starts.
101
+ // just be noise at 10Hz). A fresh stream starts each time thinking starts. Next to a
102
+ // right-aligned reading it goes on the left, so the reading itself never moves.
49
103
  const decorate = (target: UI, line: string, now: number): string => {
50
104
  if (!tui || !config.spinner.enabled || !state.thinking) {
51
105
  spinner = undefined
@@ -54,29 +108,120 @@ export default function tpsMeter(pi: ExtensionAPI): void {
54
108
  if (spinner === undefined) spinner = createSpinner(config.spinner.cells, now, Math.random)
55
109
  else advanceSpinner(spinner, config.spinner, now, Math.random)
56
110
  const paint = (text: string, c: Rgb) => target.theme.style(text, { fg: { kind: "rgb", r: c.r, g: c.g, b: c.b } })
57
- return withSpinner(line, spinnerCells(spinner, config.spinner, SPINNER_MUTED), paint, config.mode === "right" ? "before" : "after")
111
+ const side = config.mode === "right" || config.mode === "footer" ? "before" : "after"
112
+ return withSpinner(line, spinnerCells(spinner, config.spinner, SPINNER_MUTED), paint, side)
58
113
  }
59
114
 
60
115
  // A component rather than a string widget: it right-aligns itself to the width of every render,
61
116
  // so it stays in the corner across terminal resizes, and an update is only a redraw request.
62
117
  const mountRight = (target: UI): void => {
63
- target.setWidget(KEY, (host) => {
64
- rightHost = host
118
+ target.setWidget(KEY, (h) => {
119
+ host = h
65
120
  return { render: (width: number) => [alignRight(shownLine ?? "", width)], invalidate() {} }
66
121
  }, { placement: "belowEditor" })
67
122
  }
68
123
 
124
+ // pi's own footer, fed a SessionView, with the meter merged into its first (directory) line.
125
+ // Everything else on the footer stays pi's rendering.
126
+ const sessionView: SessionView = {
127
+ // Only read inside a mounted footer, which exists only after bind(); a throw here is caught
128
+ // by the footer's render() and ends in fallBack().
129
+ get sessionManager() {
130
+ if (!ctxRef) throw new Error("no session bound")
131
+ return ctxRef.sessionManager
132
+ },
133
+ get model() { return ctxRef?.model },
134
+ get state() { return { model: ctxRef?.model, thinkingLevel: pi.getThinkingLevel() } },
135
+ // As pi's AgentSession.routedModel: under a virtual model, the physical model of the latest
136
+ // successful response.
137
+ get routedModel() {
138
+ const model = ctxRef?.model
139
+ if (!ctxRef || !model || model.api !== "pi-virtual") return undefined
140
+ const branch = ctxRef.sessionManager.getBranch()
141
+ for (let i = branch.length - 1; i >= 0; i--) {
142
+ const entry = branch[i]
143
+ if (entry.type !== "message" || entry.message.role !== "assistant") continue
144
+ const m = entry.message
145
+ if (m.stopReason === "error" || m.stopReason === "aborted") continue
146
+ const physical = ctxRef.modelRegistry.find(m.provider, m.model)
147
+ return physical ? { model: physical, thinkingLevel: (m as { thinkingLevel?: string }).thinkingLevel } : undefined
148
+ }
149
+ return undefined
150
+ },
151
+ // As pi's ModelRuntime.isUsingSubscription: OAuth credentials for a subscription provider.
152
+ // The footer asks this about the current model's provider; ModelRegistry.isUsingOAuth takes
153
+ // the model and reads its provider.
154
+ modelRuntime: {
155
+ isUsingSubscription: (provider) => {
156
+ const registry = ctxRef?.modelRegistry
157
+ const model = ctxRef?.model
158
+ if (!registry || model?.provider !== provider || !registry.isUsingOAuth(model)) return false
159
+ const p = registry.getProvider(provider) as { auth?: { oauth?: { isSubscription?: boolean } } } | undefined
160
+ return p?.auth?.oauth?.isSubscription === true
161
+ },
162
+ },
163
+ getContextUsage: () => ctxRef?.getContextUsage(),
164
+ }
165
+
166
+ // If pi's footer cannot be wrapped (an older or newer pi changed it), never leave the user
167
+ // without a footer: restore pi's, fall back to the line below the editor, say so once.
168
+ // If pi's footer cannot be wrapped (an older or newer pi changed it), never leave the user
169
+ // without a footer: fall back to the line below the editor (leaving footer mode is what restores
170
+ // pi's own footer, in render()) and say so once. A mount that is no longer current (the mode
171
+ // changed, or the session was replaced, since it started) is ignored, so a stale UI is never used.
172
+ const fallBack = (target: UI, epoch: number, reason: unknown): void => {
173
+ if (epoch !== mountEpoch || ui !== target || config.mode !== "footer") return
174
+ config = { ...config, mode: "right" }
175
+ target.notify(`tps-meter: footer mode unavailable here (${reason instanceof Error ? reason.message : String(reason)}), using right`, "warning")
176
+ render()
177
+ }
178
+
179
+ const mountFooter = (target: UI, epoch: number): void => {
180
+ loadFooterKit().then((kit) => {
181
+ if (epoch !== mountEpoch || ui !== target) return
182
+ target.setFooter((h, theme, footerData) => {
183
+ host = h
184
+ const inner = kit.createFooter(sessionView, footerData)
185
+ const unsubscribe = footerData.onBranchChange(() => h.requestRender())
186
+ const fns = { visibleWidth: kit.visibleWidth, truncate: (text: string, width: number) => kit.truncateToWidth(text, width, theme.fg("dim", "...")) }
187
+ return {
188
+ render(width: number): string[] {
189
+ try {
190
+ // pi feeds this flag to its own footer instance; ours reads the same setting.
191
+ inner.setAutoCompactEnabled(pi.getSettings().compaction?.enabled ?? true)
192
+ const lines = inner.render(width)
193
+ if (lines.length > 0 && shownLine) lines[0] = mergeFooterLine(lines[0], shownLine, width, fns)
194
+ return lines
195
+ } catch (err) {
196
+ // Not from inside pi's render pass: swap the footer once this frame is done.
197
+ queueMicrotask(() => fallBack(target, epoch, err))
198
+ return []
199
+ }
200
+ },
201
+ invalidate() { inner.invalidate() },
202
+ dispose() {
203
+ unsubscribe()
204
+ inner.dispose?.()
205
+ },
206
+ }
207
+ })
208
+ }, (err: unknown) => fallBack(target, epoch, err))
209
+ }
210
+
69
211
  const render = (): void => {
70
212
  if (!ui) return
71
213
  const mode = config.mode
72
214
  if (mode !== shownMode) {
73
215
  // Exactly one surface (or none) shows the meter: clear the one being left.
74
216
  if (shownMode === "status") ui.setStatus(KEY, undefined)
75
- if (shownMode === "widget" || shownMode === "right") ui.setWidget(KEY, undefined)
76
- rightHost = undefined
217
+ if (shownMode === "widget" || shownMode === "right" || (shownMode === "footer" && !tui)) ui.setWidget(KEY, undefined)
218
+ if (shownMode === "footer" && tui) ui.setFooter(undefined)
219
+ host = undefined
220
+ mountEpoch++
77
221
  shownMode = mode
78
222
  shownLine = undefined
79
223
  if (mode === "right" && tui) mountRight(ui)
224
+ if (mode === "footer" && tui) mountFooter(ui, mountEpoch)
80
225
  }
81
226
  if (mode === "off") return
82
227
  const now = Date.now()
@@ -85,8 +230,9 @@ export default function tpsMeter(pi: ExtensionAPI): void {
85
230
  shownLine = line
86
231
  if (mode === "status") ui.setStatus(KEY, line)
87
232
  else if (mode === "widget") ui.setWidget(KEY, [line])
88
- // RPC ignores component widgets; there the line goes out as plain text below the editor.
89
- else if (tui) rightHost?.requestRender()
233
+ // RPC has no custom footers or component widgets; there the line goes out as plain text
234
+ // below the editor.
235
+ else if (tui) host?.requestRender()
90
236
  else ui.setWidget(KEY, [line], { placement: "belowEditor" })
91
237
  }
92
238
 
@@ -162,16 +308,18 @@ export default function tpsMeter(pi: ExtensionAPI): void {
162
308
  })
163
309
 
164
310
  pi.on("session_shutdown", () => {
165
- // pi resets extension UI itself when it reloads or replaces the runtime; the timer and the
166
- // reference to this session's UI are the only things left for us to drop.
311
+ // pi resets extension UI itself when it reloads or replaces the runtime (including a custom
312
+ // footer); the timer and the references to this session are the only things left to drop.
167
313
  stopTicker()
314
+ mountEpoch++
168
315
  ui = undefined
169
- rightHost = undefined
316
+ ctxRef = undefined
317
+ host = undefined
170
318
  spinner = undefined
171
319
  })
172
320
 
173
321
  pi.registerCommand("tps", {
174
- description: "TPS meter display: /tps cycles right → status → widget → off, /tps right|status|widget|off sets it",
322
+ description: "TPS meter display: /tps cycles footer → right → status → widget → off, /tps footer|right|status|widget|off sets it",
175
323
  getArgumentCompletions: (prefix) => {
176
324
  // Only strict prefixes: pi's editor makes Enter apply an open argument completion instead
177
325
  // of submitting, so a fully typed mode must close the popup or `/tps off` needs two Enters.
@@ -183,7 +331,7 @@ export default function tpsMeter(pi: ExtensionAPI): void {
183
331
  bind(ctx)
184
332
  const arg = args.trim().toLowerCase()
185
333
  if (arg && !isDisplayMode(arg)) {
186
- ctx.ui.notify(`tps-meter: unknown mode "${arg}" (use right | status | widget | off)`, "error")
334
+ ctx.ui.notify(`tps-meter: unknown mode "${arg}" (use footer | right | status | widget | off)`, "error")
187
335
  return
188
336
  }
189
337
  const mode = isDisplayMode(arg) ? arg : nextMode(config.mode)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@jack-yang/pi-tps-meter",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "description": "Live TPS / AVG / TTFT meter for the pi coding agent, calibrated against authoritative token counts (port of opencode-tps-meter)",
6
6
  "type": "module",
7
7
  "keywords": [
@@ -36,7 +36,7 @@
36
36
  "extensions": [
37
37
  "./extensions/index.ts"
38
38
  ],
39
- "image": "https://raw.githubusercontent.com/yangwudong/pi-tps-meter/main/docs/screenshot-right.png"
39
+ "image": "https://raw.githubusercontent.com/yangwudong/pi-tps-meter/main/docs/screenshot-footer.png"
40
40
  },
41
41
  "engines": {
42
42
  "node": ">=22.19.0"
package/src/config.ts CHANGED
@@ -13,12 +13,13 @@ import { dirname, join } from "node:path"
13
13
  import type { Tiers } from "./format.ts"
14
14
  import { parseHex, THEME_PRESETS, type Rgb, type SpinnerConfig } from "./spinner.ts"
15
15
 
16
- // right: one line below the editor, right-aligned (a fixed spot that other extensions' footer
17
- // statuses cannot push around); status: pi's footer status line; widget: a line above the editor.
18
- export type DisplayMode = "right" | "status" | "widget" | "off"
16
+ // footer: the right end of pi's own footer line with the working directory (pi's footer is wrapped,
17
+ // not replaced); right: one line below the editor, right-aligned; status: pi's footer status line;
18
+ // widget: a line above the editor.
19
+ export type DisplayMode = "footer" | "right" | "status" | "widget" | "off"
19
20
 
20
21
  // Also the cycle order of a bare `/tps`.
21
- export const DISPLAY_MODES: readonly DisplayMode[] = ["right", "status", "widget", "off"]
22
+ export const DISPLAY_MODES: readonly DisplayMode[] = ["footer", "right", "status", "widget", "off"]
22
23
 
23
24
  export type MeterConfig = { mode: DisplayMode; tiers: Tiers; spinner: SpinnerConfig }
24
25
 
@@ -49,7 +50,7 @@ export function resolveConfig(raw: unknown): MeterConfig {
49
50
  const custom = hexColors(sp.colors)
50
51
  const preset = hexColors(THEME_PRESETS[theme] ?? THEME_PRESETS.tech)
51
52
  return {
52
- mode: isDisplayMode(o.mode) ? o.mode : "right",
53
+ mode: isDisplayMode(o.mode) ? o.mode : "footer",
53
54
  tiers: {
54
55
  tps: { slow: num(tp.slow, 20), normal: num(tp.normal, 50), fast: num(tp.fast, 100) },
55
56
  ttft: { fast: num(tt.fast, 10_000), ok: num(tt.ok, 20_000) },
package/src/footer.ts ADDED
@@ -0,0 +1,27 @@
1
+ // Puts the meter on the right end of the first line of pi's own footer (the working directory
2
+ // line), the way pi itself right-aligns the model name on the line below. Pure: the terminal
3
+ // width functions are injected (pi-tui's in pi, simple ones in tests), so this runs on plain Node.
4
+
5
+ export type WidthFns = {
6
+ // Terminal columns of a string: ANSI codes take none, CJK characters take two.
7
+ visibleWidth(text: string): number
8
+ // Cut a string to at most `width` columns; the caller decides the ellipsis (pi uses a dim "...").
9
+ truncate(text: string, width: number): string
10
+ }
11
+
12
+ // Columns kept free between the directory and the meter.
13
+ export const FOOTER_GAP = 2
14
+
15
+ // The meter is the point of this line, so on a narrow terminal the directory gives way first
16
+ // (truncated with an ellipsis, as pi itself does); the meter is only dropped when even it alone
17
+ // would not fit. Returns exactly `width` columns, padded between the two parts.
18
+ export function mergeFooterLine(left: string, meter: string, width: number, fns: WidthFns): string {
19
+ const meterWidth = fns.visibleWidth(meter)
20
+ if (meter === "" || meterWidth > width) return left
21
+ const room = width - meterWidth - FOOTER_GAP
22
+ let head = left
23
+ if (room <= 0) head = ""
24
+ else if (fns.visibleWidth(left) > room) head = fns.truncate(left, room)
25
+ const pad = width - fns.visibleWidth(head) - meterWidth
26
+ return head + " ".repeat(Math.max(0, pad)) + meter
27
+ }