your-attention 0.1.0 → 0.1.1
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/README.md +2 -0
- package/bin/your-attention.js +6 -2
- package/dist/src/adapters/files.js +66 -0
- package/dist/src/adapters/mdns.js +53 -0
- package/dist/src/adapters/mermaid.js +50 -0
- package/dist/src/adapters/openui-body.js +40 -0
- package/dist/src/adapters/openui-library.js +3 -0
- package/dist/src/adapters/sqlite.js +68 -0
- package/dist/src/cli.js +814 -0
- package/dist/src/client.js +459 -0
- package/dist/src/cloudflare/collector.js +53 -0
- package/dist/src/cloudflare/inbox-object.js +338 -0
- package/dist/src/cloudflare/storage.js +151 -0
- package/dist/src/cloudflare/worker.js +189 -0
- package/dist/src/core/crypto.js +23 -0
- package/dist/src/core/diagrams.js +225 -0
- package/dist/src/core/expr.js +125 -0
- package/dist/src/core/file-links.js +48 -0
- package/dist/src/core/hooks.js +74 -0
- package/dist/src/core/http.js +651 -0
- package/dist/src/core/inbox.js +2463 -0
- package/dist/src/core/pin.js +234 -0
- package/dist/src/core/sealed.js +167 -0
- package/dist/src/core/session-link.js +13 -0
- package/dist/src/core/socket.js +88 -0
- package/dist/src/core/storage.js +102 -0
- package/dist/src/core/summary.js +149 -0
- package/dist/src/core/telemetry.js +221 -0
- package/dist/src/core/types.js +470 -0
- package/dist/src/core/version.js +2 -0
- package/dist/src/core/widgets.js +128 -0
- package/dist/src/extensions/triage.js +232 -0
- package/dist/src/integrations/claude/ask-hook.js +84 -0
- package/dist/src/integrations/claude/channel-flag.js +102 -0
- package/dist/src/integrations/claude/channel.js +276 -0
- package/dist/src/integrations/claude/session-name.js +32 -0
- package/dist/src/integrations/claude/turn-end-hook.js +84 -0
- package/dist/src/integrations/herdr/approve.js +71 -0
- package/dist/src/integrations/herdr/blocked.js +210 -0
- package/dist/src/integrations/herdr/dialog.js +64 -0
- package/dist/src/integrations/herdr/focus.js +31 -0
- package/dist/src/integrations/herdr/place.js +22 -0
- package/dist/src/integrations/images.js +43 -0
- package/dist/src/integrations/opencode/install.js +24 -0
- package/dist/src/integrations/opencode/plugin.js +157 -0
- package/dist/src/integrations/pi/extension.js +166 -0
- package/dist/src/integrations/run-tool.js +157 -0
- package/dist/src/integrations/secrets.js +63 -0
- package/dist/src/integrations/tags.js +27 -0
- package/dist/src/integrations/tools.js +147 -0
- package/dist/src/node/home.js +20 -0
- package/dist/src/node/install.js +225 -0
- package/dist/src/node/links.js +249 -0
- package/dist/src/node/pin-prompt.js +125 -0
- package/dist/src/node/server.js +320 -0
- package/dist/src/node/service.js +74 -0
- package/dist/src/node/telemetry.js +127 -0
- package/dist/src/owner-watch.js +49 -0
- package/dist/src/tui/api.js +203 -0
- package/dist/src/tui/app.js +586 -0
- package/dist/src/tui/body.js +727 -0
- package/dist/src/tui/cardview.js +453 -0
- package/dist/src/tui/doc.js +157 -0
- package/dist/src/tui/edit.js +119 -0
- package/dist/src/tui/input.js +192 -0
- package/dist/src/tui/md.js +196 -0
- package/dist/src/tui/screen.js +225 -0
- package/dist/src/tui/terminal.js +130 -0
- package/dist/src/tui/text.js +103 -0
- package/dist/src/tui/theme.js +105 -0
- package/dist/src/tui/view.js +314 -0
- package/dist/src/watch.js +104 -0
- package/dist/web/age.js +16 -0
- package/dist/web/answers.js +485 -0
- package/dist/web/app.js +739 -0
- package/dist/web/body-faults.js +69 -0
- package/dist/web/components.js +923 -0
- package/dist/web/copy.js +118 -0
- package/dist/web/details.js +59 -0
- package/dist/web/env.js +97 -0
- package/dist/web/favicon-waiting.svg +1 -0
- package/dist/web/favicon.svg +1 -0
- package/dist/web/index.html +16 -0
- package/dist/web/library-spec.js +33 -0
- package/dist/web/markdown.js +142 -0
- package/dist/web/paste-images.js +193 -0
- package/dist/web/pin-key.js +125 -0
- package/dist/web/pin.js +126 -0
- package/dist/web/queue.js +97 -0
- package/dist/web/sealed.js +13 -0
- package/dist/web/search.js +70 -0
- package/dist/web/stories/cards.stories.js +144 -0
- package/dist/web/stories/page.stories.js +54 -0
- package/dist/web/stories/render.js +16 -0
- package/dist/web/style.css +417 -0
- package/package.json +4 -2
- package/src/core/version.ts +1 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Editing text in a field: the value, the cursor, and the keys a person expects in a terminal text box.
|
|
2
|
+
// Lines wrap at the box's width by character, so each screen position maps to one place in the text.
|
|
3
|
+
|
|
4
|
+
import { clusterWidth, graphemes } from './text.js';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/** The text as rows of at most `width` columns, with the index in the value where each row starts and ends. */
|
|
9
|
+
export function editRows(value , width ) {
|
|
10
|
+
const w = Math.max(1, width);
|
|
11
|
+
const rows = [];
|
|
12
|
+
let at = 0;
|
|
13
|
+
for (const line of value.split('\n')) {
|
|
14
|
+
let start = at;
|
|
15
|
+
let text = '';
|
|
16
|
+
let used = 0;
|
|
17
|
+
for (const g of graphemes(line)) {
|
|
18
|
+
const cw = clusterWidth(g);
|
|
19
|
+
if (used + cw > w && text) { rows.push({ text, start, end: at }); start = at; text = ''; used = 0; }
|
|
20
|
+
text += g;
|
|
21
|
+
used += cw;
|
|
22
|
+
at += g.length;
|
|
23
|
+
}
|
|
24
|
+
rows.push({ text, start, end: at });
|
|
25
|
+
at += 1;
|
|
26
|
+
}
|
|
27
|
+
return rows;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The row and column of the cursor. */
|
|
31
|
+
export function cursorPos(state , width ) {
|
|
32
|
+
const rows = editRows(state.value, width);
|
|
33
|
+
for (let r = 0; r < rows.length; r++) {
|
|
34
|
+
const row = rows[r] ;
|
|
35
|
+
const next = rows[r + 1];
|
|
36
|
+
// At a wrap point the cursor sits at the start of the next row, unless the row ends the line.
|
|
37
|
+
if (state.cursor >= row.start && (state.cursor < row.end || (state.cursor === row.end && (!next || next.start !== row.end)))) {
|
|
38
|
+
return { row: r, col: colOf(row.text, state.cursor - row.start) };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const last = rows.length - 1;
|
|
42
|
+
return { row: last, col: colOf(rows[last] .text, rows[last] .text.length) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const colOf = (text , chars ) => {
|
|
46
|
+
let col = 0;
|
|
47
|
+
let n = 0;
|
|
48
|
+
for (const g of graphemes(text)) { if (n >= chars) break; n += g.length; col += clusterWidth(g); }
|
|
49
|
+
return col;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** The index in the value at a row and column, as a click lands. */
|
|
53
|
+
export function indexAt(value , width , row , col ) {
|
|
54
|
+
const rows = editRows(value, width);
|
|
55
|
+
const r = rows[Math.max(0, Math.min(row, rows.length - 1))] ;
|
|
56
|
+
let at = r.start;
|
|
57
|
+
let used = 0;
|
|
58
|
+
for (const g of graphemes(r.text)) {
|
|
59
|
+
const cw = clusterWidth(g);
|
|
60
|
+
if (used + cw / 2 > col) break;
|
|
61
|
+
used += cw;
|
|
62
|
+
at += g.length;
|
|
63
|
+
}
|
|
64
|
+
return at;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const prevBoundary = (v , i ) => {
|
|
68
|
+
let at = 0;
|
|
69
|
+
for (const g of graphemes(v.slice(0, i))) { if (at + g.length >= i) return at; at += g.length; }
|
|
70
|
+
return 0;
|
|
71
|
+
};
|
|
72
|
+
const nextBoundary = (v , i ) => i + (graphemes(v.slice(i))[0]?.length ?? 0);
|
|
73
|
+
/** The start of the grapheme that holds index i, so a word jump never stops inside one. */
|
|
74
|
+
const snap = (v , i ) => { let at = 0; for (const g of graphemes(v)) { if (at + g.length > i) return at; at += g.length; } return v.length; };
|
|
75
|
+
const wordLeft = (v , i ) => { let j = i; while (j > 0 && /\s/.test(v[j - 1] )) j--; while (j > 0 && !/\s/.test(v[j - 1] )) j--; return snap(v, j); };
|
|
76
|
+
const wordRight = (v , i ) => { let j = i; while (j < v.length && /\s/.test(v[j] )) j++; while (j < v.length && !/\s/.test(v[j] )) j++; return j < v.length ? snap(v, j) : j; };
|
|
77
|
+
const lineStart = (v , i ) => (i <= 0 ? 0 : v.lastIndexOf('\n', i - 1) + 1);
|
|
78
|
+
const lineEnd = (v , i ) => { const e = v.indexOf('\n', i); return e < 0 ? v.length : e; };
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The state after a key, or undefined when the field does not use the key, such as Tab or, in a one-line field,
|
|
82
|
+
* Up and Down. Enter adds a line in a multi-line field.
|
|
83
|
+
*/
|
|
84
|
+
export function editKey(state , ev , opts ) {
|
|
85
|
+
const { value: v, cursor: c } = state;
|
|
86
|
+
const set = (value , cursor ) => ({ value, cursor: Math.max(0, Math.min(value.length, cursor)) });
|
|
87
|
+
if (ev.text !== undefined && !ev.ctrl && !ev.alt) return set(v.slice(0, c) + ev.text + v.slice(c), c + ev.text.length);
|
|
88
|
+
const name = ev.name;
|
|
89
|
+
if (name === 'enter' && !ev.ctrl && !ev.alt) return opts.multiline ? set(`${v.slice(0, c)}\n${v.slice(c)}`, c + 1) : undefined;
|
|
90
|
+
if (name === 'backspace') {
|
|
91
|
+
if (ev.ctrl || ev.alt) { const j = wordLeft(v, c); return set(v.slice(0, j) + v.slice(c), j); }
|
|
92
|
+
const j = prevBoundary(v, c);
|
|
93
|
+
return set(v.slice(0, j) + v.slice(c), j);
|
|
94
|
+
}
|
|
95
|
+
if (name === 'delete') return set(v.slice(0, c) + v.slice(nextBoundary(v, c)), c);
|
|
96
|
+
if (name === 'left') return set(v, ev.ctrl || ev.alt ? wordLeft(v, c) : prevBoundary(v, c));
|
|
97
|
+
if (name === 'right') return set(v, ev.ctrl || ev.alt ? wordRight(v, c) : nextBoundary(v, c));
|
|
98
|
+
if (name === 'home' || (ev.ctrl && name === 'a')) return set(v, lineStart(v, c));
|
|
99
|
+
if (name === 'end' || (ev.ctrl && name === 'e')) return set(v, lineEnd(v, c));
|
|
100
|
+
if (ev.alt && name === 'b') return set(v, wordLeft(v, c));
|
|
101
|
+
if (ev.alt && name === 'f') return set(v, wordRight(v, c));
|
|
102
|
+
if (ev.ctrl && name === 'w') { const j = wordLeft(v, c); return set(v.slice(0, j) + v.slice(c), j); }
|
|
103
|
+
if (ev.ctrl && name === 'u') { const j = lineStart(v, c); return set(v.slice(0, j) + v.slice(c), j); }
|
|
104
|
+
if (ev.ctrl && name === 'k') { const j = lineEnd(v, c); return set(v.slice(0, c) + v.slice(j === c && j < v.length ? j + 1 : j), c); }
|
|
105
|
+
if ((name === 'up' || name === 'down') && opts.multiline) {
|
|
106
|
+
const pos = cursorPos(state, opts.width);
|
|
107
|
+
const rows = editRows(v, opts.width);
|
|
108
|
+
const row = pos.row + (name === 'up' ? -1 : 1);
|
|
109
|
+
if (row < 0 || row >= rows.length) return undefined;
|
|
110
|
+
return set(v, indexAt(v, opts.width, row, pos.col));
|
|
111
|
+
}
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Pasted text goes in at the cursor. A one-line field takes the lines joined with spaces. */
|
|
116
|
+
export function editPaste(state , text , multiline ) {
|
|
117
|
+
const t = multiline ? text : text.replace(/\n+/g, ' ');
|
|
118
|
+
return { value: state.value.slice(0, state.cursor) + t + state.value.slice(state.cursor), cursor: state.cursor + t.length };
|
|
119
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Bytes from the terminal, decoded into keys, mouse events, pastes and focus changes. It reads the legacy encodings,
|
|
2
|
+
// xterm's modifyOtherKeys and the kitty keyboard protocol, SGR mouse reports (mode 1006) and bracketed paste (mode 2004).
|
|
3
|
+
// A sequence can arrive split across reads, so an unfinished one waits for the next read. A lone Escape waits too:
|
|
4
|
+
// the caller calls flush() after a short pause, the way pi's StdinBuffer, adapted from OpenTUI, tells Escape from a sequence.
|
|
5
|
+
import { graphemes } from './text.js';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const CSI = /^\x1b\[([<?>=]?)([\d;:]*)([\x20-\x2f]*)([\x40-\x7e])/;
|
|
28
|
+
const CSI_PREFIX = /^\x1b\[[<?>=]?[\d;:]*[\x20-\x2f]*$/;
|
|
29
|
+
const PASTE_END = '\x1b[201~';
|
|
30
|
+
|
|
31
|
+
const TILDE = { 1: 'home', 2: 'insert', 3: 'delete', 4: 'end', 5: 'pageup', 6: 'pagedown', 7: 'home', 8: 'end', 11: 'f1', 12: 'f2', 13: 'f3', 14: 'f4', 15: 'f5', 17: 'f6', 18: 'f7', 19: 'f8', 20: 'f9', 21: 'f10', 23: 'f11', 24: 'f12' };
|
|
32
|
+
const LETTER = { A: 'up', B: 'down', C: 'right', D: 'left', H: 'home', F: 'end', P: 'f1', Q: 'f2', R: 'f3', S: 'f4' };
|
|
33
|
+
|
|
34
|
+
function key(name , mods = {}, text ) {
|
|
35
|
+
const e = { type: 'key', name, ctrl: !!mods.ctrl, alt: !!mods.alt, shift: !!mods.shift };
|
|
36
|
+
if (text !== undefined && !e.ctrl && !e.alt) e.text = text;
|
|
37
|
+
return e;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The modifiers of a CSI parameter: 1 plus shift 1, alt 2 and ctrl 4. */
|
|
41
|
+
function mods(n ) {
|
|
42
|
+
const m = Math.max(0, (n ?? 1) - 1);
|
|
43
|
+
return { shift: !!(m & 1), alt: !!(m & 2), ctrl: !!(m & 4) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A key by its Unicode code point, as kitty and modifyOtherKeys report it. */
|
|
47
|
+
function codeKey(code , m ) {
|
|
48
|
+
if (code === 13 || code === 57414) return key('enter', m);
|
|
49
|
+
if (code === 9) return key('tab', m);
|
|
50
|
+
if (code === 27) return key('escape', m);
|
|
51
|
+
if (code === 127 || code === 8) return key('backspace', m);
|
|
52
|
+
if (code === 32) return key('space', m, ' ');
|
|
53
|
+
if (!Number.isInteger(code) || code < 0 || code > 0x10ffff) return key('unknown', m);
|
|
54
|
+
let ch = String.fromCodePoint(code);
|
|
55
|
+
if (m.shift && /^[a-z]$/.test(ch)) ch = ch.toUpperCase();
|
|
56
|
+
return key(ch, m, ch);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One byte below 0x20, or 0x7f. */
|
|
60
|
+
function controlKey(c ) {
|
|
61
|
+
const b = c.charCodeAt(0);
|
|
62
|
+
if (c === '\r') return key('enter');
|
|
63
|
+
// Control+J. Some terminals send it for Control+Enter.
|
|
64
|
+
if (c === '\n') return key('enter', { ctrl: true });
|
|
65
|
+
if (c === '\t') return key('tab');
|
|
66
|
+
if (b === 0x7f) return key('backspace');
|
|
67
|
+
if (b === 0x08) return key('backspace', { ctrl: true });
|
|
68
|
+
if (b === 0) return key('space', { ctrl: true });
|
|
69
|
+
if (b <= 0x1a) return key(String.fromCharCode(b + 96), { ctrl: true });
|
|
70
|
+
return key(String.fromCharCode(b + 64), { ctrl: true });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class Decoder {
|
|
74
|
+
buf = '';
|
|
75
|
+
paste = null;
|
|
76
|
+
|
|
77
|
+
/** Whether part of a sequence waits for more bytes, so the caller should call flush() after a short pause. */
|
|
78
|
+
get pending() {
|
|
79
|
+
return this.buf.length > 0 && this.paste === null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
feed(chunk ) {
|
|
83
|
+
this.buf += chunk;
|
|
84
|
+
return this.drain(false);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** What is left, read as keys: a lone Escape, or Alt with the key after it. */
|
|
88
|
+
flush() {
|
|
89
|
+
return this.drain(true);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
drain(final ) {
|
|
93
|
+
const out = [];
|
|
94
|
+
for (;;) {
|
|
95
|
+
if (this.paste !== null) {
|
|
96
|
+
const end = this.buf.indexOf(PASTE_END);
|
|
97
|
+
if (end < 0) {
|
|
98
|
+
// Keep a tail that may be the start of the end marker.
|
|
99
|
+
const keep = Math.min(this.buf.length, PASTE_END.length - 1);
|
|
100
|
+
let cut = this.buf.length;
|
|
101
|
+
for (let k = keep; k > 0; k--) if (PASTE_END.startsWith(this.buf.slice(-k))) { cut = this.buf.length - k; break; }
|
|
102
|
+
this.paste += this.buf.slice(0, cut);
|
|
103
|
+
this.buf = this.buf.slice(cut);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
out.push({ type: 'paste', text: (this.paste + this.buf.slice(0, end)).replace(/\r\n?/g, '\n') });
|
|
107
|
+
this.buf = this.buf.slice(end + PASTE_END.length);
|
|
108
|
+
this.paste = null;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!this.buf) return out;
|
|
112
|
+
const s = this.buf;
|
|
113
|
+
const c = s[0] ;
|
|
114
|
+
if (c !== '\x1b') {
|
|
115
|
+
if (c < ' ' || c === '\x7f') { out.push(controlKey(c)); this.buf = s.slice(1); continue; }
|
|
116
|
+
// A run of printable text: one key per grapheme.
|
|
117
|
+
let j = 0;
|
|
118
|
+
while (j < s.length && s[j] >= ' ' && s[j] !== '\x7f' && s[j] !== '\x1b') j++;
|
|
119
|
+
for (const g of graphemes(s.slice(0, j))) out.push(key(g === ' ' ? 'space' : g, { shift: /^[A-Z]$/.test(g) }, g));
|
|
120
|
+
this.buf = s.slice(j);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (s.length === 1) {
|
|
124
|
+
if (!final) return out;
|
|
125
|
+
out.push(key('escape'));
|
|
126
|
+
this.buf = '';
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (s[1] === '[') {
|
|
130
|
+
const m = CSI.exec(s);
|
|
131
|
+
if (!m) {
|
|
132
|
+
if (CSI_PREFIX.test(s) && !final) return out;
|
|
133
|
+
// Not a sequence this decoder knows: drop the escape and read the rest as keys.
|
|
134
|
+
out.push(key('[', { alt: true }));
|
|
135
|
+
this.buf = s.slice(2);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
this.buf = s.slice(m[0].length);
|
|
139
|
+
const ev = this.csi(m[1] , m[2] , m[4] );
|
|
140
|
+
if (ev) out.push(ev);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (s[1] === 'O') {
|
|
144
|
+
if (s.length === 2) { if (!final) return out; out.push(key('O', { alt: true, shift: true })); this.buf = ''; continue; }
|
|
145
|
+
const name = LETTER[s[2] ];
|
|
146
|
+
out.push(name ? key(name) : key(s[2] , { alt: true }));
|
|
147
|
+
this.buf = s.slice(3);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (s[1] === '\x1b') { out.push(key('escape')); this.buf = s.slice(1); continue; }
|
|
151
|
+
// Escape and another key: Alt and that key.
|
|
152
|
+
const next = graphemes(s.slice(1))[0] ?? '';
|
|
153
|
+
const k = next < ' ' || next === '\x7f' ? controlKey(next) : key(next === ' ' ? 'space' : next, { shift: /^[A-Z]$/.test(next) });
|
|
154
|
+
out.push({ ...k, alt: true, text: undefined });
|
|
155
|
+
this.buf = s.slice(1 + next.length);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
csi(prefix , params , final ) {
|
|
160
|
+
const n = params.split(';').map((p) => (p === '' ? undefined : Number(p.split(':')[0])));
|
|
161
|
+
if (prefix === '<' && (final === 'M' || final === 'm')) {
|
|
162
|
+
const b = n[0] ?? 0;
|
|
163
|
+
const x = (n[1] ?? 1) - 1;
|
|
164
|
+
const y = (n[2] ?? 1) - 1;
|
|
165
|
+
const m = { shift: !!(b & 4), alt: !!(b & 8), ctrl: !!(b & 16) };
|
|
166
|
+
const button = b & 3;
|
|
167
|
+
if (b & 64) return { type: 'mouse', action: button === 0 ? 'wheel-up' : 'wheel-down', button: 0, x, y, ...m };
|
|
168
|
+
if (b & 32) return { type: 'mouse', action: button === 3 ? 'move' : 'drag', button: button === 3 ? 0 : button, x, y, ...m };
|
|
169
|
+
return { type: 'mouse', action: final === 'M' ? 'down' : 'up', button, x, y, ...m };
|
|
170
|
+
}
|
|
171
|
+
if (prefix === '?' && final === 'u') return { type: 'reply', kind: 'kitty', value: params };
|
|
172
|
+
if (prefix === '?' && final === 'c') return { type: 'reply', kind: 'attributes', value: params };
|
|
173
|
+
if (prefix) return undefined;
|
|
174
|
+
// Kitty reports a key's release as event type 3, after a colon in the modifiers. The client reads presses only.
|
|
175
|
+
if (/;\d+:3(;|$)/.test(params)) return undefined;
|
|
176
|
+
if (final === 'I' && !params) return { type: 'focus', on: true };
|
|
177
|
+
if (final === 'O' && !params) return { type: 'focus', on: false };
|
|
178
|
+
if (final === 'Z') return key('tab', { shift: true });
|
|
179
|
+
if (final === '~') {
|
|
180
|
+
const code = n[0] ?? 0;
|
|
181
|
+
if (code === 200) { this.paste = ''; return undefined; }
|
|
182
|
+
if (code === 27 && n[2] !== undefined) return codeKey(n[2], mods(n[1]));
|
|
183
|
+
const name = TILDE[code];
|
|
184
|
+
return name ? key(name, mods(n[1])) : undefined;
|
|
185
|
+
}
|
|
186
|
+
if (final === 'u') {
|
|
187
|
+
return codeKey(n[0] ?? 0, mods(n[1]));
|
|
188
|
+
}
|
|
189
|
+
const name = LETTER[final];
|
|
190
|
+
return name ? key(name, mods(n[1])) : undefined;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// Card text as Markdown in the terminal. The page's own renderer, web/markdown.js, reads the Markdown, so both show
|
|
2
|
+
// the same subset the same way; this file draws its elements as styled lines.
|
|
3
|
+
import { SESSION_LINK, renderInline, renderMarkdown } from '../../web/markdown.js';
|
|
4
|
+
import { Doc, lineWidth, wrapSpans, } from './doc.js';
|
|
5
|
+
import { editRows } from './edit.js';
|
|
6
|
+
|
|
7
|
+
import { clip, sanitize, textWidth } from './text.js';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/** An element as the renderer builds it. */
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const h = (type , props , ...children ) =>
|
|
14
|
+
({ type, props: props ?? {}, children: (children.flat(Infinity) ).filter((c) => c !== null && c !== false && c !== undefined) });
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/** The target id of a link: a session link opens in herdr, a card link opens that card, any other opens its address. */
|
|
27
|
+
export const linkTarget = (href ) => (SESSION_LINK.test(href) ? `session:${href}` : `url:${href}`);
|
|
28
|
+
|
|
29
|
+
/** Inline Markdown as spans: code, bold, italic and links. */
|
|
30
|
+
export function inlineSpans(text , ctx , base = {}) {
|
|
31
|
+
return spansOf(renderInline(h, sanitize(text), cardLinkOpts(ctx)), ctx, base);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cardLinkOpts(ctx ) {
|
|
35
|
+
return {
|
|
36
|
+
cardLink: (id ) => h('cardlink', { id }, ctx.cardTitle?.(id) ?? id),
|
|
37
|
+
diagram: (source ) => h('diagram', { source }),
|
|
38
|
+
breaks: true,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function spansOf(nodes , ctx , base ) {
|
|
43
|
+
const out = [];
|
|
44
|
+
const { s, g, caps } = ctx.theme;
|
|
45
|
+
const walk = (n , style , target ) => {
|
|
46
|
+
if (typeof n === 'string') { out.push({ text: n, style, target }); return; }
|
|
47
|
+
if (n.type === 'strong') { n.children.forEach((c) => walk(c, { ...style, bold: true }, target)); return; }
|
|
48
|
+
if (n.type === 'em') { n.children.forEach((c) => walk(c, { ...style, italic: true }, target)); return; }
|
|
49
|
+
if (n.type === 'code') {
|
|
50
|
+
// Without color, code keeps its backticks, so it still reads as code.
|
|
51
|
+
const text = n.children.join('');
|
|
52
|
+
out.push({ text: caps.color === 0 ? `\`${text}\`` : text, style: { ...style, ...s.code }, target });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (n.type === 'a') {
|
|
56
|
+
const href = String(n.props['href'] ?? '');
|
|
57
|
+
const t = linkTarget(href);
|
|
58
|
+
ctx.onLink?.(t);
|
|
59
|
+
const focused = ctx.focused === t;
|
|
60
|
+
const look = { ...style, ...s.link, ...(SESSION_LINK.test(href) ? {} : { link: href }), ...(focused ? { inverse: true } : {}) };
|
|
61
|
+
if (SESSION_LINK.test(href)) out.push({ text: `${g.session} `, style: look, target: t });
|
|
62
|
+
n.children.forEach((c) => walk(c, look, t));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (n.type === 'cardlink') {
|
|
66
|
+
const id = String(n.props['id']);
|
|
67
|
+
const known = ctx.cardTitle?.(id) !== undefined;
|
|
68
|
+
const t = `card:${id}`;
|
|
69
|
+
if (known) ctx.onLink?.(t);
|
|
70
|
+
const look = known ? { ...style, ...s.link, ...(ctx.focused === t ? { inverse: true } : {}) } : { ...style, ...s.dim, italic: true };
|
|
71
|
+
n.children.forEach((c) => walk(c, look, known ? t : undefined));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
n.children.forEach((c) => walk(c, style, target));
|
|
75
|
+
};
|
|
76
|
+
nodes.forEach((n) => walk(n, base));
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Markdown blocks drawn into the document, indented by `indent` columns. */
|
|
81
|
+
export function markdown(doc , text , ctx , indent = 0, base = {}) {
|
|
82
|
+
const blocks = renderMarkdown(h, sanitize(text), cardLinkOpts(ctx));
|
|
83
|
+
const pad = indent ? [{ text: ' '.repeat(indent) }] : [];
|
|
84
|
+
const width = doc.width;
|
|
85
|
+
const { s, g } = ctx.theme;
|
|
86
|
+
blocks.forEach((b, i) => {
|
|
87
|
+
if (i) doc.gap();
|
|
88
|
+
block(doc, b, ctx, pad, width, base, s, g);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function block(doc , b , ctx , pad , width , base , s , g ) {
|
|
93
|
+
const kids = (n , style = base) => spansOf(n.children, ctx, style);
|
|
94
|
+
if (/^h[1-6]$/.test(b.type)) { doc.para(kids(b, { ...base, ...s.heading }), { first: pad }); return; }
|
|
95
|
+
if (b.type === 'p') { doc.para(kids(b), { first: pad }); return; }
|
|
96
|
+
if (b.type === 'ul' || b.type === 'ol') {
|
|
97
|
+
b.children.forEach((li, j) => {
|
|
98
|
+
if (typeof li === 'string') return;
|
|
99
|
+
const mark = b.type === 'ol' ? `${j + 1}. ` : `${g.bullet} `;
|
|
100
|
+
doc.para(kids(li), { first: [...pad, { text: mark, style: s.dim }], rest: [...pad, { text: ' '.repeat(textWidth(mark)) }] });
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (b.type === 'blockquote') {
|
|
105
|
+
const bar = [...pad, { text: `${g.quote} `, style: s.dim }];
|
|
106
|
+
doc.para(kids(b, { ...base, ...s.dim }), { first: bar, rest: bar });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (b.type === 'diagram') { diagram(doc, String(b.props['source'] ?? ''), pad, ctx.theme); return; }
|
|
110
|
+
// A fenced block comes with the page's Copy button around it. The terminal draws only its code.
|
|
111
|
+
if (b.type === 'div' && b.props['className'] === 'code-block') {
|
|
112
|
+
const pre = b.children.find((c) => typeof c !== 'string' && c.type === 'pre');
|
|
113
|
+
if (pre) block(doc, pre, ctx, pad, width, base, s, g);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (b.type === 'pre') {
|
|
117
|
+
const code = b.children.map((c) => (typeof c === 'string' ? c : c.children.join(''))).join('');
|
|
118
|
+
codeBlock(doc, code, pad, ctx.theme);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (b.type === 'table') {
|
|
122
|
+
const rows = [];
|
|
123
|
+
for (const part of b.children) {
|
|
124
|
+
if (typeof part === 'string') continue;
|
|
125
|
+
for (const tr of part.children) {
|
|
126
|
+
if (typeof tr === 'string') continue;
|
|
127
|
+
rows.push(tr.children.map((cell) => (typeof cell === 'string' ? [{ text: cell }] : kids(cell, cell.type === 'th' ? { ...base, bold: true } : base))));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
table(doc, rows, pad, ctx.theme);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A Mermaid diagram: the terminal shows its source, and says the page draws it. */
|
|
135
|
+
export function diagram(doc , source , pad , theme ) {
|
|
136
|
+
doc.para([{ text: 'A diagram, drawn on the page in the browser. Its source:', style: theme.s.dim }], { first: pad });
|
|
137
|
+
codeBlock(doc, source, pad, theme);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Code as it is, line by line, behind a bar. A line wider than the pane breaks. */
|
|
141
|
+
export function codeBlock(doc , code , pad , theme ) {
|
|
142
|
+
const bar = [...pad, { text: `${theme.g.quote} `, style: theme.s.dim }];
|
|
143
|
+
const barW = bar.reduce((w, sp) => w + textWidth(sp.text), 0);
|
|
144
|
+
// Code keeps its spaces: each line breaks by character where it is wider than the pane.
|
|
145
|
+
for (const row of editRows(sanitize(code).replace(/\n$/, ''), Math.max(4, doc.width - barW))) {
|
|
146
|
+
doc.push({ spans: [...bar, { text: row.text, style: theme.caps.color ? theme.s.code : {} }] });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A table: the first row is the header. Columns share the width, each at least its header; text in a cell wraps
|
|
152
|
+
* inside its column. When even that does not fit, each row prints as "header: value" lines.
|
|
153
|
+
*/
|
|
154
|
+
export function table(doc , rows , pad , theme ) {
|
|
155
|
+
if (!rows.length) return;
|
|
156
|
+
const { g, s } = theme;
|
|
157
|
+
const cols = Math.max(...rows.map((r) => r.length));
|
|
158
|
+
const padW = pad.reduce((w, p) => w + textWidth(p.text), 0);
|
|
159
|
+
const avail = doc.width - padW - (cols - 1) * 3;
|
|
160
|
+
const natural = Array.from({ length: cols }, (_, c) => Math.max(1, ...rows.map((r) => lineWidth({ spans: r[c] ?? [] }))));
|
|
161
|
+
const header = rows[0] ;
|
|
162
|
+
const min = Array.from({ length: cols }, (_, c) => Math.min(natural[c] , Math.max(4, ...(header[c] ?? []).map((sp) => Math.min(12, textWidth(sp.text))))));
|
|
163
|
+
if (min.reduce((a, b) => a + b, 0) > avail) {
|
|
164
|
+
// Too narrow for columns: one block per row.
|
|
165
|
+
rows.slice(1).forEach((r, i) => {
|
|
166
|
+
if (i) doc.gap();
|
|
167
|
+
r.forEach((cell, c) => doc.para([{ text: `${clip(header[c]?.map((sp) => sp.text).join('') ?? '', 30)}: `, style: s.bold }, ...cell], { first: pad, rest: [...pad, { text: ' ' }] }));
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
// Give each column its natural width, then shrink the widest until the table fits.
|
|
172
|
+
const w = [...natural];
|
|
173
|
+
while (w.reduce((a, b) => a + b, 0) > avail) {
|
|
174
|
+
let widest = 0;
|
|
175
|
+
for (let c = 1; c < cols; c++) if (w[c] - min[c] > w[widest] - min[widest] ) widest = c;
|
|
176
|
+
if (w[widest] <= min[widest] ) break;
|
|
177
|
+
w[widest] --;
|
|
178
|
+
}
|
|
179
|
+
const sep = { text: ` ${g.table.v} `, style: s.dim };
|
|
180
|
+
rows.forEach((r, ri) => {
|
|
181
|
+
const wrapped = Array.from({ length: cols }, (_, c) => wrapSpans(r[c] ?? [], w[c] ));
|
|
182
|
+
const height = Math.max(...wrapped.map((x) => x.length));
|
|
183
|
+
for (let k = 0; k < height; k++) {
|
|
184
|
+
const spans = [...pad];
|
|
185
|
+
for (let c = 0; c < cols; c++) {
|
|
186
|
+
if (c) spans.push(sep);
|
|
187
|
+
const line = wrapped[c] [k] ?? { spans: [] };
|
|
188
|
+
spans.push(...line.spans, { text: ' '.repeat(Math.max(0, w[c] - lineWidth(line))) });
|
|
189
|
+
}
|
|
190
|
+
doc.push({ spans });
|
|
191
|
+
}
|
|
192
|
+
if (ri === 0) doc.push({ spans: [...pad, { text: w.map((x) => g.table.h.repeat(x)).join(`${g.table.h}${g.table.x}${g.table.h}`), style: s.dim }] });
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|