@warren-dean/code-kit 0.25.0 → 0.26.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.
@@ -0,0 +1,79 @@
1
+ // A lane's character (docs/specs/27-the-mod-redesigned.md, UI-11 and UI-12), as a surface module: it
2
+ // runs on the surface's own frame clock, so only it redraws while it moves. A pose that loops (the
3
+ // kind of work) plays while its agent works; a move (arriving, handed back, sent back, merged) plays
4
+ // once, then the pose shows. Still poses, and `motion: off`, start no timer at all.
5
+ import { POSES, characterFrame } from '../characters.mjs';
6
+
7
+ const hex = (rgb) => `#${rgb.toString(16).padStart(6, '0')}`;
8
+
9
+ /** Each instance's timer and frame, by its surface (the same object across its calls). */
10
+ const running = new WeakMap();
11
+
12
+ /** A 12 by 12 grid as six rows of half-blocks: a run of cells sharing colours is one Text. */
13
+ export function halfBlocks(grid, { Box, Text }) {
14
+ const rows = [];
15
+ for (let y = 0; y < grid.length; y += 2) {
16
+ const runs = [];
17
+ for (let x = 0; x < grid[y].length; x++) {
18
+ const top = grid[y][x];
19
+ const bottom = grid[y + 1]?.[x] ?? null;
20
+ const cell =
21
+ top === null && bottom === null
22
+ ? { ch: ' ' }
23
+ : top === null
24
+ ? { ch: '▄', color: hex(bottom) }
25
+ : { ch: '▀', color: hex(top), ...(bottom === null ? {} : { bg: hex(bottom) }) };
26
+ const last = runs.at(-1);
27
+ if (last && last.color === cell.color && last.bg === cell.bg) last.text += cell.ch;
28
+ else runs.push({ text: cell.ch, color: cell.color, bg: cell.bg });
29
+ }
30
+ rows.push(
31
+ Box({
32
+ key: `row-${y}`,
33
+ flexDirection: 'row',
34
+ children: runs.map((r, i) =>
35
+ Text({
36
+ key: `run-${i}`,
37
+ ...(r.color ? { color: r.color } : {}),
38
+ ...(r.bg ? { backgroundColor: r.bg } : {}),
39
+ children: [r.text],
40
+ }),
41
+ ),
42
+ }),
43
+ );
44
+ }
45
+ return Box({ flexDirection: 'column', children: rows });
46
+ }
47
+
48
+ export default function Character(props, surface) {
49
+ const { look, pose = 'resting', move = null, motion = 'while-active' } = props ?? {};
50
+ const rec = running.get(surface) ?? { seq: null, f: 0, stop: null, played: null };
51
+ running.set(surface, rec);
52
+ const moving = motion !== 'off';
53
+ const playing = moving && move && rec.played !== move.key ? move : null;
54
+ const shown = playing ? playing.kind : pose;
55
+ const spec = POSES[shown] ?? POSES.resting;
56
+ const animate = moving && spec.frames > 1 && (playing || spec.loop);
57
+ const seq = animate ? `${playing ? `move:${move.key}` : 'pose'}:${shown}` : null;
58
+ if (seq !== rec.seq) {
59
+ rec.stop?.();
60
+ rec.stop = null;
61
+ rec.f = 0;
62
+ rec.seq = seq;
63
+ if (animate)
64
+ rec.stop = surface.every(spec.ms, () => {
65
+ rec.f += 1;
66
+ if (!spec.loop && rec.f >= spec.frames) {
67
+ // The move has played: hold its last frame no longer, show the pose.
68
+ rec.played = move.key;
69
+ rec.f = spec.frames - 1;
70
+ rec.stop?.();
71
+ rec.stop = null;
72
+ rec.seq = null;
73
+ }
74
+ surface.setState({ tick: (surface.state?.tick ?? 0) + 1 });
75
+ });
76
+ }
77
+ const frame = animate ? rec.f % spec.frames : 0;
78
+ return halfBlocks(characterFrame(shown, frame, look, { motion }), surface.elements);
79
+ }
@@ -0,0 +1,37 @@
1
+ // An agent's elapsed clock (UI-12, VIEW-4), as a surface module: it ticks on the surface's own frame
2
+ // clock while its agent works, so the pane isn't redrawn every second. Still otherwise.
3
+
4
+ /** How long, as a clock reads it: 45s, 2:05, 1h 12m. */
5
+ export function elapsed(ms) {
6
+ const s = Math.max(0, Math.floor(ms / 1000));
7
+ if (s < 60) return `${s}s`;
8
+ if (s < 3600) return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;
9
+ return `${Math.floor(s / 3600)}h ${Math.floor((s % 3600) / 60)}m`;
10
+ }
11
+
12
+ const running = new WeakMap();
13
+
14
+ export default function Clock(props, surface) {
15
+ const { ms = 0, running: on = false, motion = 'while-active', color = null } = props ?? {};
16
+ const rec = running.get(surface) ?? { base: null, ticks: 0, stop: null };
17
+ running.set(surface, rec);
18
+ if (rec.base !== ms) {
19
+ rec.base = ms;
20
+ rec.ticks = 0;
21
+ }
22
+ const tick = on && motion !== 'off';
23
+ if (tick && !rec.stop)
24
+ rec.stop = surface.every(1000, () => {
25
+ rec.ticks += 1;
26
+ surface.setState({ ticks: rec.ticks });
27
+ });
28
+ if (!tick && rec.stop) {
29
+ rec.stop();
30
+ rec.stop = null;
31
+ }
32
+ const { Text } = surface.elements;
33
+ return Text({
34
+ ...(color ? { color } : { dimColor: true }),
35
+ children: [elapsed(rec.base + rec.ticks * 1000)],
36
+ });
37
+ }
@@ -0,0 +1,33 @@
1
+ // The rail from the lead to the lanes it dispatched (UI-12), as a surface module: a dot travels along
2
+ // it while the lead's turn runs; still, it's a plain line.
3
+
4
+ const running = new WeakMap();
5
+
6
+ /** The rail as text: a line `width` long, the dot at `at` (none when null), an arrow at the end. */
7
+ export function railText(width, at) {
8
+ const n = Math.max(2, width - 1);
9
+ const cells = Array.from({ length: n }, () => '─');
10
+ if (at !== null) cells[at % n] = '•';
11
+ return `${cells.join('')}▸`;
12
+ }
13
+
14
+ export default function Rail(props, surface) {
15
+ const { width = 12, running: on = false, motion = 'while-active' } = props ?? {};
16
+ const rec = running.get(surface) ?? { at: 0, stop: null };
17
+ running.set(surface, rec);
18
+ const move = on && motion !== 'off';
19
+ if (move && !rec.stop)
20
+ rec.stop = surface.every(120, () => {
21
+ rec.at += 1;
22
+ surface.setState({ at: rec.at });
23
+ });
24
+ if (!move && rec.stop) {
25
+ rec.stop();
26
+ rec.stop = null;
27
+ }
28
+ const { Text } = surface.elements;
29
+ return Text({
30
+ ...(on ? { color: 'yellow' } : { dimColor: true }),
31
+ children: [railText(width, on ? rec.at : null)],
32
+ });
33
+ }