@sejuneoh/aitop 0.39.0 → 0.41.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/README.md +52 -5
- package/bin/aitop.mjs +37 -7
- package/package.json +1 -1
- package/src/config.mjs +53 -0
- package/src/doctor.mjs +2 -0
- package/src/help.mjs +24 -3
- package/src/i18n.mjs +115 -4
- package/src/json.mjs +13 -1
- package/src/keys.mjs +99 -1
- package/src/notify.mjs +4 -1
- package/src/order.mjs +12 -1
- package/src/remote.mjs +372 -0
- package/src/resume.mjs +44 -1
- package/src/sources/artifact-source.mjs +278 -0
- package/src/sources/artifacts.mjs +10 -0
- package/src/sources/claude-tracker.mjs +83 -3
- package/src/sources/claude.mjs +5 -1
- package/src/tail.mjs +18 -10
- package/src/ui/artifact-view.mjs +634 -0
- package/src/ui/chrome.mjs +4 -0
- package/src/ui/detail.mjs +23 -7
- package/src/ui/table.mjs +28 -2
- package/src/ui.mjs +33 -10
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
// 아티팩트 원본 보기 (#307): html·md 를 터미널 글자로 바꿔 전체 화면에 스크롤해 보인다.
|
|
2
|
+
// html 은 실행하지 않는다. <script>·<style>·<template>·주석은 버리고 제목·문단·목록·표·pre/code·인용·링크 글만 남긴다.
|
|
3
|
+
// 모든 글은 stripControl·mask 를 지나고, 링크는 #305 처럼 https://claude.ai/ 주소만 OSC 8 로 감싼다(draw.mjs line).
|
|
4
|
+
// 그 밖의 https 주소는 글 뒤에 흐린 글자로 붙이고, https 가 아닌 주소는 버린다.
|
|
5
|
+
import { mask, pad, stripControl, truncate, truncateLeft, width } from '../text.mjs';
|
|
6
|
+
import { SOURCE_MAX, artifactLink } from '../sources/artifacts.mjs';
|
|
7
|
+
import { PALETTE } from './theme.mjs';
|
|
8
|
+
import { box, segsWidth, tilde } from './draw.mjs';
|
|
9
|
+
import { glyphs } from './glyphs.mjs';
|
|
10
|
+
import { t as tr } from '../i18n.mjs';
|
|
11
|
+
|
|
12
|
+
// ── 글 정리 ──
|
|
13
|
+
const clean = (s) => mask(stripControl(String(s ?? '').replace(/\t/g, ' ')));
|
|
14
|
+
|
|
15
|
+
const NAMED = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', copy: '©', reg: '®', trade: '™', hellip: '…', mdash: '\u2014', ndash: '\u2013', middot: '·', bull: '•', laquo: '«', raquo: '»', lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', rarr: '→', larr: '←', uarr: '↑', darr: '↓', times: '×', divide: '÷', deg: '°', plusmn: '±', le: '≤', ge: '≥', ne: '≠', check: '✓', cross: '✗', star: '☆', starf: '★', zwj: '', zwnj: '', shy: '' };
|
|
16
|
+
|
|
17
|
+
// 엔티티 풀기. 제어 문자가 되는 숫자 엔티티( 등)는 뒤의 clean 이 지운다. 없는 이름은 그대로 둔다.
|
|
18
|
+
export function decodeEntities(s) {
|
|
19
|
+
return String(s).replace(/&(#x[0-9a-f]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});?/gi, (m, e) => {
|
|
20
|
+
if (e[0] === '#') {
|
|
21
|
+
const n = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
|
22
|
+
return Number.isFinite(n) && n > 0 && n <= 0x10ffff && !(n >= 0xd800 && n <= 0xdfff) ? String.fromCodePoint(n) : '�';
|
|
23
|
+
}
|
|
24
|
+
const v = NAMED[e.toLowerCase()];
|
|
25
|
+
return v == null ? m : v;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 크기 글: 1536 → 1.5KB, 120000 → 117KB
|
|
30
|
+
export function fmtBytes(n) {
|
|
31
|
+
if (n < 1024) return `${n}B`;
|
|
32
|
+
if (n < 1024 * 1024) return `${n < 10 * 1024 ? (n / 1024).toFixed(1) : Math.round(n / 1024)}KB`;
|
|
33
|
+
return `${(n / 1024 / 1024).toFixed(1)}MB`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// data URI 의 본문 크기(바이트, 대략).
|
|
37
|
+
function dataSize(src) {
|
|
38
|
+
const comma = src.indexOf(',');
|
|
39
|
+
if (comma < 0) return 0;
|
|
40
|
+
const body = src.slice(comma + 1).replace(/\s+/g, '');
|
|
41
|
+
return /;base64$/i.test(src.slice(0, comma)) ? Math.floor((body.replace(/=+$/, '').length * 3) / 4) : body.length;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 링크 주소: https 만. 쓸 수 있는 ASCII 글자만 있고 길이 상한 안일 때.
|
|
45
|
+
const HTTPS = /^https:\/\/[A-Za-z0-9\-._~:/?#[\]@!$&'()*+,;=%]+$/;
|
|
46
|
+
const httpsOnly = (href) => {
|
|
47
|
+
const h = String(href ?? '').trim();
|
|
48
|
+
return h.length <= 300 && HTTPS.test(h) ? h : null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function attr(attrs, name) {
|
|
52
|
+
const m = new RegExp(`(?:^|\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, 'i').exec(attrs);
|
|
53
|
+
return m ? decodeEntities(m[1] ?? m[2] ?? m[3] ?? '') : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── 블록 ──
|
|
57
|
+
// { kind: 'h', level, runs } | { kind: 'p', runs, quote, list? } | { kind: 'li', depth, mark, runs, quote } | { kind: 'pre', text, quote }
|
|
58
|
+
// | { kind: 'table', rows: [{ head, cells: [string] }] } | { kind: 'hr' }
|
|
59
|
+
// runs: [{ t, bold, italic, code, link }]
|
|
60
|
+
|
|
61
|
+
const BLOCK = new Set(['p', 'div', 'section', 'article', 'header', 'footer', 'main', 'nav', 'aside', 'figure', 'figcaption', 'dl', 'dt', 'dd', 'details', 'summary', 'form', 'fieldset', 'legend', 'address', 'center', 'body', 'html', 'caption']);
|
|
62
|
+
const DROP = ['script', 'style', 'template', 'noscript', 'svg', 'math', 'head', 'object', 'iframe', 'canvas'];
|
|
63
|
+
|
|
64
|
+
// html 을 조각으로 나눈다: 태그면 [close, name, attrs], 글이면 [, , , text].
|
|
65
|
+
// 태그는 '<이름' 뒤로 따옴표 밖의 첫 '>' 까지다. 닫히지 않으면(따옴표가 안 닫히거나 '>' 가 없으면) 그 '<' 는 글자다.
|
|
66
|
+
// 정규식으로 하면 닫히지 않은 '<b' 마다 문서 끝까지 훑어 크기의 제곱이 걸린다. 여기서는 길이에 비례하게 한다.
|
|
67
|
+
// 1) '<' '>' 와 따옴표의 위치를 처음에 한 번 모아 두고 '다음 위치' 를 이분 탐색으로 찾는다.
|
|
68
|
+
// 2) 따옴표를 건너뛰며 '>' 를 찾는 걸음은 여는 따옴표가 같으면 그 뒤가 늘 같다. 그래서 따옴표마다 '여기서 열면 태그가 어디서
|
|
69
|
+
// 닫히는지(없으면 -1)' 를 뒤에서부터 한 번 채워 두고(shutAt), 걸음은 첫 여는 따옴표에서 그 표를 읽고 끝난다.
|
|
70
|
+
export function* tokens(src) {
|
|
71
|
+
const n = src.length;
|
|
72
|
+
const where = (re) => {
|
|
73
|
+
const out = [];
|
|
74
|
+
for (const m of src.matchAll(re)) out.push(m.index);
|
|
75
|
+
return out;
|
|
76
|
+
};
|
|
77
|
+
const lts = where(/</g);
|
|
78
|
+
const gts = where(/>/g);
|
|
79
|
+
const quotes = where(/["']/g);
|
|
80
|
+
// a 에서 j 이상인 첫 칸의 번호.
|
|
81
|
+
const seek = (a, j) => {
|
|
82
|
+
let lo = 0;
|
|
83
|
+
let hi = a.length;
|
|
84
|
+
while (lo < hi) {
|
|
85
|
+
const mid = (lo + hi) >> 1;
|
|
86
|
+
if (a[mid] < j) lo = mid + 1;
|
|
87
|
+
else hi = mid;
|
|
88
|
+
}
|
|
89
|
+
return lo;
|
|
90
|
+
};
|
|
91
|
+
const after = (a, j) => {
|
|
92
|
+
const k = seek(a, j);
|
|
93
|
+
return k < a.length ? a[k] : n;
|
|
94
|
+
};
|
|
95
|
+
// 따옴표 밖 j 에서 출발해 태그가 닫히는 '>' 위치, 없으면 -1. k 는 j 이후 첫 따옴표의 번호.
|
|
96
|
+
const close = (j, k) => {
|
|
97
|
+
const gt = after(gts, j);
|
|
98
|
+
const q = k < quotes.length ? quotes[k] : n;
|
|
99
|
+
if (gt < q) return gt;
|
|
100
|
+
return q >= n ? -1 : shutAt[k];
|
|
101
|
+
};
|
|
102
|
+
// shutAt[k]: quotes[k] 를 여는 따옴표로 쓰면 태그가 닫히는 위치 (-1 은 닫히지 않음). 뒤에서부터 채운다.
|
|
103
|
+
const shutAt = new Int32Array(quotes.length);
|
|
104
|
+
// 같은 글자의 다음 따옴표 번호 (닫는 따옴표).
|
|
105
|
+
const nextSame = new Int32Array(quotes.length);
|
|
106
|
+
const lastOf = { '"': quotes.length, "'": quotes.length };
|
|
107
|
+
for (let k = quotes.length - 1; k >= 0; k--) {
|
|
108
|
+
const ch = src[quotes[k]];
|
|
109
|
+
nextSame[k] = lastOf[ch];
|
|
110
|
+
lastOf[ch] = k;
|
|
111
|
+
}
|
|
112
|
+
for (let k = quotes.length - 1; k >= 0; k--) {
|
|
113
|
+
const s = nextSame[k];
|
|
114
|
+
shutAt[k] = s >= quotes.length ? -1 : close(quotes[s] + 1, s + 1);
|
|
115
|
+
}
|
|
116
|
+
const NAME = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)/y;
|
|
117
|
+
let i = 0;
|
|
118
|
+
// 글 조각의 시작. 태그가 못 된 '<' 는 앞뒤 글과 한 조각으로 묶는다.
|
|
119
|
+
let from = 0;
|
|
120
|
+
while (i < n) {
|
|
121
|
+
const lt = after(lts, i);
|
|
122
|
+
if (lt >= n) break;
|
|
123
|
+
NAME.lastIndex = lt;
|
|
124
|
+
const m = NAME.exec(src);
|
|
125
|
+
const end = m ? close(NAME.lastIndex, seek(quotes, NAME.lastIndex)) : -1;
|
|
126
|
+
if (end < 0) {
|
|
127
|
+
i = lt + 1;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (lt > from) yield [undefined, undefined, undefined, src.slice(from, lt)];
|
|
131
|
+
yield [m[1], m[2], src.slice(lt + m[0].length, end)];
|
|
132
|
+
i = from = end + 1;
|
|
133
|
+
}
|
|
134
|
+
if (n > from) yield [undefined, undefined, undefined, src.slice(from)];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function htmlToBlocks(html) {
|
|
138
|
+
let src = String(html ?? '');
|
|
139
|
+
let title = null;
|
|
140
|
+
// 제목: 첫 <title 의 '>' 와 그 뒤 첫 </title> 사이. 원문에서 대소문자를 무시하고 찾아 위치가 어긋나지 않게 한다
|
|
141
|
+
// (toLowerCase 는 'İ' 처럼 길이가 바뀌는 글자가 있다). 정규식은 앞으로만 한 번 훑으므로 닫힘이 없는 <title 이 많아도 길이에 비례한다.
|
|
142
|
+
const open = /<title\b/i.exec(src);
|
|
143
|
+
const tgt = open ? src.indexOf('>', open.index) : -1;
|
|
144
|
+
const shut = /<\/title\s*>/gi;
|
|
145
|
+
shut.lastIndex = tgt + 1;
|
|
146
|
+
const tend = tgt < 0 ? null : shut.exec(src);
|
|
147
|
+
if (tend) title = clean(decodeEntities(src.slice(tgt + 1, tend.index))).replace(/\s+/g, ' ').trim() || null;
|
|
148
|
+
// 주석·CDATA 와 실행·숨김 내용을 통째로 버린다. 닫는 태그가 없으면 끝까지 버린다.
|
|
149
|
+
src = src.replace(/<!--[\s\S]*?(?:-->|$)/g, '').replace(/<!\[CDATA\[[\s\S]*?(?:\]\]>|$)/g, '').replace(/<![^>]*(?:>|$)/g, '');
|
|
150
|
+
for (const tag of DROP) src = src.replace(new RegExp(`<${tag}\\b[^>]*?\\/>|<${tag}\\b[\\s\\S]*?(?:<\\/${tag}\\s*>|$)`, 'gi'), '');
|
|
151
|
+
|
|
152
|
+
const blocks = [];
|
|
153
|
+
let runs = [];
|
|
154
|
+
let kind = 'p';
|
|
155
|
+
let level = 0;
|
|
156
|
+
let liMark = '';
|
|
157
|
+
let liDepth = 0;
|
|
158
|
+
const lists = [];
|
|
159
|
+
let quote = 0;
|
|
160
|
+
let bold = 0;
|
|
161
|
+
let italic = 0;
|
|
162
|
+
let code = 0;
|
|
163
|
+
let link = null;
|
|
164
|
+
let linkText = '';
|
|
165
|
+
let pre = null;
|
|
166
|
+
let table = null;
|
|
167
|
+
let cell = null;
|
|
168
|
+
|
|
169
|
+
const push = (t, extra = {}) => {
|
|
170
|
+
if (cell) {
|
|
171
|
+
cell.text += t;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
runs.push({ t, bold: bold > 0, italic: italic > 0, code: code > 0, ...(link ? { link } : {}), ...extra });
|
|
175
|
+
};
|
|
176
|
+
const flush = () => {
|
|
177
|
+
const text = runs.map((r) => r.t).join('');
|
|
178
|
+
if (text.trim()) {
|
|
179
|
+
if (kind === 'h') blocks.push({ kind: 'h', level, runs });
|
|
180
|
+
else if (kind === 'li') blocks.push({ kind: 'li', depth: liDepth, mark: liMark, runs, quote });
|
|
181
|
+
else blocks.push({ kind: 'p', runs, quote });
|
|
182
|
+
// 같은 항목 안에서 이어지는 글은 표시 없이 들여 쓴다.
|
|
183
|
+
if (kind === 'li') liMark = '';
|
|
184
|
+
}
|
|
185
|
+
runs = [];
|
|
186
|
+
if (kind === 'h') kind = 'p';
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
for (const [close, rawName, attrs = '', text] of tokens(src)) {
|
|
190
|
+
if (text != null) {
|
|
191
|
+
const s = decodeEntities(text);
|
|
192
|
+
if (pre) pre.text += s;
|
|
193
|
+
else push(s.replace(/\s+/g, ' '));
|
|
194
|
+
if (link) linkText += s;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const name = rawName.toLowerCase();
|
|
198
|
+
if (pre && name !== 'pre') {
|
|
199
|
+
if (name === 'br') pre.text += '\n';
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (name === 'pre') {
|
|
203
|
+
if (!close) {
|
|
204
|
+
flush();
|
|
205
|
+
pre = { text: '' };
|
|
206
|
+
} else if (pre) {
|
|
207
|
+
blocks.push({ kind: 'pre', text: pre.text.replace(/^\n/, '').replace(/\s+$/, ''), quote });
|
|
208
|
+
pre = null;
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (/^h[1-6]$/.test(name)) {
|
|
213
|
+
flush();
|
|
214
|
+
if (!close) {
|
|
215
|
+
kind = 'h';
|
|
216
|
+
level = Number(name[1]);
|
|
217
|
+
}
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (name === 'table') {
|
|
221
|
+
if (!close && !table) {
|
|
222
|
+
flush();
|
|
223
|
+
table = { rows: [] };
|
|
224
|
+
} else if (close && table) {
|
|
225
|
+
if (cell) table.rows.at(-1)?.cells.push(cell.text);
|
|
226
|
+
cell = null;
|
|
227
|
+
const rows = table.rows.map((r) => ({ head: r.head, cells: r.cells.map((c) => clean(c).replace(/\s+/g, ' ').trim()) })).filter((r) => r.cells.some(Boolean));
|
|
228
|
+
if (rows.length) blocks.push({ kind: 'table', rows });
|
|
229
|
+
table = null;
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (table) {
|
|
234
|
+
if (name === 'tr' && !close) {
|
|
235
|
+
if (cell) table.rows.at(-1)?.cells.push(cell.text);
|
|
236
|
+
cell = null;
|
|
237
|
+
table.rows.push({ head: false, cells: [] });
|
|
238
|
+
} else if ((name === 'td' || name === 'th') && !close) {
|
|
239
|
+
if (!table.rows.length) table.rows.push({ head: false, cells: [] });
|
|
240
|
+
if (cell) table.rows.at(-1).cells.push(cell.text);
|
|
241
|
+
cell = { text: '' };
|
|
242
|
+
if (name === 'th') table.rows.at(-1).head = true;
|
|
243
|
+
} else if ((name === 'td' || name === 'th') && close && cell) {
|
|
244
|
+
table.rows.at(-1).cells.push(cell.text);
|
|
245
|
+
cell = null;
|
|
246
|
+
} else if (name === 'br' || name === 'p' || name === 'li') {
|
|
247
|
+
if (cell) cell.text += ' ';
|
|
248
|
+
} else if (name === 'img' && cell) cell.text += imgText(attrs);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (name === 'ul' || name === 'ol') {
|
|
252
|
+
flush();
|
|
253
|
+
const start = Number(attr(attrs, 'start'));
|
|
254
|
+
if (!close) lists.push({ ordered: name === 'ol', n: Number.isInteger(start) && start > 0 ? start - 1 : 0 });
|
|
255
|
+
else lists.pop();
|
|
256
|
+
kind = lists.length ? kind : 'p';
|
|
257
|
+
liDepth = Math.max(1, lists.length);
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (name === 'li') {
|
|
261
|
+
flush();
|
|
262
|
+
if (!close) {
|
|
263
|
+
const l = lists.at(-1) ?? { ordered: false, n: 0 };
|
|
264
|
+
l.n++;
|
|
265
|
+
kind = 'li';
|
|
266
|
+
liDepth = Math.max(1, lists.length);
|
|
267
|
+
liMark = l.ordered ? `${l.n}.` : glyphs().event;
|
|
268
|
+
} else kind = lists.length ? 'li' : 'p';
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
if (name === 'blockquote') {
|
|
272
|
+
flush();
|
|
273
|
+
quote = Math.max(0, quote + (close ? -1 : 1));
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (name === 'hr') {
|
|
277
|
+
flush();
|
|
278
|
+
blocks.push({ kind: 'hr' });
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (name === 'br') {
|
|
282
|
+
push('\n');
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (BLOCK.has(name) || name === 'tr') {
|
|
286
|
+
flush();
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (name === 'b' || name === 'strong') bold += close ? -1 : 1;
|
|
290
|
+
else if (name === 'i' || name === 'em') italic += close ? -1 : 1;
|
|
291
|
+
else if (name === 'code' || name === 'kbd' || name === 'samp') code += close ? -1 : 1;
|
|
292
|
+
else if (name === 'a') {
|
|
293
|
+
if (!close) {
|
|
294
|
+
link = { href: httpsOnly(attr(attrs, 'href')) };
|
|
295
|
+
linkText = '';
|
|
296
|
+
} else if (link) {
|
|
297
|
+
// claude.ai 가 아닌 https 주소는 글과 다를 때만 글 뒤에 붙인다.
|
|
298
|
+
const href = link.href;
|
|
299
|
+
link = null;
|
|
300
|
+
if (href && !artifactLink(href) && linkText.trim() !== href) push(` <${href}>`, { url: true });
|
|
301
|
+
}
|
|
302
|
+
} else if (name === 'img') push(imgText(attrs));
|
|
303
|
+
bold = Math.max(0, bold);
|
|
304
|
+
italic = Math.max(0, italic);
|
|
305
|
+
code = Math.max(0, code);
|
|
306
|
+
}
|
|
307
|
+
if (pre) blocks.push({ kind: 'pre', text: pre.text.replace(/\s+$/, ''), quote });
|
|
308
|
+
if (table) {
|
|
309
|
+
if (cell) table.rows.at(-1)?.cells.push(cell.text);
|
|
310
|
+
const rows = table.rows.map((r) => ({ head: r.head, cells: r.cells.map((c) => clean(c).replace(/\s+/g, ' ').trim()) })).filter((r) => r.cells.some(Boolean));
|
|
311
|
+
if (rows.length) blocks.push({ kind: 'table', rows });
|
|
312
|
+
}
|
|
313
|
+
flush();
|
|
314
|
+
return { title, blocks: blocks.map(cleanBlock) };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function imgText(attrs) {
|
|
318
|
+
const src = attr(attrs, 'src') ?? '';
|
|
319
|
+
if (/^data:/i.test(src.trim())) return ` ${tr('art.image', { size: fmtBytes(dataSize(src.trim())) })} `;
|
|
320
|
+
const alt = (attr(attrs, 'alt') ?? '').trim();
|
|
321
|
+
return alt ? ` ${tr('art.imageAlt', { alt })} ` : '';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// 글 조각마다 제어 문자 지우기·비밀값 가리기. 링크는 { href } 에서 주소(허용 목록을 지난 것)만 남긴다.
|
|
325
|
+
function cleanBlock(b) {
|
|
326
|
+
if (b.runs) b.runs = b.runs.map((r) => ({ ...r, t: clean(r.t), link: r.link?.href && artifactLink(r.link.href) ? r.link.href : undefined }));
|
|
327
|
+
if (b.kind === 'pre') b.text = clean(b.text);
|
|
328
|
+
return b;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ── md (간단히): 제목·목록·코드 블록·인용·표·굵게·코드·링크 ──
|
|
332
|
+
function mdInline(s) {
|
|
333
|
+
const runs = [];
|
|
334
|
+
const re = /\*\*(.+?)\*\*|`([^`]+)`|\[([^\]]+)\]\(([^)\s]+)\)/g;
|
|
335
|
+
let last = 0;
|
|
336
|
+
let m;
|
|
337
|
+
while ((m = re.exec(s))) {
|
|
338
|
+
if (m.index > last) runs.push({ t: s.slice(last, m.index) });
|
|
339
|
+
if (m[1] != null) runs.push({ t: m[1], bold: true });
|
|
340
|
+
else if (m[2] != null) runs.push({ t: m[2], code: true });
|
|
341
|
+
else {
|
|
342
|
+
const href = httpsOnly(m[4]);
|
|
343
|
+
runs.push({ t: m[3], ...(href ? { link: { href } } : {}) });
|
|
344
|
+
if (href && !artifactLink(href) && m[3] !== href) runs.push({ t: ` <${href}>`, url: true });
|
|
345
|
+
}
|
|
346
|
+
last = re.lastIndex;
|
|
347
|
+
}
|
|
348
|
+
if (last < s.length) runs.push({ t: s.slice(last) });
|
|
349
|
+
return runs;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function mdToBlocks(md) {
|
|
353
|
+
const blocks = [];
|
|
354
|
+
let title = null;
|
|
355
|
+
let para = [];
|
|
356
|
+
let fence = null;
|
|
357
|
+
let table = null;
|
|
358
|
+
const flushPara = () => {
|
|
359
|
+
if (para.length) blocks.push({ kind: 'p', runs: mdInline(para.join(' ')), quote: 0 });
|
|
360
|
+
para = [];
|
|
361
|
+
};
|
|
362
|
+
const flushTable = () => {
|
|
363
|
+
if (table?.length) blocks.push({ kind: 'table', rows: table });
|
|
364
|
+
table = null;
|
|
365
|
+
};
|
|
366
|
+
for (const raw of String(md ?? '').split(/\r?\n/)) {
|
|
367
|
+
if (fence) {
|
|
368
|
+
if (/^\s*(```|~~~)/.test(raw)) {
|
|
369
|
+
blocks.push({ kind: 'pre', text: fence.join('\n'), quote: 0 });
|
|
370
|
+
fence = null;
|
|
371
|
+
} else fence.push(raw);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (/^\s*(```|~~~)/.test(raw)) {
|
|
375
|
+
flushPara();
|
|
376
|
+
flushTable();
|
|
377
|
+
fence = [];
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
const l = raw.trimEnd();
|
|
381
|
+
if (/^\s*\|.*\|\s*$/.test(l)) {
|
|
382
|
+
flushPara();
|
|
383
|
+
if (/^\s*\|[\s:|-]+\|\s*$/.test(l) && l.includes('-')) {
|
|
384
|
+
if (table?.length) table[0].head = true;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
(table ??= []).push({ head: false, cells: l.trim().slice(1, -1).split('|').map((c) => c.trim()) });
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
flushTable();
|
|
391
|
+
let m;
|
|
392
|
+
if (!l.trim()) flushPara();
|
|
393
|
+
else if ((m = /^(#{1,6})\s+(.*)$/.exec(l))) {
|
|
394
|
+
flushPara();
|
|
395
|
+
if (!title && m[1].length === 1) title = m[2].trim();
|
|
396
|
+
blocks.push({ kind: 'h', level: m[1].length, runs: mdInline(m[2]) });
|
|
397
|
+
} else if ((m = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/.exec(l))) {
|
|
398
|
+
flushPara();
|
|
399
|
+
const depth = 1 + Math.floor(m[1].replace(/\t/g, ' ').length / 2);
|
|
400
|
+
blocks.push({ kind: 'li', depth, mark: /\d/.test(m[2]) ? m[2] : glyphs().event, runs: mdInline(m[3]), quote: 0 });
|
|
401
|
+
} else if ((m = /^\s*>\s?(.*)$/.exec(l))) {
|
|
402
|
+
flushPara();
|
|
403
|
+
blocks.push({ kind: 'p', runs: mdInline(m[1]), quote: 1 });
|
|
404
|
+
} else if (/^\s*([-*_]\s*){3,}$/.test(l)) {
|
|
405
|
+
flushPara();
|
|
406
|
+
blocks.push({ kind: 'hr' });
|
|
407
|
+
} else para.push(l.trim());
|
|
408
|
+
}
|
|
409
|
+
if (fence) blocks.push({ kind: 'pre', text: fence.join('\n'), quote: 0 });
|
|
410
|
+
flushPara();
|
|
411
|
+
flushTable();
|
|
412
|
+
for (const b of blocks) if (b.kind === 'table') b.rows = b.rows.map((r) => ({ ...r, cells: r.cells.map((c) => clean(c)) }));
|
|
413
|
+
return { title: title && clean(title), blocks: blocks.map(cleanBlock) };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// 원본 종류: 경로 끝이 .md·.markdown 이면 md, .html·.htm 이거나 html 처럼 생겼으면 html, 아니면 글 그대로(pre).
|
|
417
|
+
export function docKind(path, text) {
|
|
418
|
+
if (/\.(md|markdown)$/i.test(path ?? '')) return 'md';
|
|
419
|
+
if (/\.html?$/i.test(path ?? '') || /^\s*(<!doctype html|<html|<head|<body|<div|<p[\s>])/i.test(text ?? '')) return 'html';
|
|
420
|
+
return 'text';
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export function parseDoc(path, text) {
|
|
424
|
+
const k = docKind(path, text);
|
|
425
|
+
if (k === 'html') return htmlToBlocks(text);
|
|
426
|
+
if (k === 'md') return mdToBlocks(text);
|
|
427
|
+
return { title: null, blocks: [{ kind: 'pre', text: clean(text), quote: 0 }] };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// ── 그리기 ──
|
|
431
|
+
const runSeg = (r) => ({
|
|
432
|
+
t: r.t,
|
|
433
|
+
fg: r.url ? PALETTE.info : r.code ? PALETTE.tool : r.link ? PALETTE.info : PALETTE.text,
|
|
434
|
+
...(r.bold ? { bold: true } : {}),
|
|
435
|
+
...(r.italic ? { italic: true } : {}),
|
|
436
|
+
...(r.link ? { link: r.link, underline: true } : {}),
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// 글자 단위로 폭 w 에 맞게 자른다 (한글 2칸).
|
|
440
|
+
function chunks(s, w) {
|
|
441
|
+
const out = [];
|
|
442
|
+
let cur = '';
|
|
443
|
+
let cw = 0;
|
|
444
|
+
// 폭은 글자마다 더해 센다 (긴 낱말에서 줄 전체를 다시 재지 않게).
|
|
445
|
+
for (const ch of s) {
|
|
446
|
+
const k = width(ch);
|
|
447
|
+
if (cur && cw + k > w) {
|
|
448
|
+
out.push(cur);
|
|
449
|
+
cur = '';
|
|
450
|
+
cw = 0;
|
|
451
|
+
}
|
|
452
|
+
cur += ch;
|
|
453
|
+
cw += k;
|
|
454
|
+
}
|
|
455
|
+
if (cur || !out.length) out.push(cur);
|
|
456
|
+
return out;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// 조각들을 폭 w 로 감싼다. 띄어쓰기에서 끊고, 긴 낱말은 글자 단위로. '\n' 은 줄을 바꾼다.
|
|
460
|
+
export function wrapRuns(runs, w) {
|
|
461
|
+
w = Math.max(1, w);
|
|
462
|
+
const lines = [[]];
|
|
463
|
+
let used = 0;
|
|
464
|
+
const add = (seg) => {
|
|
465
|
+
const last = lines.at(-1);
|
|
466
|
+
const prev = last.at(-1);
|
|
467
|
+
if (prev && prev.fg === seg.fg && prev.bold === seg.bold && prev.italic === seg.italic && prev.link === seg.link) prev.t += seg.t;
|
|
468
|
+
else last.push(seg);
|
|
469
|
+
used += width(seg.t);
|
|
470
|
+
};
|
|
471
|
+
const br = () => {
|
|
472
|
+
const last = lines.at(-1);
|
|
473
|
+
if (last.length) last[last.length - 1].t = last.at(-1).t.trimEnd();
|
|
474
|
+
lines.push([]);
|
|
475
|
+
used = 0;
|
|
476
|
+
};
|
|
477
|
+
for (const r of runs) {
|
|
478
|
+
const seg = runSeg(r);
|
|
479
|
+
for (const part of r.t.split(/(\n| +)/)) {
|
|
480
|
+
if (!part) continue;
|
|
481
|
+
if (part === '\n') {
|
|
482
|
+
br();
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
if (/^ +$/.test(part)) {
|
|
486
|
+
// 이어진 공백은 하나로 (조각 경계에서 겹치는 것도).
|
|
487
|
+
if (used > 0 && used < w && !/ $/.test(lines.at(-1).at(-1)?.t ?? '')) add({ ...seg, t: ' ' });
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
const pw = width(part);
|
|
491
|
+
if (used + pw > w && used > 0) br();
|
|
492
|
+
if (pw <= w) add({ ...seg, t: part });
|
|
493
|
+
else
|
|
494
|
+
for (const c of chunks(part, w)) {
|
|
495
|
+
if (used > 0) br();
|
|
496
|
+
add({ ...seg, t: c });
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
while (lines.length > 1 && !lines.at(-1).length) lines.pop();
|
|
501
|
+
return lines.map((l) => l.filter((x) => x.t));
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function tableLines(rows, w, sep) {
|
|
505
|
+
const n = Math.max(...rows.map((r) => r.cells.length));
|
|
506
|
+
const cols = Array.from({ length: n }, (_, i) => Math.max(1, ...rows.map((r) => width(r.cells[i] ?? ''))));
|
|
507
|
+
const gap = width(sep);
|
|
508
|
+
const total = () => cols.reduce((a, b) => a + b, 0) + gap * (n - 1);
|
|
509
|
+
// 넘치면 가장 넓은 칸부터 줄인다 (칸마다 4칸까지).
|
|
510
|
+
while (total() > w) {
|
|
511
|
+
const i = cols.indexOf(Math.max(...cols));
|
|
512
|
+
if (cols[i] <= 4) break;
|
|
513
|
+
cols[i]--;
|
|
514
|
+
}
|
|
515
|
+
const out = [];
|
|
516
|
+
if (total() > w) {
|
|
517
|
+
// 칸이 너무 많으면 한 행을 ' · ' 로 이어 감싼다.
|
|
518
|
+
for (const r of rows) for (const l of wrapRuns([{ t: r.cells.join(' · '), bold: r.head }], w)) out.push(l);
|
|
519
|
+
return out;
|
|
520
|
+
}
|
|
521
|
+
rows.forEach((r, k) => {
|
|
522
|
+
const segs = [];
|
|
523
|
+
cols.forEach((cw, i) => {
|
|
524
|
+
if (i) segs.push({ t: sep, fg: PALETTE.line });
|
|
525
|
+
segs.push({ t: pad(r.cells[i] ?? '', cw), fg: PALETTE.text, ...(r.head ? { bold: true } : {}) });
|
|
526
|
+
});
|
|
527
|
+
out.push(segs);
|
|
528
|
+
if (r.head && k === 0) out.push([{ t: cols.map((cw) => glyphs().rule.repeat(cw)).join(glyphs().rule.repeat(gap)), fg: PALETTE.line }]);
|
|
529
|
+
});
|
|
530
|
+
return out;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// 블록 → 화면 줄 [segs]. 블록 사이에 빈 줄 하나.
|
|
534
|
+
export function renderBlocks(blocks, w) {
|
|
535
|
+
const out = [];
|
|
536
|
+
const bar = glyphs().edge.normal.v;
|
|
537
|
+
const gapBefore = () => {
|
|
538
|
+
if (out.length && out.at(-1).length) out.push([]);
|
|
539
|
+
};
|
|
540
|
+
for (const b of blocks) {
|
|
541
|
+
const qPre = b.quote ? [{ t: `${bar} `.repeat(b.quote), fg: PALETTE.muted }] : [];
|
|
542
|
+
const qw = segsWidth(qPre);
|
|
543
|
+
if (b.kind === 'h') {
|
|
544
|
+
gapBefore();
|
|
545
|
+
const head = { t: '#'.repeat(b.level) + ' ', fg: PALETTE.brand, bold: true };
|
|
546
|
+
const lines = wrapRuns(b.runs.map((r) => ({ ...r, bold: true })), w - width(head.t));
|
|
547
|
+
lines.forEach((l, i) => out.push([i ? { t: ' '.repeat(width(head.t)) } : head, ...l.map((x) => ({ ...x, fg: x.link ? x.fg : PALETTE.brand }))]));
|
|
548
|
+
out.push([]);
|
|
549
|
+
} else if (b.kind === 'p') {
|
|
550
|
+
const lines = wrapRuns(b.runs, w - qw);
|
|
551
|
+
const prev = out.at(-1);
|
|
552
|
+
if (prev && prev.length && !prev.isLi) out.push([]);
|
|
553
|
+
for (const l of lines) out.push([...qPre, ...l]);
|
|
554
|
+
} else if (b.kind === 'li') {
|
|
555
|
+
const ind = ' '.repeat(Math.max(0, b.depth - 1));
|
|
556
|
+
const mark = b.mark ? `${b.mark} ` : '';
|
|
557
|
+
const head = ind + (mark || ' ');
|
|
558
|
+
const lines = wrapRuns(b.runs, w - qw - width(head));
|
|
559
|
+
lines.forEach((l, i) => {
|
|
560
|
+
const row = [...qPre, { t: i ? ' '.repeat(width(head)) : ind, fg: PALETTE.muted }, ...(i ? [] : [{ t: mark || ' ', fg: PALETTE.brand }]), ...l];
|
|
561
|
+
row.isLi = true;
|
|
562
|
+
out.push(row);
|
|
563
|
+
});
|
|
564
|
+
} else if (b.kind === 'pre') {
|
|
565
|
+
gapBefore();
|
|
566
|
+
for (const raw of b.text.split('\n')) for (const c of chunks(raw, Math.max(1, w - qw - 2))) out.push([...qPre, { t: `${bar} `, fg: PALETTE.faint }, { t: c, fg: PALETTE.tool }]);
|
|
567
|
+
out.push([]);
|
|
568
|
+
} else if (b.kind === 'table') {
|
|
569
|
+
gapBefore();
|
|
570
|
+
out.push(...tableLines(b.rows, w, ` ${bar} `));
|
|
571
|
+
out.push([]);
|
|
572
|
+
} else if (b.kind === 'hr') {
|
|
573
|
+
gapBefore();
|
|
574
|
+
out.push([{ t: glyphs().rule.repeat(Math.max(1, w)), fg: PALETTE.line }]);
|
|
575
|
+
out.push([]);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
while (out.length && !out.at(-1).length) out.pop();
|
|
579
|
+
while (out.length && !out[0].length) out.shift();
|
|
580
|
+
return out;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// 원본 하나를 폭 w 의 줄들로. 같은 원본·폭이면 다시 만들지 않는다 (doc 에 붙여 둔다).
|
|
584
|
+
export function docLines(doc, w) {
|
|
585
|
+
if (doc.lines && doc.linesW === w) return doc.lines;
|
|
586
|
+
if (!doc.parsed) doc.parsed = doc.kind === 'none' || doc.kind === 'loading' ? { title: null, blocks: [] } : parseDoc(doc.path, doc.text);
|
|
587
|
+
if (doc.kind === 'loading') return [[{ t: tr('art.loading'), fg: PALETTE.info, italic: true }]];
|
|
588
|
+
const lines = doc.kind === 'none' ? [[{ t: tr('art.noneNote'), fg: PALETTE.info, italic: true }]] : renderBlocks(doc.parsed.blocks, w);
|
|
589
|
+
doc.lines = lines.length ? lines : [[{ t: tr('art.empty'), fg: PALETTE.info, italic: true }]];
|
|
590
|
+
doc.linesW = w;
|
|
591
|
+
return doc.lines;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const SOURCE_KEY = { local: 'art.srcLocal', record: 'art.srcRecord', none: 'art.srcNone', loading: 'art.srcLoading' };
|
|
595
|
+
|
|
596
|
+
// 머리 두 줄: 제목 · v3 · 출처 · 경고 / 경로. 좁으면 경고 → 버전 순으로 줄이고 제목은 잘라 남긴다.
|
|
597
|
+
export function headLines(doc, a, w, pos) {
|
|
598
|
+
const sep = { t: ' · ', fg: PALETTE.info };
|
|
599
|
+
const title = a?.title || doc.parsed?.title || '-';
|
|
600
|
+
const ver = a?.to != null ? `v${a.to}` : null;
|
|
601
|
+
const src = { t: tr(SOURCE_KEY[doc.kind] ?? 'art.srcNone'), fg: doc.kind === 'none' ? PALETTE.alert : doc.kind === 'record' ? PALETTE.waiting : PALETTE.busy, bold: true };
|
|
602
|
+
const warns = [doc.changed && { t: `! ${tr('art.mayDiffer')}`, fg: PALETTE.waiting, bold: true }, doc.cut && { t: tr('art.cut', { n: fmtBytes(SOURCE_MAX) }), fg: PALETTE.waiting }].filter(Boolean);
|
|
603
|
+
const tail = (k) => [...(k < 2 && ver ? [sep, { t: ver, fg: PALETTE.muted }] : []), sep, src, ...(k < 1 ? warns.flatMap((x) => [sep, x]) : [])];
|
|
604
|
+
const meta = pos ? [{ t: ` ${pos}`, fg: PALETTE.info }] : [];
|
|
605
|
+
let k = 0;
|
|
606
|
+
while (k < 2 && w - segsWidth([...tail(k), ...meta]) < Math.min(12, width(title))) k++;
|
|
607
|
+
const rest = [...tail(k), ...meta];
|
|
608
|
+
const first = [{ t: truncate(title, Math.max(1, w - segsWidth(rest))), fg: PALETTE.text, bold: true }, ...rest];
|
|
609
|
+
const lines = [first];
|
|
610
|
+
// 경고가 첫 줄에서 빠졌으면 둘째 줄로 내린다.
|
|
611
|
+
if (k >= 1 && warns.length) lines.push(warns.flatMap((x, i) => (i ? [sep, x] : [x])));
|
|
612
|
+
if (doc.path) lines.push([{ t: truncateLeft(tilde(doc.path), w), fg: PALETTE.muted }]);
|
|
613
|
+
return lines;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// 전체 화면: 상자(W×H) 안에 머리, 가로줄, 본문. 스크롤 위치는 여기서 본문 길이에 맞춰 다시 맞춘다.
|
|
617
|
+
export function artifactScreen(doc, a, W, H, view) {
|
|
618
|
+
const inner = Math.max(1, W - 4);
|
|
619
|
+
const body = docLines(doc, inner);
|
|
620
|
+
const rows = Math.max(1, H - 2);
|
|
621
|
+
// 머리 줄 수를 먼저 정한 뒤 본문 쪽수를 센다.
|
|
622
|
+
const head0 = headLines(doc, a, inner, '');
|
|
623
|
+
const page = Math.max(1, rows - head0.length - 1);
|
|
624
|
+
view.artPage = Math.max(1, page - 1);
|
|
625
|
+
const max = Math.max(0, body.length - page);
|
|
626
|
+
if (view.artScroll === Infinity) view.artScroll = max;
|
|
627
|
+
const top = Math.min(Math.max(0, view.artScroll ?? 0), max);
|
|
628
|
+
view.artScroll = top;
|
|
629
|
+
const pos = body.length > page ? `${Math.round(((top + page) / body.length) * 100)}%` : '';
|
|
630
|
+
const head = headLines(doc, a, inner, pos);
|
|
631
|
+
const lines = [...head, [{ t: glyphs().rule.repeat(inner), fg: PALETTE.brand }], ...body.slice(top, top + page)];
|
|
632
|
+
const title = [{ t: tr('view.artifact'), fg: PALETTE.brand, bold: true }];
|
|
633
|
+
return box(title, W, H, null, lines.slice(0, rows), 0, PALETTE.brand, true);
|
|
634
|
+
}
|
package/src/ui/chrome.mjs
CHANGED
|
@@ -202,6 +202,8 @@ export function footer(view, W, matched, sel) {
|
|
|
202
202
|
const input = NO_COLOR ? [{ t: '[/]' }] : [{ t: ' / ', fg: PALETTE.chipText, bg: PALETTE.brand, bold: true }];
|
|
203
203
|
return hintBar([{ segs: [...input, { t: ` ${view.filter ?? ''}`, fg: PALETTE.text, bold: true }, { t: glyphs().cursor, fg: PALETTE.brand }, { t: ` ${t('foot.count', { n: matched })} `, fg: PALETTE.muted }], keep: true }, { k: '⏎', t: t('key.apply'), keep: true }, { k: 'Esc', t: t('foot.clear'), keep: true }], W);
|
|
204
204
|
}
|
|
205
|
+
if (view.focus === 'artifact') return hintBar([...toast, { k: '↑↓', t: t('key.scroll') }, { k: 'space', t: t('key.page') }, { k: 'Home End', t: t('key.ends') }, { k: '[ ]', t: t('key.prevNextArt') }, { k: 'Esc', t: t('foot.toArtifacts'), keep: true }, ...tail], W, RO);
|
|
206
|
+
if (view.focus === 'artifacts') return hintBar([...toast, { k: '↑↓', t: t('foot.pickArtifact') }, { k: '⏎', t: t('foot.viewSource') }, { k: 'Esc', t: t('foot.toSessions'), keep: true }, ...tail], W, RO);
|
|
205
207
|
if (view.focus) {
|
|
206
208
|
const keys =
|
|
207
209
|
view.focus === 'diff'
|
|
@@ -222,6 +224,8 @@ export function footer(view, W, matched, sel) {
|
|
|
222
224
|
{ k: 'n', t: t('foot.next') },
|
|
223
225
|
...(order ? [order] : []),
|
|
224
226
|
...(sel?.files?.length ? [{ k: 'f', t: t('foot.files') }] : []),
|
|
227
|
+
// 아티팩트 목록(v)은 상세 패널이 열려 있고 게시한 것이 있을 때만 안내한다 (#307).
|
|
228
|
+
...(view.showDetail && sel?.artifacts?.length ? [{ k: 'v', t: t('foot.artifacts') }] : []),
|
|
225
229
|
{ k: 'd', t: t(view.showDetail ? 'foot.hideDetail' : 'foot.showDetail') },
|
|
226
230
|
ended,
|
|
227
231
|
{ k: 'c', t: t('foot.costMode') },
|