@accodev/hairline 0.47.0 → 0.47.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/LICENSE +21 -21
- package/README.md +69 -69
- package/package.json +29 -29
- package/pi/anim.ts +138 -138
- package/pi/index.ts +598 -598
- package/pi/line.ts +504 -504
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Marco Accorinti
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marco Accorinti
|
|
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,69 +1,69 @@
|
|
|
1
|
-
# hairline
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
Hairline is a Claude Code mod and a [Pi](https://pi.dev) extension that adds a status line under the prompt. In the terminal it is two rows of four columns over a context bar that spans the terminal. The rows show the repository with the worktree and the branch, the context use with its auto-compact mark, the activity: output tokens per second, eased every 400 ms, over a filled graph that moves every frame; while a request waits, "waiting", the seconds in white and a blue mark that runs along a dim track; while tools run, their count and each name and argument. Under it the token split: input, output, cache read and cache write in one muted gray, joined by `/`. Then 5h and 7d limits, and the month's extra usage spend against its cap while extra usage is on and the 5h or 7d limit is full. The spend comes from claude.ai's undocumented usage endpoint once a minute, with a Claude subscription login only; when that call fails, or with `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` set, the figure stays hidden. While idle the line reads the account's 5h and 7d use from the same endpoint once a minute, so other sessions' use shows too. Click the token split for cost, cache use, subagent share and how fast each limit fills. Click the model or effort to pick another, and click the session ID to copy it. Hairline takes its colors from your terminal's own palette. Hairline is tested on Claude Code 2.1.312 in the terminal. In the Claude desktop app it draws in the band above the prompt, without the folder, branch, model and effort that the app shows already. The idle mark is a white circle everywhere. In the terminal the tool glyph comes from a Nerd Font when one is installed; without one, its place stays blank. Hairline looks for one only once and keeps the answer. After you install or remove a Nerd Font, run `/hairline-fonts` to look again. The desktop app shows an emoji for it.
|
|
6
|
-
|
|
7
|
-

|
|
8
|
-
|
|
9
|
-

|
|
10
|
-
|
|
11
|
-

|
|
12
|
-
|
|
13
|
-
## Install
|
|
14
|
-
|
|
15
|
-
```sh
|
|
16
|
-
claude plugin marketplace add accodev/hairline
|
|
17
|
-
claude plugin install hairline@hairline
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
Run `/reload-plugins` in an open session to load it.
|
|
21
|
-
|
|
22
|
-
## Pi
|
|
23
|
-
|
|
24
|
-
```sh
|
|
25
|
-
pi install git:github.com/accodev/hairline
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Run `/reload` in an open session to load it. In Pi, hairline replaces the footer and keeps other extensions' status texts on a line below it. In Pi's fullscreen mode (`tuiMode`, the default) the line takes the same clicks as in Claude Code: the token split opens the panel, the model and the thinking level open pickers, and the session ID copies itself. The part under the pointer is underlined. Pi's regular mode leaves the mouse to the terminal, so commands stand in for the clicks there:
|
|
29
|
-
|
|
30
|
-
| Command | Effect |
|
|
31
|
-
|---|---|
|
|
32
|
-
| `/hairline` | Shows or hides the ledger. |
|
|
33
|
-
| `/hairline-id` | Copies the session ID. |
|
|
34
|
-
| `/hairline-fonts` | Looks for a Nerd Font again. |
|
|
35
|
-
|
|
36
|
-
The model picker offers the session's scoped models, or every model with a key. Pi's thinking level takes the place of effort. Pi's own `/model`, `Ctrl+P` and `Shift+Tab` still work. The speed shows from the first streamed chunk, without Claude Code's 1 second warm-up. The 5h and 7d limits come from the headers of Anthropic subscription responses, so other providers show none. The extra usage spend needs Pi's Anthropic login through `/login`; an Anthropic API key has none. The context bar marks where `compaction.reserveTokens` starts auto-compaction. The ledger has no subagent share, because Pi subagents run as separate sessions. Hairline keeps the font answer, the last limits and each session's turn time in `~/.pi/agent/hairline.json`. Pi's `settings.json` takes the options below under a `hairline` key, for example `"hairline": { "currency": "USD" }`.
|
|
37
|
-
|
|
38
|
-
## Settings
|
|
39
|
-
|
|
40
|
-
Claude Code keeps them in `settings.json`, under `pluginConfigs["hairline@hairline"].options`.
|
|
41
|
-
|
|
42
|
-
| Option | Default | Effect |
|
|
43
|
-
|---|---|---|
|
|
44
|
-
| `currency` | `EUR` | `USD`, `CAD`, `AUD`, `EUR`, `JPY` or `CNY`. |
|
|
45
|
-
| `usdRate` | `0.9` | Value of 1 US dollar in that currency, used when the exchange rate cannot be fetched. |
|
|
46
|
-
| `cacheTtlMinutes` | `60` | Prompt cache lifetime, 60 or 5 as your plan uses. |
|
|
47
|
-
| `speedRefreshPerSecond` | `30` | Updates of the tokens per second figure, 1 to 30. |
|
|
48
|
-
| `animationFps` | `30` | Frames per second of the graph, 1 to 60. |
|
|
49
|
-
|
|
50
|
-
## Development
|
|
51
|
-
|
|
52
|
-
```sh
|
|
53
|
-
claude plugin validate .
|
|
54
|
-
claude plugin test .
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
```sh
|
|
58
|
-
npm ci
|
|
59
|
-
npm run typecheck:pi
|
|
60
|
-
npm run test:pi
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
Those check the Pi extension in `pi/`. CI runs all five commands on every push to `main` and on pull requests. On `main` it also renders the README images from `tests/screenshots.test.ts` and pushes them to the `screenshots` branch.
|
|
64
|
-
|
|
65
|
-
```sh
|
|
66
|
-
node scripts/screenshots.mjs out
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
That renders them locally, with Chrome or Edge.
|
|
1
|
+
# hairline
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Hairline is a Claude Code mod and a [Pi](https://pi.dev) extension that adds a status line under the prompt. In the terminal it is two rows of four columns over a context bar that spans the terminal. The rows show the repository with the worktree and the branch, the context use with its auto-compact mark, the activity: output tokens per second, eased every 400 ms, over a filled graph that moves every frame; while a request waits, "waiting", the seconds in white and a blue mark that runs along a dim track; while tools run, their count and each name and argument. Under it the token split: input, output, cache read and cache write in one muted gray, joined by `/`. Then 5h and 7d limits, and the month's extra usage spend against its cap while extra usage is on and the 5h or 7d limit is full. The spend comes from claude.ai's undocumented usage endpoint once a minute, with a Claude subscription login only; when that call fails, or with `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` set, the figure stays hidden. While idle the line reads the account's 5h and 7d use from the same endpoint once a minute, so other sessions' use shows too. Click the token split for cost, cache use, subagent share and how fast each limit fills. Click the model or effort to pick another, and click the session ID to copy it. Hairline takes its colors from your terminal's own palette. Hairline is tested on Claude Code 2.1.312 in the terminal. In the Claude desktop app it draws in the band above the prompt, without the folder, branch, model and effort that the app shows already. The idle mark is a white circle everywhere. In the terminal the tool glyph comes from a Nerd Font when one is installed; without one, its place stays blank. Hairline looks for one only once and keeps the answer. After you install or remove a Nerd Font, run `/hairline-fonts` to look again. The desktop app shows an emoji for it.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
claude plugin marketplace add accodev/hairline
|
|
17
|
+
claude plugin install hairline@hairline
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Run `/reload-plugins` in an open session to load it.
|
|
21
|
+
|
|
22
|
+
## Pi
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
pi install git:github.com/accodev/hairline
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Run `/reload` in an open session to load it. In Pi, hairline replaces the footer and keeps other extensions' status texts on a line below it. In Pi's fullscreen mode (`tuiMode`, the default) the line takes the same clicks as in Claude Code: the token split opens the panel, the model and the thinking level open pickers, and the session ID copies itself. The part under the pointer is underlined. Pi's regular mode leaves the mouse to the terminal, so commands stand in for the clicks there:
|
|
29
|
+
|
|
30
|
+
| Command | Effect |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `/hairline` | Shows or hides the ledger. |
|
|
33
|
+
| `/hairline-id` | Copies the session ID. |
|
|
34
|
+
| `/hairline-fonts` | Looks for a Nerd Font again. |
|
|
35
|
+
|
|
36
|
+
The model picker offers the session's scoped models, or every model with a key. Pi's thinking level takes the place of effort. Pi's own `/model`, `Ctrl+P` and `Shift+Tab` still work. The speed shows from the first streamed chunk, without Claude Code's 1 second warm-up. The 5h and 7d limits come from the headers of Anthropic subscription responses, so other providers show none. The extra usage spend needs Pi's Anthropic login through `/login`; an Anthropic API key has none. The context bar marks where `compaction.reserveTokens` starts auto-compaction. The ledger has no subagent share, because Pi subagents run as separate sessions. Hairline keeps the font answer, the last limits and each session's turn time in `~/.pi/agent/hairline.json`. Pi's `settings.json` takes the options below under a `hairline` key, for example `"hairline": { "currency": "USD" }`.
|
|
37
|
+
|
|
38
|
+
## Settings
|
|
39
|
+
|
|
40
|
+
Claude Code keeps them in `settings.json`, under `pluginConfigs["hairline@hairline"].options`.
|
|
41
|
+
|
|
42
|
+
| Option | Default | Effect |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `currency` | `EUR` | `USD`, `CAD`, `AUD`, `EUR`, `JPY` or `CNY`. |
|
|
45
|
+
| `usdRate` | `0.9` | Value of 1 US dollar in that currency, used when the exchange rate cannot be fetched. |
|
|
46
|
+
| `cacheTtlMinutes` | `60` | Prompt cache lifetime, 60 or 5 as your plan uses. |
|
|
47
|
+
| `speedRefreshPerSecond` | `30` | Updates of the tokens per second figure, 1 to 30. |
|
|
48
|
+
| `animationFps` | `30` | Frames per second of the graph, 1 to 60. |
|
|
49
|
+
|
|
50
|
+
## Development
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
claude plugin validate .
|
|
54
|
+
claude plugin test .
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
npm ci
|
|
59
|
+
npm run typecheck:pi
|
|
60
|
+
npm run test:pi
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Those check the Pi extension in `pi/`. CI runs all five commands on every push to `main` and on pull requests. On `main` it also renders the README images from `tests/screenshots.test.ts` and pushes them to the `screenshots` branch.
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
node scripts/screenshots.mjs out
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
That renders them locally, with Chrome or Edge.
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@accodev/hairline",
|
|
3
|
-
"version": "0.47.
|
|
4
|
-
"description": "A status line for Claude Code and Pi.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"repository": "github:accodev/hairline",
|
|
8
|
-
"keywords": ["pi-package"],
|
|
9
|
-
"files": ["pi/index.ts", "pi/line.ts", "pi/anim.ts"],
|
|
10
|
-
"pi": {
|
|
11
|
-
"extensions": ["./pi/index.ts"]
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"typecheck:pi": "tsc -p pi",
|
|
15
|
-
"test:pi": "node --experimental-strip-types --no-warnings --test pi/tests.ts"
|
|
16
|
-
},
|
|
17
|
-
"peerDependencies": {
|
|
18
|
-
"@earendil-works/pi-ai": "*",
|
|
19
|
-
"@earendil-works/pi-coding-agent": "*",
|
|
20
|
-
"@earendil-works/pi-tui": "*"
|
|
21
|
-
},
|
|
22
|
-
"devDependencies": {
|
|
23
|
-
"@earendil-works/pi-ai": "1.5.0",
|
|
24
|
-
"@earendil-works/pi-coding-agent": "1.5.0",
|
|
25
|
-
"@earendil-works/pi-tui": "1.5.0",
|
|
26
|
-
"@types/node": "^22.10.2",
|
|
27
|
-
"typescript": "5.6.3"
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@accodev/hairline",
|
|
3
|
+
"version": "0.47.2",
|
|
4
|
+
"description": "A status line for Claude Code and Pi.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": "github:accodev/hairline",
|
|
8
|
+
"keywords": ["pi-package"],
|
|
9
|
+
"files": ["pi/index.ts", "pi/line.ts", "pi/anim.ts"],
|
|
10
|
+
"pi": {
|
|
11
|
+
"extensions": ["./pi/index.ts"]
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"typecheck:pi": "tsc -p pi",
|
|
15
|
+
"test:pi": "node --experimental-strip-types --no-warnings --test pi/tests.ts"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@earendil-works/pi-ai": "*",
|
|
19
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
20
|
+
"@earendil-works/pi-tui": "*"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@earendil-works/pi-ai": "1.5.0",
|
|
24
|
+
"@earendil-works/pi-coding-agent": "1.5.0",
|
|
25
|
+
"@earendil-works/pi-tui": "1.5.0",
|
|
26
|
+
"@types/node": "^22.10.2",
|
|
27
|
+
"typescript": "5.6.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/pi/anim.ts
CHANGED
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
// The readout's scramble and the graph, as in hooks/readout.tsx and hooks/graph.tsx. Pi has no
|
|
2
|
-
// Client modules, so the footer's own timer calls tick() and redraws the line.
|
|
3
|
-
|
|
4
|
-
// When the phase changes, every cell that held or will hold a character shows random ones, then
|
|
5
|
-
// the cells settle on the new text from left to right, one each frame after the first SETTLE_AT.
|
|
6
|
-
const SETTLE_AT = 2
|
|
7
|
-
const NOISE = 'abcdefghijklmnopqrstuvwxyz0123456789/'
|
|
8
|
-
const SENDING_STEP_MS = 100 // the sending time shows tenths of a second
|
|
9
|
-
|
|
10
|
-
// Steps through NOISE by frame and cell, so neighbouring cells differ and a test sees the same frames.
|
|
11
|
-
const noise = (frame: number, cell: number) => NOISE[(frame * 7 + cell * 13) % NOISE.length]!
|
|
12
|
-
|
|
13
|
-
export function frameText(from: string, to: string, frame: number): string {
|
|
14
|
-
return Array.from(to, (char, cell) => {
|
|
15
|
-
if (frame >= SETTLE_AT + cell) return char
|
|
16
|
-
return char === ' ' && (from[cell] ?? ' ') === ' ' ? ' ' : noise(frame, cell)
|
|
17
|
-
}).join('')
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// The time since the request went out, in the readout's width: 4.1s, 12.8s.
|
|
21
|
-
export function sendingText(ms: number, width: number): string {
|
|
22
|
-
return `${(Math.floor(ms / SENDING_STEP_MS) / 10).toFixed(1)}s`.padStart(width)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// The readout while a request is out: "waiting" with 1 to 3 dots, one more each DOT_MS, then the
|
|
26
|
-
// time in its last SECONDS_WIDTH cells: waiting.. 4.1s.
|
|
27
|
-
export const SECONDS_WIDTH = 6 // 123.4s
|
|
28
|
-
const DOT_MS = 400
|
|
29
|
-
export function waitingText(ms: number, width: number): string {
|
|
30
|
-
return `waiting${'.'.repeat(1 + (Math.floor(ms / DOT_MS) % 3))}`.padEnd(width - SECONDS_WIDTH) + sendingText(ms, SECONDS_WIDTH)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type Readout = { kind: string; from: string; frame: number; shown: string }
|
|
34
|
-
|
|
35
|
-
export const newReadout = (): Readout => ({ kind: 'idle', from: '', frame: Number.POSITIVE_INFINITY, shown: '' })
|
|
36
|
-
|
|
37
|
-
// A change of kind scrambles from what the readout shows; a change of text alone does not.
|
|
38
|
-
export function setReadoutKind(s: Readout, kind: string, text: string): void {
|
|
39
|
-
if (s.kind === kind) return
|
|
40
|
-
s.from = s.shown || text
|
|
41
|
-
s.kind = kind
|
|
42
|
-
s.frame = 0
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function tickReadout(s: Readout, width: number): void {
|
|
46
|
-
if (!Number.isFinite(s.frame)) return
|
|
47
|
-
s.frame = s.frame + 1 >= SETTLE_AT + width ? Number.POSITIVE_INFINITY : s.frame + 1
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const isScrambling = (s: Readout) => Number.isFinite(s.frame)
|
|
51
|
-
|
|
52
|
-
export function readoutFrame(s: Readout, target: string): string {
|
|
53
|
-
s.shown = frameText(s.from, target, s.frame)
|
|
54
|
-
return s.shown
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const CELLS = 6
|
|
58
|
-
const SAMPLES = CELLS * 2 // a braille cell holds 2 columns of 4 dots
|
|
59
|
-
// Unicode added the bottom braille row last, so its bits (0x40, 0x80) break the order.
|
|
60
|
-
const DOTS = [[0x01, 0x02, 0x04, 0x40], [0x08, 0x10, 0x20, 0x80]] as const
|
|
61
|
-
const MIN_SPAN = 0.2 // of the peak: a steadier stream than that would draw noise as big swings
|
|
62
|
-
const SWEEP_MS = 1200
|
|
63
|
-
const SWEEP_HALF_WIDTH = 2 // samples on each side of the bump's top
|
|
64
|
-
|
|
65
|
-
// From the lowest recent sample to the peak, so a steady 100 to 140 t/s fills the height.
|
|
66
|
-
function scale(samples: number[], top: number, isLive: boolean): (v: number) => number {
|
|
67
|
-
const low = isLive ? Math.min(...samples) : 0
|
|
68
|
-
const span = Math.max(top - low, top * MIN_SPAN)
|
|
69
|
-
return v => Math.min(1, Math.max(0, (v - low) / span))
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Each column also lights the rows up to the previous sample, so steep steps stay joined.
|
|
73
|
-
export function dotLine(samples: number[], at: (v: number) => number): string {
|
|
74
|
-
const rows = samples.map(v => 3 - Math.round(at(v) * 3))
|
|
75
|
-
let line = ''
|
|
76
|
-
for (let cell = 0; cell < samples.length / 2; cell++) {
|
|
77
|
-
let bits = 0
|
|
78
|
-
for (const side of [0, 1] as const) {
|
|
79
|
-
const i = cell * 2 + side
|
|
80
|
-
const from = Math.min(rows[i]!, rows[i - 1] ?? rows[i]!)
|
|
81
|
-
const to = Math.max(rows[i]!, rows[i - 1] ?? rows[i]!)
|
|
82
|
-
for (let r = from; r <= to; r++) bits |= DOTS[side][r]!
|
|
83
|
-
}
|
|
84
|
-
line += String.fromCharCode(0x2800 + bits)
|
|
85
|
-
}
|
|
86
|
-
return line
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// The samples of the bump at `ms` into the sweep; it starts and ends outside the line. A sweep
|
|
90
|
-
// runs while a request is out and nothing has come back, so the wait shows as motion.
|
|
91
|
-
export function sweepBars(ms: number, samples = SAMPLES): number[] {
|
|
92
|
-
const at = ((ms % SWEEP_MS) / SWEEP_MS) * (samples + 2 * SWEEP_HALF_WIDTH) - SWEEP_HALF_WIDTH
|
|
93
|
-
return Array.from({ length: samples }, (_, i) => Math.max(0, 1 - Math.abs(i - at) / SWEEP_HALF_WIDTH))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export type Graph = { shown: number; bars: number[]; top: number; sweepMs: number; isSettled: boolean }
|
|
97
|
-
|
|
98
|
-
// cells: the footer's columns pass a wider line than the 6 cells of the single row.
|
|
99
|
-
export const newGraph = (cells = CELLS): Graph => ({ shown: 0, bars: Array(cells * 2).fill(0), top: 1, sweepMs: 0, isSettled: true })
|
|
100
|
-
|
|
101
|
-
// target: the tokens per second while a reply streams, 0 otherwise.
|
|
102
|
-
export function tickGraph(s: Graph, frameMs: number, target: number, isSweeping: boolean): void {
|
|
103
|
-
if (s.isSettled && target === 0 && !isSweeping) return
|
|
104
|
-
const ease = 1 - 0.88 ** (frameMs / 16) // 12% of the gap per 16 ms, whatever the frame interval
|
|
105
|
-
s.shown += (target - s.shown) * ease
|
|
106
|
-
// A new sample every frame: at one each 60 ms the graph lagged the stream it drew.
|
|
107
|
-
s.bars = [...s.bars.slice(1), s.shown]
|
|
108
|
-
// The scale freezes when the stream ends, so the falling samples drop on screen. The line runs
|
|
109
|
-
// on until every sample has reached the bottom row.
|
|
110
|
-
if (target > 0) s.top = Math.max(...s.bars, 1)
|
|
111
|
-
s.sweepMs = isSweeping ? s.sweepMs + frameMs : 0
|
|
112
|
-
s.isSettled = target === 0 && !isSweeping && s.bars.every(v => v < s.top / 8)
|
|
113
|
-
if (s.isSettled) s.bars = s.bars.map(() => 0)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Filled from the bottom, one cell per 2 samples: an area reads as a level at a glance, where a
|
|
117
|
-
// thin line of dots read as noise.
|
|
118
|
-
const AREA = '⣀⣤⣶⣿'
|
|
119
|
-
export function areaLine(samples: number[], at: (v: number) => number): string {
|
|
120
|
-
return Array.from({ length: samples.length / 2 }, (_, cell) => AREA[Math.round(at((samples[cell * 2]! + samples[cell * 2 + 1]!) / 2) * (AREA.length - 1))]!).join('')
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// While a request is out the graph is a plain track, and scanCells places the head on it.
|
|
124
|
-
export function graphText(s: Graph, isLive: boolean, isSweeping: boolean): string {
|
|
125
|
-
if (isSweeping) return '─'.repeat(s.bars.length / 2)
|
|
126
|
-
return areaLine(s.bars, scale(s.bars, s.top, isLive))
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// The scan while a request is out, as in hooks/graph.tsx: a SCAN_HEAD-cell ━ runs along a ─
|
|
130
|
-
// track, one pass each SCAN_MS. No progress can be known, so the motion says only that the wait goes on.
|
|
131
|
-
const SCAN_MS = 1500
|
|
132
|
-
const SCAN_HEAD = 6
|
|
133
|
-
export function scanCells(ms: number, cells: number): [number, number, number] {
|
|
134
|
-
const at = Math.floor(((ms % SCAN_MS) / SCAN_MS) * (cells + SCAN_HEAD)) - SCAN_HEAD
|
|
135
|
-
const from = Math.min(cells, Math.max(0, at))
|
|
136
|
-
const to = Math.min(cells, Math.max(0, at + SCAN_HEAD))
|
|
137
|
-
return [from, to - from, cells - to]
|
|
138
|
-
}
|
|
1
|
+
// The readout's scramble and the graph, as in hooks/readout.tsx and hooks/graph.tsx. Pi has no
|
|
2
|
+
// Client modules, so the footer's own timer calls tick() and redraws the line.
|
|
3
|
+
|
|
4
|
+
// When the phase changes, every cell that held or will hold a character shows random ones, then
|
|
5
|
+
// the cells settle on the new text from left to right, one each frame after the first SETTLE_AT.
|
|
6
|
+
const SETTLE_AT = 2
|
|
7
|
+
const NOISE = 'abcdefghijklmnopqrstuvwxyz0123456789/'
|
|
8
|
+
const SENDING_STEP_MS = 100 // the sending time shows tenths of a second
|
|
9
|
+
|
|
10
|
+
// Steps through NOISE by frame and cell, so neighbouring cells differ and a test sees the same frames.
|
|
11
|
+
const noise = (frame: number, cell: number) => NOISE[(frame * 7 + cell * 13) % NOISE.length]!
|
|
12
|
+
|
|
13
|
+
export function frameText(from: string, to: string, frame: number): string {
|
|
14
|
+
return Array.from(to, (char, cell) => {
|
|
15
|
+
if (frame >= SETTLE_AT + cell) return char
|
|
16
|
+
return char === ' ' && (from[cell] ?? ' ') === ' ' ? ' ' : noise(frame, cell)
|
|
17
|
+
}).join('')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// The time since the request went out, in the readout's width: 4.1s, 12.8s.
|
|
21
|
+
export function sendingText(ms: number, width: number): string {
|
|
22
|
+
return `${(Math.floor(ms / SENDING_STEP_MS) / 10).toFixed(1)}s`.padStart(width)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The readout while a request is out: "waiting" with 1 to 3 dots, one more each DOT_MS, then the
|
|
26
|
+
// time in its last SECONDS_WIDTH cells: waiting.. 4.1s.
|
|
27
|
+
export const SECONDS_WIDTH = 6 // 123.4s
|
|
28
|
+
const DOT_MS = 400
|
|
29
|
+
export function waitingText(ms: number, width: number): string {
|
|
30
|
+
return `waiting${'.'.repeat(1 + (Math.floor(ms / DOT_MS) % 3))}`.padEnd(width - SECONDS_WIDTH) + sendingText(ms, SECONDS_WIDTH)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type Readout = { kind: string; from: string; frame: number; shown: string }
|
|
34
|
+
|
|
35
|
+
export const newReadout = (): Readout => ({ kind: 'idle', from: '', frame: Number.POSITIVE_INFINITY, shown: '' })
|
|
36
|
+
|
|
37
|
+
// A change of kind scrambles from what the readout shows; a change of text alone does not.
|
|
38
|
+
export function setReadoutKind(s: Readout, kind: string, text: string): void {
|
|
39
|
+
if (s.kind === kind) return
|
|
40
|
+
s.from = s.shown || text
|
|
41
|
+
s.kind = kind
|
|
42
|
+
s.frame = 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function tickReadout(s: Readout, width: number): void {
|
|
46
|
+
if (!Number.isFinite(s.frame)) return
|
|
47
|
+
s.frame = s.frame + 1 >= SETTLE_AT + width ? Number.POSITIVE_INFINITY : s.frame + 1
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const isScrambling = (s: Readout) => Number.isFinite(s.frame)
|
|
51
|
+
|
|
52
|
+
export function readoutFrame(s: Readout, target: string): string {
|
|
53
|
+
s.shown = frameText(s.from, target, s.frame)
|
|
54
|
+
return s.shown
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const CELLS = 6
|
|
58
|
+
const SAMPLES = CELLS * 2 // a braille cell holds 2 columns of 4 dots
|
|
59
|
+
// Unicode added the bottom braille row last, so its bits (0x40, 0x80) break the order.
|
|
60
|
+
const DOTS = [[0x01, 0x02, 0x04, 0x40], [0x08, 0x10, 0x20, 0x80]] as const
|
|
61
|
+
const MIN_SPAN = 0.2 // of the peak: a steadier stream than that would draw noise as big swings
|
|
62
|
+
const SWEEP_MS = 1200
|
|
63
|
+
const SWEEP_HALF_WIDTH = 2 // samples on each side of the bump's top
|
|
64
|
+
|
|
65
|
+
// From the lowest recent sample to the peak, so a steady 100 to 140 t/s fills the height.
|
|
66
|
+
function scale(samples: number[], top: number, isLive: boolean): (v: number) => number {
|
|
67
|
+
const low = isLive ? Math.min(...samples) : 0
|
|
68
|
+
const span = Math.max(top - low, top * MIN_SPAN)
|
|
69
|
+
return v => Math.min(1, Math.max(0, (v - low) / span))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Each column also lights the rows up to the previous sample, so steep steps stay joined.
|
|
73
|
+
export function dotLine(samples: number[], at: (v: number) => number): string {
|
|
74
|
+
const rows = samples.map(v => 3 - Math.round(at(v) * 3))
|
|
75
|
+
let line = ''
|
|
76
|
+
for (let cell = 0; cell < samples.length / 2; cell++) {
|
|
77
|
+
let bits = 0
|
|
78
|
+
for (const side of [0, 1] as const) {
|
|
79
|
+
const i = cell * 2 + side
|
|
80
|
+
const from = Math.min(rows[i]!, rows[i - 1] ?? rows[i]!)
|
|
81
|
+
const to = Math.max(rows[i]!, rows[i - 1] ?? rows[i]!)
|
|
82
|
+
for (let r = from; r <= to; r++) bits |= DOTS[side][r]!
|
|
83
|
+
}
|
|
84
|
+
line += String.fromCharCode(0x2800 + bits)
|
|
85
|
+
}
|
|
86
|
+
return line
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The samples of the bump at `ms` into the sweep; it starts and ends outside the line. A sweep
|
|
90
|
+
// runs while a request is out and nothing has come back, so the wait shows as motion.
|
|
91
|
+
export function sweepBars(ms: number, samples = SAMPLES): number[] {
|
|
92
|
+
const at = ((ms % SWEEP_MS) / SWEEP_MS) * (samples + 2 * SWEEP_HALF_WIDTH) - SWEEP_HALF_WIDTH
|
|
93
|
+
return Array.from({ length: samples }, (_, i) => Math.max(0, 1 - Math.abs(i - at) / SWEEP_HALF_WIDTH))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type Graph = { shown: number; bars: number[]; top: number; sweepMs: number; isSettled: boolean }
|
|
97
|
+
|
|
98
|
+
// cells: the footer's columns pass a wider line than the 6 cells of the single row.
|
|
99
|
+
export const newGraph = (cells = CELLS): Graph => ({ shown: 0, bars: Array(cells * 2).fill(0), top: 1, sweepMs: 0, isSettled: true })
|
|
100
|
+
|
|
101
|
+
// target: the tokens per second while a reply streams, 0 otherwise.
|
|
102
|
+
export function tickGraph(s: Graph, frameMs: number, target: number, isSweeping: boolean): void {
|
|
103
|
+
if (s.isSettled && target === 0 && !isSweeping) return
|
|
104
|
+
const ease = 1 - 0.88 ** (frameMs / 16) // 12% of the gap per 16 ms, whatever the frame interval
|
|
105
|
+
s.shown += (target - s.shown) * ease
|
|
106
|
+
// A new sample every frame: at one each 60 ms the graph lagged the stream it drew.
|
|
107
|
+
s.bars = [...s.bars.slice(1), s.shown]
|
|
108
|
+
// The scale freezes when the stream ends, so the falling samples drop on screen. The line runs
|
|
109
|
+
// on until every sample has reached the bottom row.
|
|
110
|
+
if (target > 0) s.top = Math.max(...s.bars, 1)
|
|
111
|
+
s.sweepMs = isSweeping ? s.sweepMs + frameMs : 0
|
|
112
|
+
s.isSettled = target === 0 && !isSweeping && s.bars.every(v => v < s.top / 8)
|
|
113
|
+
if (s.isSettled) s.bars = s.bars.map(() => 0)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Filled from the bottom, one cell per 2 samples: an area reads as a level at a glance, where a
|
|
117
|
+
// thin line of dots read as noise.
|
|
118
|
+
const AREA = '⣀⣤⣶⣿'
|
|
119
|
+
export function areaLine(samples: number[], at: (v: number) => number): string {
|
|
120
|
+
return Array.from({ length: samples.length / 2 }, (_, cell) => AREA[Math.round(at((samples[cell * 2]! + samples[cell * 2 + 1]!) / 2) * (AREA.length - 1))]!).join('')
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// While a request is out the graph is a plain track, and scanCells places the head on it.
|
|
124
|
+
export function graphText(s: Graph, isLive: boolean, isSweeping: boolean): string {
|
|
125
|
+
if (isSweeping) return '─'.repeat(s.bars.length / 2)
|
|
126
|
+
return areaLine(s.bars, scale(s.bars, s.top, isLive))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// The scan while a request is out, as in hooks/graph.tsx: a SCAN_HEAD-cell ━ runs along a ─
|
|
130
|
+
// track, one pass each SCAN_MS. No progress can be known, so the motion says only that the wait goes on.
|
|
131
|
+
const SCAN_MS = 1500
|
|
132
|
+
const SCAN_HEAD = 6
|
|
133
|
+
export function scanCells(ms: number, cells: number): [number, number, number] {
|
|
134
|
+
const at = Math.floor(((ms % SCAN_MS) / SCAN_MS) * (cells + SCAN_HEAD)) - SCAN_HEAD
|
|
135
|
+
const from = Math.min(cells, Math.max(0, at))
|
|
136
|
+
const to = Math.min(cells, Math.max(0, at + SCAN_HEAD))
|
|
137
|
+
return [from, to - from, cells - to]
|
|
138
|
+
}
|