mayx-cli 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +126 -2
- package/bin/mayx.js +23 -0
- package/package.json +30 -4
- package/src/ansi.js +62 -0
- package/src/canvas.js +352 -0
- package/src/cli.js +422 -0
- package/src/commands.js +303 -0
- package/src/data.js +408 -0
- package/src/format.js +170 -0
- package/src/markdown.js +267 -0
- package/src/open.js +56 -0
- package/src/tui.js +1117 -0
package/src/tui.js
ADDED
|
@@ -0,0 +1,1117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 全屏 TUI。
|
|
5
|
+
*
|
|
6
|
+
* 布局:
|
|
7
|
+
* ┌ 顶栏 ────────────────────────────────┐
|
|
8
|
+
* ├ 文章列表 ─┬─ 正文 ───────────────────┤
|
|
9
|
+
* ├ 状态栏 ──────────────────────────────┤
|
|
10
|
+
* └ 输 入 行 ─────────────────────────────┘
|
|
11
|
+
*
|
|
12
|
+
* 渲染是全量重绘(每帧把整屏行拼出来再一次性写回),终端里这样做最不容易出现残影。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { C, runs, push, runsFit, runsWidth, wrapRuns, strWidth, toAnsi, overlayRow } = require('./canvas');
|
|
16
|
+
const data = require('./data');
|
|
17
|
+
const mdRenderer = require('./markdown');
|
|
18
|
+
const fmt = require('./format');
|
|
19
|
+
const commands = require('./commands');
|
|
20
|
+
const { openInBrowser, copyToClipboard } = require('./open');
|
|
21
|
+
|
|
22
|
+
const MIN_W = 34;
|
|
23
|
+
const MIN_H = 10;
|
|
24
|
+
|
|
25
|
+
/* ============================ 文本小工具 ============================ */
|
|
26
|
+
|
|
27
|
+
function padTo(s, w) {
|
|
28
|
+
const sw = strWidth(s);
|
|
29
|
+
return sw >= w ? s : s + ' '.repeat(w - sw);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function clip(s, w) {
|
|
33
|
+
if (w <= 0) return '';
|
|
34
|
+
if (strWidth(s) <= w) return s;
|
|
35
|
+
let acc = '';
|
|
36
|
+
let used = 0;
|
|
37
|
+
for (const ch of s) {
|
|
38
|
+
const cw = strWidth(ch);
|
|
39
|
+
if (used + cw > w - 1) break;
|
|
40
|
+
acc += ch;
|
|
41
|
+
used += cw;
|
|
42
|
+
}
|
|
43
|
+
return acc + '…';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function lineLR(left, right, w) {
|
|
47
|
+
const rw = Math.min(runsWidth(right), Math.max(0, w - 1));
|
|
48
|
+
const lw = Math.max(0, w - rw);
|
|
49
|
+
return [...runsFit(left, lw), ...runsFit(right, rw)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ============================ 状态 ============================ */
|
|
53
|
+
|
|
54
|
+
function createState() {
|
|
55
|
+
return {
|
|
56
|
+
mode: 'browse', // browse | search | cmd
|
|
57
|
+
focus: 'list', // list | content
|
|
58
|
+
pane: 'list', // 窄屏时显示哪一栏
|
|
59
|
+
posts: [],
|
|
60
|
+
list: [],
|
|
61
|
+
sel: 0,
|
|
62
|
+
listTop: 0,
|
|
63
|
+
scroll: 0,
|
|
64
|
+
view: null,
|
|
65
|
+
overlay: null,
|
|
66
|
+
input: '',
|
|
67
|
+
cursor: 0,
|
|
68
|
+
history: [],
|
|
69
|
+
historyIdx: -1,
|
|
70
|
+
loading: '',
|
|
71
|
+
msg: '',
|
|
72
|
+
msgStyle: '',
|
|
73
|
+
width: 80,
|
|
74
|
+
height: 24,
|
|
75
|
+
indexAt: 0,
|
|
76
|
+
stale: false,
|
|
77
|
+
geo: { wide: true, listW: 30, bodyTop: 2, bodyH: 20, contentInner: 40, listTop: 0 },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* ============================ 过滤 ============================ */
|
|
82
|
+
|
|
83
|
+
function applyFilter(st) {
|
|
84
|
+
const kw = st.input.trim().toLowerCase();
|
|
85
|
+
if (!kw) {
|
|
86
|
+
st.list = st.posts;
|
|
87
|
+
} else {
|
|
88
|
+
const parts = kw.split(/\s+/).filter(Boolean);
|
|
89
|
+
st.list = st.posts.filter((p) => parts.every((x) => p.hay.includes(x)));
|
|
90
|
+
}
|
|
91
|
+
st.sel = 0;
|
|
92
|
+
st.listTop = 0;
|
|
93
|
+
st.scroll = 0;
|
|
94
|
+
st.view = null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* ============================ 视图 ============================ */
|
|
98
|
+
|
|
99
|
+
function makeView(post, width, raw) {
|
|
100
|
+
const lines = fmt.metaBlock(post, width);
|
|
101
|
+
lines.push([]);
|
|
102
|
+
if (raw) {
|
|
103
|
+
lines.push(...mdRenderer.render(raw, width));
|
|
104
|
+
} else {
|
|
105
|
+
lines.push(...mdRenderer.render(post.excerpt, width));
|
|
106
|
+
lines.push([]);
|
|
107
|
+
lines.push([...runs('▸ 按 Enter 加载完整正文', C.dim)]);
|
|
108
|
+
}
|
|
109
|
+
return { post, width, raw: raw || null, lines };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function ensureView(st, width) {
|
|
113
|
+
const post = st.list[st.sel];
|
|
114
|
+
if (!post) {
|
|
115
|
+
st.view = null;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (st.view && st.view.post === post && st.view.width === width) return;
|
|
119
|
+
|
|
120
|
+
// 已经加载过正文、只是窗口宽度变了:保留正文重新排版
|
|
121
|
+
if (st.view && st.view.post === post && st.view.raw && st.view.width !== width) {
|
|
122
|
+
st.view = makeView(post, width, st.view.raw);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
st.view = makeView(post, width, null);
|
|
126
|
+
st.scroll = 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ============================ 各区块 ============================ */
|
|
130
|
+
|
|
131
|
+
function buildListPane(st, w, h) {
|
|
132
|
+
const rows = [];
|
|
133
|
+
const list = st.list;
|
|
134
|
+
|
|
135
|
+
if (!list.length) {
|
|
136
|
+
rows.push(runsFit(runs(st.posts.length ? '没有匹配的文章' : '正在载入索引…', C.dim), w));
|
|
137
|
+
while (rows.length < h) rows.push(runsFit([], w));
|
|
138
|
+
return rows;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 保证选中项在可视范围内
|
|
142
|
+
if (st.sel < st.listTop) st.listTop = st.sel;
|
|
143
|
+
if (st.sel >= st.listTop + h) st.listTop = st.sel - h + 1;
|
|
144
|
+
const maxTop = Math.max(0, list.length - h);
|
|
145
|
+
st.listTop = Math.max(0, Math.min(st.listTop, maxTop));
|
|
146
|
+
st.geo.listTop = st.listTop;
|
|
147
|
+
|
|
148
|
+
const titleW = Math.max(4, w - 8);
|
|
149
|
+
|
|
150
|
+
for (let i = 0; i < h; i++) {
|
|
151
|
+
const idx = st.listTop + i;
|
|
152
|
+
const p = list[idx];
|
|
153
|
+
if (!p) {
|
|
154
|
+
rows.push(runsFit([], w));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const selected = idx === st.sel;
|
|
158
|
+
const marker = selected ? '▸ ' : ' ';
|
|
159
|
+
const title = clip(p.title, titleW);
|
|
160
|
+
const date = (p.date || '').slice(5) || '';
|
|
161
|
+
|
|
162
|
+
if (selected) {
|
|
163
|
+
const plain = padTo(marker + padTo(title, titleW) + ' ' + date, w);
|
|
164
|
+
const style = C.reverse + (st.focus === 'list' ? C.bold : '');
|
|
165
|
+
rows.push(runsFit([{ t: plain, s: style }], w));
|
|
166
|
+
} else {
|
|
167
|
+
const row = [];
|
|
168
|
+
push(row, marker, C.cyan);
|
|
169
|
+
push(row, padTo(title, titleW), '');
|
|
170
|
+
push(row, ' ', '');
|
|
171
|
+
push(row, date, C.date);
|
|
172
|
+
rows.push(runsFit(row, w));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return rows;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function buildContentPane(st, w, h) {
|
|
179
|
+
const inner = Math.max(6, w - 1);
|
|
180
|
+
st.geo.contentInner = inner;
|
|
181
|
+
ensureView(st, inner);
|
|
182
|
+
|
|
183
|
+
const lines = st.view ? st.view.lines : [];
|
|
184
|
+
const maxScroll = Math.max(0, lines.length - h);
|
|
185
|
+
st.scroll = Math.max(0, Math.min(st.scroll, maxScroll));
|
|
186
|
+
|
|
187
|
+
const rows = [];
|
|
188
|
+
for (let i = 0; i < h; i++) {
|
|
189
|
+
rows.push(runsFit(lines[st.scroll + i] || [], inner));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 右侧滚动条
|
|
193
|
+
if (lines.length > h) {
|
|
194
|
+
const thumb = Math.max(1, Math.round((h * h) / lines.length));
|
|
195
|
+
const pos = maxScroll ? Math.round(((h - thumb) * st.scroll) / maxScroll) : 0;
|
|
196
|
+
for (let i = 0; i < h; i++) {
|
|
197
|
+
const onThumb = i >= pos && i < pos + thumb;
|
|
198
|
+
rows[i] = [...rows[i], { t: onThumb ? '█' : '│', s: onThumb ? C.cyan : C.border }];
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
for (let i = 0; i < h; i++) rows[i] = [...rows[i], { t: ' ', s: '' }];
|
|
202
|
+
}
|
|
203
|
+
return rows;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/* ============================ 弹窗 ============================ */
|
|
207
|
+
|
|
208
|
+
function drawBox(title, body, hint, w, h) {
|
|
209
|
+
const inner = w - 4;
|
|
210
|
+
const rows = [];
|
|
211
|
+
|
|
212
|
+
const top = [];
|
|
213
|
+
push(top, '╭─ ', C.border);
|
|
214
|
+
push(top, title, C.h3);
|
|
215
|
+
const tw = strWidth(title);
|
|
216
|
+
push(top, ' ' + '─'.repeat(Math.max(1, w - tw - 5)) + '╮', C.border);
|
|
217
|
+
rows.push(runsFit(top, w));
|
|
218
|
+
|
|
219
|
+
for (let i = 0; i < h; i++) {
|
|
220
|
+
const line = body[i] || [];
|
|
221
|
+
rows.push([...runs('│ ', C.border), ...runsFit(line, inner), ...runs(' │', C.border)]);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const bottom = [];
|
|
225
|
+
push(bottom, '╰', C.border);
|
|
226
|
+
if (hint) {
|
|
227
|
+
push(bottom, '─ ', C.border);
|
|
228
|
+
push(bottom, hint, C.dim);
|
|
229
|
+
const hw = strWidth(hint);
|
|
230
|
+
push(bottom, ' ' + '─'.repeat(Math.max(1, w - hw - 5)) + '╯', C.border);
|
|
231
|
+
} else {
|
|
232
|
+
push(bottom, '─'.repeat(Math.max(1, w - 2)) + '╯', C.border);
|
|
233
|
+
}
|
|
234
|
+
rows.push(runsFit(bottom, w));
|
|
235
|
+
return rows;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function buildOverlay(st, W, H, bodyH) {
|
|
239
|
+
const o = st.overlay;
|
|
240
|
+
const boxW = Math.max(30, Math.min(W - 4, 100));
|
|
241
|
+
// 内容区高度:整体(含上下边框)不能超过可见区域
|
|
242
|
+
const boxH = Math.max(3, Math.min(bodyH - 2, 26, H - 8));
|
|
243
|
+
const inner = boxW - 4;
|
|
244
|
+
|
|
245
|
+
const body = o.build ? o.build(inner) : [];
|
|
246
|
+
const maxScroll = Math.max(0, body.length - boxH);
|
|
247
|
+
o.scroll = Math.max(0, Math.min(o.scroll || 0, maxScroll));
|
|
248
|
+
|
|
249
|
+
const slice = body.slice(o.scroll, o.scroll + boxH);
|
|
250
|
+
const rows = drawBox(o.title, slice, o.hint, boxW, Math.min(boxH, Math.max(1, slice.length)));
|
|
251
|
+
o.geo = { boxW, boxH: rows.length, maxScroll };
|
|
252
|
+
return rows;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/* ============================ 组帧 ============================ */
|
|
256
|
+
|
|
257
|
+
function buildFrame(st, W, H) {
|
|
258
|
+
// 终端太小:给个明确提示,别画得乱七八糟
|
|
259
|
+
if (W < MIN_W || H < MIN_H) {
|
|
260
|
+
const rows = [];
|
|
261
|
+
rows.push(runsFit(runs('终端太小', C.warn), W));
|
|
262
|
+
rows.push(runsFit(runs(`需要至少 ${MIN_W}×${MIN_H}`, C.dim), W));
|
|
263
|
+
rows.push(runsFit(runs(`当前 ${W}×${H}`, C.dim), W));
|
|
264
|
+
while (rows.length < H) rows.push(runsFit([], W));
|
|
265
|
+
return rows.map(toAnsi);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
st.width = W;
|
|
269
|
+
st.height = H;
|
|
270
|
+
|
|
271
|
+
const bodyTop = 2;
|
|
272
|
+
const bodyH = H - 5;
|
|
273
|
+
st.geo.bodyTop = bodyTop;
|
|
274
|
+
st.geo.bodyH = bodyH;
|
|
275
|
+
|
|
276
|
+
/* --- 顶栏 --- */
|
|
277
|
+
const rightInfo = st.loading
|
|
278
|
+
? `◌ ${st.loading}`
|
|
279
|
+
: `${st.posts.length} 篇${st.stale ? ' · 离线缓存' : ''} · ${data.humanAge(st.indexAt)}`;
|
|
280
|
+
const header = lineLR(
|
|
281
|
+
[...runs('▌', C.cyan), ...runs(' ' + data.SITE_NAME, C.title)],
|
|
282
|
+
[...runs(rightInfo, st.stale ? C.warn : C.dim)],
|
|
283
|
+
W
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const sep = runsFit(runs('─'.repeat(W), C.border), W);
|
|
287
|
+
|
|
288
|
+
/* --- 主体 --- */
|
|
289
|
+
const wide = W >= 72;
|
|
290
|
+
st.geo.wide = wide;
|
|
291
|
+
const listW = wide ? Math.max(26, Math.min(44, Math.round(W * 0.33))) : W;
|
|
292
|
+
st.geo.listW = listW;
|
|
293
|
+
|
|
294
|
+
const body = [];
|
|
295
|
+
if (wide) {
|
|
296
|
+
const listRows = buildListPane(st, listW, bodyH);
|
|
297
|
+
const contentRows = buildContentPane(st, W - listW - 1, bodyH);
|
|
298
|
+
for (let i = 0; i < bodyH; i++) {
|
|
299
|
+
body.push([...listRows[i], { t: '│', s: C.border }, ...contentRows[i]]);
|
|
300
|
+
}
|
|
301
|
+
} else if (st.pane === 'list') {
|
|
302
|
+
const listRows = buildListPane(st, W, bodyH);
|
|
303
|
+
for (let i = 0; i < bodyH; i++) body.push(runsFit(listRows[i], W));
|
|
304
|
+
} else {
|
|
305
|
+
const contentRows = buildContentPane(st, W, bodyH);
|
|
306
|
+
for (let i = 0; i < bodyH; i++) body.push(runsFit(contentRows[i], W));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/* --- 状态栏 --- */
|
|
310
|
+
let hintText;
|
|
311
|
+
if (st.mode === 'search') hintText = '输入关键词 · 支持空格分隔多关键词 · Enter 确认 · Esc 取消';
|
|
312
|
+
else if (st.mode === 'cmd') hintText = '输入命令 · Enter 执行 · ↑↓ 历史 · Esc 取消';
|
|
313
|
+
else if (st.overlay) hintText = 'Esc 关闭';
|
|
314
|
+
else if (st.focus === 'content') hintText = '↑↓ 滚动 · Space 翻页 · o 浏览器 · c 评论 · Esc 返回列表';
|
|
315
|
+
else hintText = '↑↓ 选择 · Enter 阅读 · Tab 切窗 · / 搜索 · : 命令 · ? 帮助 · q 退出';
|
|
316
|
+
|
|
317
|
+
const status = lineLR(runs(st.msg || '', st.msgStyle), runs(hintText, C.dim), W);
|
|
318
|
+
|
|
319
|
+
/* --- 输入行 --- */
|
|
320
|
+
let inputRow;
|
|
321
|
+
if (st.mode === 'search' || st.mode === 'cmd') {
|
|
322
|
+
const prompt = st.mode === 'search' ? '/' : ':';
|
|
323
|
+
const chars = Array.from(st.input);
|
|
324
|
+
const cur = Math.max(0, Math.min(st.cursor, chars.length));
|
|
325
|
+
let before = chars.slice(0, cur).join('');
|
|
326
|
+
const at = chars[cur] !== undefined ? chars[cur] : ' ';
|
|
327
|
+
const after = chars.slice(cur + 1).join('');
|
|
328
|
+
|
|
329
|
+
const avail = W - 3;
|
|
330
|
+
// 太长就裁掉左边,保证光标可见
|
|
331
|
+
while (strWidth(before) > avail && before.length) before = Array.from(before).slice(1).join('');
|
|
332
|
+
|
|
333
|
+
const row = [];
|
|
334
|
+
push(row, prompt + ' ', C.cyan);
|
|
335
|
+
push(row, before, '');
|
|
336
|
+
push(row, at, C.reverse);
|
|
337
|
+
push(row, after, '');
|
|
338
|
+
inputRow = runsFit(row, W);
|
|
339
|
+
} else {
|
|
340
|
+
const url = st.view && st.view.post ? st.view.post.webUrl : '';
|
|
341
|
+
inputRow = runsFit([...runs(url ? ' ' + url : '', C.dim)], W);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let rows = [header, sep, ...body, sep, status, inputRow];
|
|
345
|
+
|
|
346
|
+
/* --- 弹窗 --- */
|
|
347
|
+
if (st.overlay) {
|
|
348
|
+
// 背景整体压暗,形成「模态」的观感
|
|
349
|
+
for (let i = 0; i < bodyH; i++) {
|
|
350
|
+
rows[bodyTop + i] = rows[bodyTop + i].map((r) => (r.t.trim() ? { t: r.t, s: C.dim } : { t: r.t, s: '' }));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const box = buildOverlay(st, W, H, bodyH);
|
|
354
|
+
const boxW = st.overlay.geo.boxW;
|
|
355
|
+
const x = Math.floor((W - boxW) / 2);
|
|
356
|
+
const y = bodyTop + Math.floor((bodyH - box.length) / 2);
|
|
357
|
+
for (let i = 0; i < box.length; i++) {
|
|
358
|
+
const row = y + i;
|
|
359
|
+
if (row < bodyTop || row >= bodyTop + bodyH) continue;
|
|
360
|
+
rows[row] = overlayRow(rows[row], box[i], x);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return rows.map((r) => toAnsi(runsFit(r, W)));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/* ============================ 键盘解析 ============================ */
|
|
368
|
+
|
|
369
|
+
const SEQ = [
|
|
370
|
+
['\x1b[1;2A', 'up'], ['\x1b[1;2B', 'down'],
|
|
371
|
+
['\x1b[5~', 'pgup'], ['\x1b[6~', 'pgdn'], ['\x1b[1~', 'home'], ['\x1b[4~', 'end'],
|
|
372
|
+
['\x1b[2~', 'insert'], ['\x1b[3~', 'del'],
|
|
373
|
+
['\x1b[A', 'up'], ['\x1b[B', 'down'], ['\x1b[C', 'right'], ['\x1b[D', 'left'],
|
|
374
|
+
['\x1bOA', 'up'], ['\x1bOB', 'down'], ['\x1bOC', 'right'], ['\x1bOD', 'left'],
|
|
375
|
+
['\x1b[H', 'home'], ['\x1b[F', 'end'], ['\x1bOH', 'home'], ['\x1bOF', 'end'],
|
|
376
|
+
['\x1b[Z', 'shift-tab'],
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
const CTRL = {
|
|
380
|
+
'\x03': 'c-c', '\x04': 'c-d', '\x0c': 'c-l', '\x15': 'c-u',
|
|
381
|
+
'\x0e': 'c-n', '\x10': 'c-p', '\x01': 'c-a', '\x05': 'c-e',
|
|
382
|
+
'\x7f': 'backspace', '\b': 'backspace',
|
|
383
|
+
'\r': 'enter', '\n': 'enter', '\t': 'tab',
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
function parseKeys(buf) {
|
|
387
|
+
const keys = [];
|
|
388
|
+
let i = 0;
|
|
389
|
+
while (i < buf.length) {
|
|
390
|
+
const rest = buf.slice(i);
|
|
391
|
+
|
|
392
|
+
if (rest[0] === '\x1b') {
|
|
393
|
+
const m = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])/.exec(rest);
|
|
394
|
+
if (m) {
|
|
395
|
+
keys.push({ k: 'mouse', button: +m[1], x: +m[2], y: +m[3], down: m[4] === 'M' });
|
|
396
|
+
i += m[0].length;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
let hit = null;
|
|
400
|
+
for (const [seq, name] of SEQ) {
|
|
401
|
+
if (rest.startsWith(seq)) {
|
|
402
|
+
hit = [seq, name];
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (hit) {
|
|
407
|
+
keys.push({ k: hit[1] });
|
|
408
|
+
i += hit[0].length;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (rest.length === 1) break; // 可能是 ESC 的开头,等下一批数据
|
|
412
|
+
keys.push({ k: 'esc' });
|
|
413
|
+
i += 1;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const ch = String.fromCodePoint(rest.codePointAt(0));
|
|
418
|
+
if (CTRL[ch]) {
|
|
419
|
+
keys.push({ k: CTRL[ch] });
|
|
420
|
+
i += ch.length;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
keys.push({ k: 'char', ch });
|
|
424
|
+
i += ch.length;
|
|
425
|
+
}
|
|
426
|
+
return { keys, rest: buf.slice(i) };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/* ============================ 交互 ============================ */
|
|
430
|
+
|
|
431
|
+
function run(opts) {
|
|
432
|
+
const out = process.stdout;
|
|
433
|
+
const inp = process.stdin;
|
|
434
|
+
|
|
435
|
+
const st = createState();
|
|
436
|
+
let started = false;
|
|
437
|
+
let pendingRender = null;
|
|
438
|
+
let escTimer = null;
|
|
439
|
+
let buf = '';
|
|
440
|
+
const size = () => {
|
|
441
|
+
const W = out.columns || 80;
|
|
442
|
+
const H = out.rows || 24;
|
|
443
|
+
return [Math.max(1, W), Math.max(1, H)];
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const paint = () => {
|
|
447
|
+
if (!started) return;
|
|
448
|
+
const [W, H] = size();
|
|
449
|
+
const frame = buildFrame(st, W, H);
|
|
450
|
+
out.write('\x1b[H' + frame.join('\r\n'));
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
const render = () => {
|
|
454
|
+
if (pendingRender) return;
|
|
455
|
+
pendingRender = setImmediate(() => {
|
|
456
|
+
pendingRender = null;
|
|
457
|
+
paint();
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const cleanup = () => {
|
|
462
|
+
if (!started) return;
|
|
463
|
+
started = false;
|
|
464
|
+
try {
|
|
465
|
+
inp.setRawMode(false);
|
|
466
|
+
} catch (_) {
|
|
467
|
+
/* ignore */
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
inp.pause();
|
|
471
|
+
} catch (_) {
|
|
472
|
+
/* ignore */
|
|
473
|
+
}
|
|
474
|
+
out.write('\x1b[0m\x1b[?7h\x1b[?1000l\x1b[?1006l\x1b[?25h\x1b[?1049l');
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const quit = () => {
|
|
478
|
+
cleanup();
|
|
479
|
+
process.exit(0);
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
/* ---- 异步动作 ---- */
|
|
483
|
+
|
|
484
|
+
const loadFull = async () => {
|
|
485
|
+
const post = st.list[st.sel];
|
|
486
|
+
if (!post) return;
|
|
487
|
+
const inner = st.geo.contentInner || Math.max(10, (st.geo.wide ? st.width - st.geo.listW - 1 : st.width) - 1);
|
|
488
|
+
st.loading = '正在获取正文…';
|
|
489
|
+
render();
|
|
490
|
+
try {
|
|
491
|
+
const raw = await data.readPost(post);
|
|
492
|
+
st.view = makeView(post, inner, raw);
|
|
493
|
+
st.scroll = 0;
|
|
494
|
+
st.focus = 'content';
|
|
495
|
+
if (!st.geo.wide) st.pane = 'content';
|
|
496
|
+
st.msg = '';
|
|
497
|
+
} catch (e) {
|
|
498
|
+
st.msg = e.message;
|
|
499
|
+
st.msgStyle = C.err;
|
|
500
|
+
if (!st.geo.wide) st.pane = 'content';
|
|
501
|
+
} finally {
|
|
502
|
+
st.loading = '';
|
|
503
|
+
render();
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const refreshIndex = async () => {
|
|
508
|
+
st.loading = '正在刷新索引…';
|
|
509
|
+
st.msg = '';
|
|
510
|
+
render();
|
|
511
|
+
try {
|
|
512
|
+
const res = await data.loadIndex({ refresh: true });
|
|
513
|
+
st.posts = res.posts;
|
|
514
|
+
st.indexAt = res.at;
|
|
515
|
+
st.stale = res.stale;
|
|
516
|
+
applyFilter(st);
|
|
517
|
+
st.msg = `索引已更新,共 ${st.posts.length} 篇`;
|
|
518
|
+
st.msgStyle = C.ok;
|
|
519
|
+
} catch (e) {
|
|
520
|
+
st.msg = e.message;
|
|
521
|
+
st.msgStyle = C.err;
|
|
522
|
+
} finally {
|
|
523
|
+
st.loading = '';
|
|
524
|
+
render();
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const exec = async (line) => {
|
|
529
|
+
const cmd = String(line || '').trim();
|
|
530
|
+
st.mode = 'browse';
|
|
531
|
+
st.input = '';
|
|
532
|
+
st.cursor = 0;
|
|
533
|
+
if (!cmd) {
|
|
534
|
+
render();
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
st.history.unshift(cmd);
|
|
538
|
+
if (st.history.length > 50) st.history.pop();
|
|
539
|
+
st.historyIdx = -1;
|
|
540
|
+
|
|
541
|
+
const current = st.list[st.sel] || null;
|
|
542
|
+
st.loading = '执行中…';
|
|
543
|
+
render();
|
|
544
|
+
|
|
545
|
+
let res;
|
|
546
|
+
try {
|
|
547
|
+
res = await commands.run(cmd, { posts: st.posts, current });
|
|
548
|
+
} catch (e) {
|
|
549
|
+
st.loading = '';
|
|
550
|
+
st.msg = e.message;
|
|
551
|
+
st.msgStyle = C.err;
|
|
552
|
+
render();
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
st.loading = '';
|
|
556
|
+
|
|
557
|
+
if (!res) {
|
|
558
|
+
render();
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
switch (res.kind) {
|
|
563
|
+
case 'quit':
|
|
564
|
+
quit();
|
|
565
|
+
return;
|
|
566
|
+
case 'refresh':
|
|
567
|
+
await refreshIndex();
|
|
568
|
+
return;
|
|
569
|
+
case 'clear':
|
|
570
|
+
st.input = '';
|
|
571
|
+
applyFilter(st);
|
|
572
|
+
st.msg = '已清空搜索';
|
|
573
|
+
st.msgStyle = C.dim;
|
|
574
|
+
render();
|
|
575
|
+
return;
|
|
576
|
+
case 'view': {
|
|
577
|
+
const idx = st.posts.indexOf(res.post);
|
|
578
|
+
if (idx >= 0) {
|
|
579
|
+
if (st.list !== st.posts) {
|
|
580
|
+
st.input = '';
|
|
581
|
+
st.list = st.posts;
|
|
582
|
+
}
|
|
583
|
+
st.sel = idx;
|
|
584
|
+
}
|
|
585
|
+
await loadFull();
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
case 'overlay':
|
|
589
|
+
st.overlay = {
|
|
590
|
+
title: res.title,
|
|
591
|
+
build: res.build,
|
|
592
|
+
hint: res.hint || 'Esc 关闭',
|
|
593
|
+
scroll: 0,
|
|
594
|
+
};
|
|
595
|
+
render();
|
|
596
|
+
return;
|
|
597
|
+
default:
|
|
598
|
+
render();
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
/* ---- 状态操作 ---- */
|
|
603
|
+
|
|
604
|
+
const moveSel = (delta) => {
|
|
605
|
+
if (!st.list.length) return;
|
|
606
|
+
const next = Math.max(0, Math.min(st.list.length - 1, st.sel + delta));
|
|
607
|
+
if (next !== st.sel) {
|
|
608
|
+
st.sel = next;
|
|
609
|
+
st.scroll = 0;
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
const maxScroll = () => {
|
|
614
|
+
const total = st.view ? st.view.lines.length : 0;
|
|
615
|
+
const h = Math.max(1, st.geo.bodyH - 1);
|
|
616
|
+
return Math.max(0, total - h);
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
const nav = (delta) => {
|
|
620
|
+
if (st.focus === 'content' && (st.geo.wide || st.pane === 'content')) {
|
|
621
|
+
st.scroll = Math.max(0, Math.min(st.scroll, maxScroll()) + delta);
|
|
622
|
+
} else {
|
|
623
|
+
moveSel(delta);
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const page = (dir) => {
|
|
628
|
+
const h = Math.max(1, st.geo.bodyH - 1);
|
|
629
|
+
if (st.focus === 'content' && (st.geo.wide || st.pane === 'content')) {
|
|
630
|
+
st.scroll = Math.max(0, Math.min(st.scroll, maxScroll()) + dir * h);
|
|
631
|
+
} else {
|
|
632
|
+
moveSel(dir * h);
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
const enterInput = (mode) => {
|
|
637
|
+
st.mode = mode;
|
|
638
|
+
st.input = '';
|
|
639
|
+
st.cursor = 0;
|
|
640
|
+
st.historyIdx = -1;
|
|
641
|
+
st.msg = '';
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
const commitInput = () => {
|
|
645
|
+
if (st.mode === 'search') {
|
|
646
|
+
st.mode = 'browse';
|
|
647
|
+
st.focus = 'list';
|
|
648
|
+
st.msg = st.input.trim() ? `筛选:${st.input.trim()}` : '';
|
|
649
|
+
st.msgStyle = C.dim;
|
|
650
|
+
render();
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (st.mode === 'cmd') {
|
|
654
|
+
const line = st.input;
|
|
655
|
+
st.input = '';
|
|
656
|
+
st.cursor = 0;
|
|
657
|
+
exec(line);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
const cancelInput = () => {
|
|
662
|
+
const wasSearch = st.mode === 'search';
|
|
663
|
+
st.mode = 'browse';
|
|
664
|
+
st.input = '';
|
|
665
|
+
st.cursor = 0;
|
|
666
|
+
st.historyIdx = -1;
|
|
667
|
+
if (wasSearch) applyFilter(st);
|
|
668
|
+
render();
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
const insertChar = (ch) => {
|
|
672
|
+
const chars = Array.from(st.input);
|
|
673
|
+
const cur = Math.max(0, Math.min(st.cursor, chars.length));
|
|
674
|
+
chars.splice(cur, 0, ch);
|
|
675
|
+
st.input = chars.join('');
|
|
676
|
+
st.cursor = cur + 1;
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
/* ---- 键盘 ---- */
|
|
680
|
+
|
|
681
|
+
const handleInputKey = (key) => {
|
|
682
|
+
switch (key.k) {
|
|
683
|
+
case 'c-c':
|
|
684
|
+
case 'esc':
|
|
685
|
+
cancelInput();
|
|
686
|
+
return;
|
|
687
|
+
case 'enter':
|
|
688
|
+
commitInput();
|
|
689
|
+
return;
|
|
690
|
+
case 'backspace': {
|
|
691
|
+
const chars = Array.from(st.input);
|
|
692
|
+
if (st.cursor > 0) {
|
|
693
|
+
chars.splice(st.cursor - 1, 1);
|
|
694
|
+
st.input = chars.join('');
|
|
695
|
+
st.cursor -= 1;
|
|
696
|
+
if (st.mode === 'search') applyFilter(st);
|
|
697
|
+
}
|
|
698
|
+
render();
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
case 'del': {
|
|
702
|
+
const chars = Array.from(st.input);
|
|
703
|
+
if (st.cursor < chars.length) {
|
|
704
|
+
chars.splice(st.cursor, 1);
|
|
705
|
+
st.input = chars.join('');
|
|
706
|
+
if (st.mode === 'search') applyFilter(st);
|
|
707
|
+
}
|
|
708
|
+
render();
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
case 'left':
|
|
712
|
+
st.cursor = Math.max(0, st.cursor - 1);
|
|
713
|
+
render();
|
|
714
|
+
return;
|
|
715
|
+
case 'right':
|
|
716
|
+
st.cursor = Math.min(Array.from(st.input).length, st.cursor + 1);
|
|
717
|
+
render();
|
|
718
|
+
return;
|
|
719
|
+
case 'home':
|
|
720
|
+
case 'c-a':
|
|
721
|
+
st.cursor = 0;
|
|
722
|
+
render();
|
|
723
|
+
return;
|
|
724
|
+
case 'end':
|
|
725
|
+
case 'c-e':
|
|
726
|
+
st.cursor = Array.from(st.input).length;
|
|
727
|
+
render();
|
|
728
|
+
return;
|
|
729
|
+
case 'c-u':
|
|
730
|
+
st.input = '';
|
|
731
|
+
st.cursor = 0;
|
|
732
|
+
if (st.mode === 'search') applyFilter(st);
|
|
733
|
+
render();
|
|
734
|
+
return;
|
|
735
|
+
case 'up':
|
|
736
|
+
case 'down': {
|
|
737
|
+
if (st.mode === 'search') {
|
|
738
|
+
nav(key.k === 'up' ? -1 : 1);
|
|
739
|
+
render();
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
// 命令历史
|
|
743
|
+
if (!st.history.length) return;
|
|
744
|
+
if (key.k === 'up') {
|
|
745
|
+
st.historyIdx = Math.min(st.history.length - 1, st.historyIdx + 1);
|
|
746
|
+
} else {
|
|
747
|
+
st.historyIdx = Math.max(-1, st.historyIdx - 1);
|
|
748
|
+
}
|
|
749
|
+
st.input = st.historyIdx >= 0 ? st.history[st.historyIdx] : '';
|
|
750
|
+
st.cursor = Array.from(st.input).length;
|
|
751
|
+
render();
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
default:
|
|
755
|
+
if (key.k === 'char') {
|
|
756
|
+
insertChar(key.ch);
|
|
757
|
+
if (st.mode === 'search') applyFilter(st);
|
|
758
|
+
render();
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
const handleNormalKey = (key) => {
|
|
764
|
+
if (key.k === 'mouse') {
|
|
765
|
+
handleMouse(key);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// 字符键
|
|
770
|
+
if (key.k === 'char') {
|
|
771
|
+
const ch = key.ch;
|
|
772
|
+
switch (ch) {
|
|
773
|
+
case 'q':
|
|
774
|
+
quit();
|
|
775
|
+
return;
|
|
776
|
+
case 'j':
|
|
777
|
+
nav(1);
|
|
778
|
+
render();
|
|
779
|
+
return;
|
|
780
|
+
case 'k':
|
|
781
|
+
nav(-1);
|
|
782
|
+
render();
|
|
783
|
+
return;
|
|
784
|
+
case 'g':
|
|
785
|
+
if (st.focus === 'content') st.scroll = 0;
|
|
786
|
+
else {
|
|
787
|
+
st.sel = 0;
|
|
788
|
+
st.listTop = 0;
|
|
789
|
+
}
|
|
790
|
+
render();
|
|
791
|
+
return;
|
|
792
|
+
case 'G':
|
|
793
|
+
if (st.focus === 'content') st.scroll = 1e9;
|
|
794
|
+
else st.sel = st.list.length ? st.list.length - 1 : 0;
|
|
795
|
+
render();
|
|
796
|
+
return;
|
|
797
|
+
case '/':
|
|
798
|
+
enterInput('search');
|
|
799
|
+
render();
|
|
800
|
+
return;
|
|
801
|
+
case ':':
|
|
802
|
+
enterInput('cmd');
|
|
803
|
+
render();
|
|
804
|
+
return;
|
|
805
|
+
case '?':
|
|
806
|
+
st.overlay = {
|
|
807
|
+
title: '帮助',
|
|
808
|
+
build: commands.buildHelp,
|
|
809
|
+
hint: 'Esc 关闭',
|
|
810
|
+
scroll: 0,
|
|
811
|
+
};
|
|
812
|
+
render();
|
|
813
|
+
return;
|
|
814
|
+
case 'r':
|
|
815
|
+
exec('random');
|
|
816
|
+
return;
|
|
817
|
+
case 'o': {
|
|
818
|
+
const p = st.list[st.sel];
|
|
819
|
+
if (!p) return;
|
|
820
|
+
openInBrowser(p.webUrl);
|
|
821
|
+
st.msg = `已在浏览器中打开 #${p.num}`;
|
|
822
|
+
st.msgStyle = C.ok;
|
|
823
|
+
render();
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
case 'c': {
|
|
827
|
+
const p = st.list[st.sel];
|
|
828
|
+
if (!p) return;
|
|
829
|
+
exec(`comments ${p.num}`);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
case 'y': {
|
|
833
|
+
const p = st.list[st.sel];
|
|
834
|
+
if (!p) return;
|
|
835
|
+
copyToClipboard(p.webUrl);
|
|
836
|
+
st.msg = `已复制链接 ${p.webUrl}`;
|
|
837
|
+
st.msgStyle = C.ok;
|
|
838
|
+
render();
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
case ' ':
|
|
842
|
+
page(1);
|
|
843
|
+
render();
|
|
844
|
+
return;
|
|
845
|
+
default:
|
|
846
|
+
break;
|
|
847
|
+
}
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
switch (key.k) {
|
|
852
|
+
case 'c-c':
|
|
853
|
+
quit();
|
|
854
|
+
return;
|
|
855
|
+
case 'c-l':
|
|
856
|
+
out.write('\x1b[2J');
|
|
857
|
+
render();
|
|
858
|
+
return;
|
|
859
|
+
case 'up':
|
|
860
|
+
case 'c-p':
|
|
861
|
+
nav(-1);
|
|
862
|
+
render();
|
|
863
|
+
return;
|
|
864
|
+
case 'down':
|
|
865
|
+
case 'c-n':
|
|
866
|
+
nav(1);
|
|
867
|
+
render();
|
|
868
|
+
return;
|
|
869
|
+
case 'pgup':
|
|
870
|
+
page(-1);
|
|
871
|
+
render();
|
|
872
|
+
return;
|
|
873
|
+
case 'pgdn':
|
|
874
|
+
page(1);
|
|
875
|
+
render();
|
|
876
|
+
return;
|
|
877
|
+
case 'home':
|
|
878
|
+
if (st.focus === 'content') st.scroll = 0;
|
|
879
|
+
else {
|
|
880
|
+
st.sel = 0;
|
|
881
|
+
st.listTop = 0;
|
|
882
|
+
}
|
|
883
|
+
render();
|
|
884
|
+
return;
|
|
885
|
+
case 'end':
|
|
886
|
+
if (st.focus === 'content') st.scroll = 1e9;
|
|
887
|
+
else st.sel = st.list.length ? st.list.length - 1 : 0;
|
|
888
|
+
render();
|
|
889
|
+
return;
|
|
890
|
+
case 'enter':
|
|
891
|
+
if (st.focus === 'list' || !st.geo.wide) {
|
|
892
|
+
if (st.overlay) return;
|
|
893
|
+
loadFull();
|
|
894
|
+
}
|
|
895
|
+
return;
|
|
896
|
+
case 'tab':
|
|
897
|
+
if (st.geo.wide) {
|
|
898
|
+
st.focus = st.focus === 'list' ? 'content' : 'list';
|
|
899
|
+
} else {
|
|
900
|
+
st.pane = st.pane === 'list' ? 'content' : 'list';
|
|
901
|
+
st.focus = st.pane;
|
|
902
|
+
}
|
|
903
|
+
render();
|
|
904
|
+
return;
|
|
905
|
+
case 'esc':
|
|
906
|
+
if (st.focus === 'content') {
|
|
907
|
+
st.focus = 'list';
|
|
908
|
+
if (!st.geo.wide) st.pane = 'list';
|
|
909
|
+
}
|
|
910
|
+
render();
|
|
911
|
+
return;
|
|
912
|
+
case 'c-d':
|
|
913
|
+
page(1);
|
|
914
|
+
render();
|
|
915
|
+
return;
|
|
916
|
+
default:
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
const handleOverlayKey = (key) => {
|
|
921
|
+
const o = st.overlay;
|
|
922
|
+
// 先把 scroll 收敛到合法范围,否则按过 End 之后按 ↑ 会「看起来没反应」
|
|
923
|
+
const max = (o.geo && o.geo.maxScroll) || 0;
|
|
924
|
+
o.scroll = Math.max(0, Math.min(o.scroll || 0, max));
|
|
925
|
+
|
|
926
|
+
switch (key.k) {
|
|
927
|
+
case 'esc':
|
|
928
|
+
case 'q':
|
|
929
|
+
st.overlay = null;
|
|
930
|
+
break;
|
|
931
|
+
case 'up':
|
|
932
|
+
case 'c-p':
|
|
933
|
+
o.scroll = Math.max(0, o.scroll - 1);
|
|
934
|
+
break;
|
|
935
|
+
case 'down':
|
|
936
|
+
case 'c-n':
|
|
937
|
+
o.scroll = Math.min(max, o.scroll + 1);
|
|
938
|
+
break;
|
|
939
|
+
case 'pgup':
|
|
940
|
+
o.scroll = Math.max(0, o.scroll - 10);
|
|
941
|
+
break;
|
|
942
|
+
case 'pgdn':
|
|
943
|
+
o.scroll = Math.min(max, o.scroll + 10);
|
|
944
|
+
break;
|
|
945
|
+
case 'home':
|
|
946
|
+
o.scroll = 0;
|
|
947
|
+
break;
|
|
948
|
+
case 'end':
|
|
949
|
+
o.scroll = max;
|
|
950
|
+
break;
|
|
951
|
+
case 'c-c':
|
|
952
|
+
quit();
|
|
953
|
+
return;
|
|
954
|
+
default:
|
|
955
|
+
break;
|
|
956
|
+
}
|
|
957
|
+
render();
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
const handleMouse = (key) => {
|
|
961
|
+
const row = key.y - 1;
|
|
962
|
+
const col = key.x - 1;
|
|
963
|
+
const { bodyTop, bodyH, listW, listTop } = st.geo;
|
|
964
|
+
|
|
965
|
+
// 滚轮
|
|
966
|
+
if (key.button === 64 || key.button === 65) {
|
|
967
|
+
const dir = key.button === 64 ? -1 : 1;
|
|
968
|
+
const overList = st.geo.wide && col < listW;
|
|
969
|
+
if (overList) moveSel(dir * 3);
|
|
970
|
+
else st.scroll = Math.max(0, st.scroll + dir * 3);
|
|
971
|
+
render();
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
if (key.button !== 0 || !key.down) return;
|
|
976
|
+
|
|
977
|
+
// 点击正文区域 → 焦点移到正文
|
|
978
|
+
if (row >= bodyTop && row < bodyTop + bodyH) {
|
|
979
|
+
if (st.geo.wide && col > listW) {
|
|
980
|
+
st.focus = 'content';
|
|
981
|
+
render();
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
if (st.geo.wide || st.pane === 'list') {
|
|
985
|
+
const idx = listTop + (row - bodyTop);
|
|
986
|
+
if (idx >= 0 && idx < st.list.length) {
|
|
987
|
+
st.sel = idx;
|
|
988
|
+
st.focus = 'list';
|
|
989
|
+
render();
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
const handleKey = (key) => {
|
|
997
|
+
if (st.overlay) {
|
|
998
|
+
handleOverlayKey(key);
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
if (st.mode === 'search' || st.mode === 'cmd') {
|
|
1002
|
+
handleInputKey(key);
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
handleNormalKey(key);
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
/* ---- 启动 ---- */
|
|
1009
|
+
|
|
1010
|
+
out.write('\x1b[?1049h\x1b[?25l\x1b[?7l\x1b[?1000h\x1b[?1006h');
|
|
1011
|
+
started = true;
|
|
1012
|
+
|
|
1013
|
+
process.on('exit', cleanup);
|
|
1014
|
+
process.on('SIGTERM', () => {
|
|
1015
|
+
cleanup();
|
|
1016
|
+
process.exit(0);
|
|
1017
|
+
});
|
|
1018
|
+
process.on('SIGINT', () => {
|
|
1019
|
+
cleanup();
|
|
1020
|
+
process.exit(0);
|
|
1021
|
+
});
|
|
1022
|
+
process.on('uncaughtException', (e) => {
|
|
1023
|
+
cleanup();
|
|
1024
|
+
console.error('\n' + (e && e.stack ? e.stack : e));
|
|
1025
|
+
process.exit(1);
|
|
1026
|
+
});
|
|
1027
|
+
|
|
1028
|
+
inp.setRawMode(true);
|
|
1029
|
+
inp.resume();
|
|
1030
|
+
inp.setEncoding('utf8');
|
|
1031
|
+
|
|
1032
|
+
inp.on('data', (chunk) => {
|
|
1033
|
+
buf += chunk;
|
|
1034
|
+
const { keys, rest } = parseKeys(buf);
|
|
1035
|
+
buf = rest;
|
|
1036
|
+
if (keys.length && escTimer) {
|
|
1037
|
+
clearTimeout(escTimer);
|
|
1038
|
+
escTimer = null;
|
|
1039
|
+
}
|
|
1040
|
+
for (const k of keys) handleKey(k);
|
|
1041
|
+
// 孤立的 ESC:稍等一下看是不是转义序列的开头
|
|
1042
|
+
if (buf === '\x1b') {
|
|
1043
|
+
if (escTimer) clearTimeout(escTimer);
|
|
1044
|
+
escTimer = setTimeout(() => {
|
|
1045
|
+
buf = '';
|
|
1046
|
+
escTimer = null;
|
|
1047
|
+
handleKey({ k: 'esc' });
|
|
1048
|
+
}, 40);
|
|
1049
|
+
} else if (buf.length) {
|
|
1050
|
+
if (escTimer) clearTimeout(escTimer);
|
|
1051
|
+
escTimer = setTimeout(() => {
|
|
1052
|
+
// 无法解析的残留,直接丢掉,避免越积越多
|
|
1053
|
+
buf = '';
|
|
1054
|
+
escTimer = null;
|
|
1055
|
+
}, 50);
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
inp.on('end', quit);
|
|
1060
|
+
out.on('resize', () => {
|
|
1061
|
+
out.write('\x1b[2J');
|
|
1062
|
+
render();
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
// 首帧:先把界面画出来,再去拉数据
|
|
1066
|
+
paint();
|
|
1067
|
+
|
|
1068
|
+
(async () => {
|
|
1069
|
+
st.loading = '正在获取文章索引…';
|
|
1070
|
+
render();
|
|
1071
|
+
try {
|
|
1072
|
+
const res = await data.loadIndex({ refresh: opts && opts.refresh });
|
|
1073
|
+
st.posts = res.posts;
|
|
1074
|
+
st.indexAt = res.at;
|
|
1075
|
+
st.stale = res.stale;
|
|
1076
|
+
st.list = res.posts;
|
|
1077
|
+
st.loading = '';
|
|
1078
|
+
if (res.stale) {
|
|
1079
|
+
st.msg = '网络不可用,正在使用本地缓存的文章索引';
|
|
1080
|
+
st.msgStyle = C.warn;
|
|
1081
|
+
}
|
|
1082
|
+
render();
|
|
1083
|
+
|
|
1084
|
+
// 支持 `mayx <序号>` 直接打开某篇
|
|
1085
|
+
if (opts && opts.open !== undefined && opts.open !== null) {
|
|
1086
|
+
const post = data.resolvePost(st.posts, opts.open);
|
|
1087
|
+
if (post) {
|
|
1088
|
+
st.sel = st.posts.indexOf(post);
|
|
1089
|
+
await loadFull();
|
|
1090
|
+
} else {
|
|
1091
|
+
st.msg = `没有找到文章:${opts.open}`;
|
|
1092
|
+
st.msgStyle = C.err;
|
|
1093
|
+
render();
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
} catch (e) {
|
|
1097
|
+
st.loading = '';
|
|
1098
|
+
st.msg = e.message;
|
|
1099
|
+
st.msgStyle = C.err;
|
|
1100
|
+
st.overlay = {
|
|
1101
|
+
title: '出错了',
|
|
1102
|
+
build: () => [
|
|
1103
|
+
fmt.errorLine(e.message),
|
|
1104
|
+
[],
|
|
1105
|
+
[...runs('检查网络后按 :refresh 重试,或按 q 退出。', C.dim)],
|
|
1106
|
+
],
|
|
1107
|
+
hint: 'Esc 关闭',
|
|
1108
|
+
scroll: 0,
|
|
1109
|
+
};
|
|
1110
|
+
render();
|
|
1111
|
+
}
|
|
1112
|
+
})();
|
|
1113
|
+
|
|
1114
|
+
return { st, paint, quit };
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
module.exports = { run, buildFrame, createState, parseKeys, applyFilter };
|