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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mayx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,127 @@
1
- # Temporary Holding Version
1
+ # mayx
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [Mayx's Blog](https://mabbs.github.io) 的终端版。装完直接 `npx mayx-cli` 就能用,全屏界面像 GUI 一样点着看,也能敲命令。
4
+
5
+ ```
6
+ ▌ Mayx's Blog 183 篇 · 3 分钟前
7
+ ────────────────────────────────────────────────────────────────────────────────────────
8
+ 关于压缩的各种探索 11/01│#4 一次制作终端版博客的经历 █
9
+ 如何制作一个“完整”的… 10/01│2026/08/01 · #WASM #终端 #Linux #博客 █
10
+ Vibe Coding 新体验(… 09/01│─────────────────────────────────────────────────────────│
11
+ ▸ 一次制作终端版博客的… 08/01│ │
12
+ Vibe Coding新体验 07/01│在浏览器上用真的Linux访问博客会是什么样的感觉? │
13
+ 如何节约游戏占用的硬… 06/01│ │
14
+ 虚拟局域网的组网探索… 05/01│起因 │
15
+ 关于AI个人助理的探索 04/14│ │
16
+ 近期LLM的部署与应用… 03/01│在去年的时候我为了想在博客上加个终端,写过一篇关于在浏览 │
17
+ 在Google杀死XSLT之后… 02/08│器中运行Linux的文章。不过那时候只是调查了有哪些可能的方 │
18
+ 年终总结 01/01│法,并没有真的做出来,主要是自己做感觉太麻烦了😂。 │
19
+ 在浏览器中运行Linux… 12/01│但从上次进行了Vibe Coding之后,我意识到这可能不再是什么麻│
20
+ 让博客永恒的探索 11/01│烦事,也许只要和AI聊聊天就能轻松做出来了,于是接着之前的 │
21
+ 一次找回GitHub上被删… 10/12│方案,我开始着手完成我的想法。 │
22
+ 关于ZIP Quine与自产… 09/01│ │
23
+ 在Tilde社区的游玩体验 08/10│终端版博客的不同形式 │
24
+ 用Service Worker实现… 08/01│ │
25
+ ────────────────────────────────────────────────────────────────────────────────────────
26
+ ↑↓ 滚动 · Space 翻页 · o 浏览器 · c 评论 · Esc 返回列表
27
+ https://mabbs.github.io/2026/08/01/terminal.html
28
+ ```
29
+
30
+ ## 用
31
+
32
+ ```bash
33
+ npx mayx-cli # 直接跑,不用装
34
+ npm i -g mayx-cli # 或者装到全局,之后命令就叫 mayx
35
+ ```
36
+
37
+ 要求 Node.js 18 以上,无任何第三方依赖。
38
+
39
+ > 包名本来想叫 `mayx`,但 npm 认为它和 `mobx`/`rax`/`sax` 太像,不给注册,只好加个 `-cli` 后缀。
40
+
41
+ ## 全屏界面
42
+
43
+ 左边文章列表,右边正文,底下状态栏。键盘和鼠标都能用。
44
+
45
+ | 按键 | 作用 |
46
+ | --- | --- |
47
+ | `↑` `↓` / `j` `k` | 移动选择 · 滚动正文 |
48
+ | `PgUp` `PgDn` | 翻页 |
49
+ | `g` / `G` | 回到顶部 / 跳到底部 |
50
+ | `Enter` | 阅读全文(右窗格加载完整 Markdown) |
51
+ | `Tab` | 在文章列表和正文之间切换焦点 |
52
+ | `/` | 搜索,边打边过滤,支持空格分隔多关键词 |
53
+ | `:` | 命令模式 |
54
+ | `?` | 帮助 |
55
+ | `r` | 随机一篇 |
56
+ | `o` | 用浏览器打开当前文章 |
57
+ | `c` | 查看评论 |
58
+ | `y` | 复制当前文章链接 |
59
+ | `Esc` | 返回 / 关闭弹窗 / 清空搜索 |
60
+ | `q` | 退出 |
61
+
62
+ 鼠标滚轮滚动,点列表选中文章,点正文把焦点切过去。
63
+
64
+ 命令模式下可以敲:
65
+
66
+ ```
67
+ :list [页码] 分页列出文章
68
+ :get <序号|URL|标题> 查看文章元信息
69
+ :read <序号|URL|标题> 在主界面打开文章
70
+ :search <关键词> 搜索
71
+ :grep <模式> [--regex] 全文检索,带上下文
72
+ :random 随机打开一篇
73
+ :open <序号> 浏览器打开
74
+ :comments <序号> 看评论
75
+ :about 站点与作者信息
76
+ :refresh 强制刷新索引
77
+ :clear 清空搜索过滤
78
+ :help 帮助
79
+ ```
80
+
81
+ `id` 支持三种写法:序号(`12`,从 1 开始,1 是最新一篇)、URL(`/2026/11/01/compress.html`)或标题关键词。
82
+
83
+ ## 命令行模式
84
+
85
+ 不想要全屏也能直接用,输出可以接管道:
86
+
87
+ ```bash
88
+ mayx list # 列出第一页
89
+ mayx list 3 # 第三页
90
+ mayx read 12 # 读第 12 篇(也就是 mayx 12)
91
+ mayx search 压缩 # 搜索
92
+ mayx grep "WireGuard" # 全文检索
93
+ mayx grep 'v?2ray' --regex # 正则检索
94
+ mayx random --open # 随机一篇并用浏览器打开
95
+ mayx comments 4 # 看评论
96
+ mayx about # 站点信息
97
+ mayx refresh # 刷新索引
98
+ ```
99
+
100
+ 加上 `--json` 就是给脚本用的:
101
+
102
+ ```bash
103
+ mayx search jekyll --json | jq '.posts[].title'
104
+ mayx list --json | jq -r '.posts[] | "\(.num)\t\(.title)"'
105
+ ```
106
+
107
+ ## 缓存
108
+
109
+ 文章索引和正文缓存在 `~/.cache/mayx/`(索引默认 6 小时过期)。断网时只要之前读过,仍然能看。
110
+
111
+ | 环境变量 | 说明 |
112
+ | --- | --- |
113
+ | `MAYX_CACHE_TTL` | 索引缓存有效期(毫秒),默认 `21600000` |
114
+ | `MAYX_TIMEOUT` | 单次请求超时(毫秒),默认 `20000` |
115
+ | `NO_COLOR` | 设了就不上色 |
116
+
117
+ ## 数据从哪来
118
+
119
+ - 文章列表和全文:`https://mabbs.github.io/search.json`
120
+ - 完整 Markdown:GitHub 仓库 `Mabbs/mabbs.github.io` 的 `_posts/`
121
+ - 评论:Gitalk 存在 GitHub Issues 里,按 `Gitalk,<文章路径>` 这个 label 找
122
+
123
+ 网页版的控制台脚本(`assets/js/blog-console.js`)读评论时需要带 Gitalk 的 clientID/clientSecret 做 Basic 认证,这两个值本来就暴露在前端。CLI 读的是公开仓库,匿名请求就够,所以**这里没有任何密钥**。
124
+
125
+ ## License
126
+
127
+ MIT © Mayx
package/bin/mayx.js ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * mayx — Mayx's Blog in your terminal
6
+ *
7
+ * 不带参数且处于终端中 → 全屏界面;否则走命令模式。
8
+ */
9
+
10
+ const { main } = require('../src/cli');
11
+
12
+ main(process.argv.slice(2))
13
+ .then((code) => {
14
+ process.exitCode = code || 0;
15
+ })
16
+ .catch((e) => {
17
+ if (e && e.name === 'AbortError') {
18
+ process.stderr.write('请求超时,检查一下网络\n');
19
+ } else {
20
+ process.stderr.write((e && e.message ? e.message : String(e)) + '\n');
21
+ }
22
+ process.exitCode = 1;
23
+ });
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "mayx-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Mayx's Blog in your terminal — 全屏 TUI 阅读器,同时支持命令行操作",
5
+ "keywords": [
6
+ "blog",
7
+ "cli",
8
+ "tui",
9
+ "terminal",
10
+ "reader",
11
+ "mayx"
12
+ ],
13
+ "homepage": "https://mabbs.github.io",
14
+ "author": "Mayx <mayx@outlook.com>",
15
+ "license": "MIT",
16
+ "type": "commonjs",
17
+ "bin": {
18
+ "mayx": "bin/mayx.js",
19
+ "mayx-cli": "bin/mayx.js"
20
+ },
21
+ "files": [
22
+ "bin/",
23
+ "src/",
24
+ "README.md"
25
+ ],
26
+ "engines": {
27
+ "node": ">=18"
28
+ },
29
+ "scripts": {
30
+ "start": "node bin/mayx.js"
31
+ }
32
+ }
package/src/ansi.js ADDED
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 颜色与 SGR 样式。
5
+ *
6
+ * 设计原则:不主动设置背景色,只设置前景色与字重,这样在浅色和深色终端里
7
+ * 都能看清;反显(reverse)用于选中行,跟随终端自身的配色。
8
+ */
9
+
10
+ // 只在真正是终端、且没有被显式关闭时才上色。
11
+ // 管道输出(`mayx list | grep xxx`)会自动变成纯文本,方便二次处理。
12
+ const COLOR_ON =
13
+ !!process.stdout.isTTY &&
14
+ !process.env.NO_COLOR &&
15
+ process.env.TERM !== 'dumb' &&
16
+ !process.argv.includes('--no-color');
17
+
18
+ function sgr(code) {
19
+ return COLOR_ON ? `\x1b[${code}m` : '';
20
+ }
21
+
22
+ const C = {
23
+ reset: COLOR_ON ? '\x1b[0m' : '',
24
+ bold: sgr('1'),
25
+ dim: sgr('2'),
26
+ italic: sgr('3'),
27
+ underline: sgr('4'),
28
+ reverse: sgr('7'),
29
+ strike: sgr('9'),
30
+
31
+ black: sgr('30'),
32
+ red: sgr('31'),
33
+ green: sgr('32'),
34
+ yellow: sgr('33'),
35
+ blue: sgr('34'),
36
+ magenta: sgr('35'),
37
+ cyan: sgr('36'),
38
+ white: sgr('37'),
39
+ gray: sgr('90'),
40
+ };
41
+
42
+ // −−− 语义化样式 −−−
43
+ C.title = C.bold + C.cyan; // 文章标题
44
+ C.header = C.bold; // 顶栏站点名
45
+ C.h1 = C.bold + C.underline + C.cyan; // 一级标题
46
+ C.h2 = C.bold + C.cyan;
47
+ C.h3 = C.bold + C.yellow;
48
+ C.h4 = C.bold;
49
+ C.code = C.yellow; // 行内代码
50
+ C.codeBlock = C.dim + C.yellow; // 代码块
51
+ C.link = C.underline + C.cyan; // 链接
52
+ C.quote = C.dim + C.italic;
53
+ C.tag = C.magenta;
54
+ C.date = C.gray;
55
+ C.ok = C.green;
56
+ C.warn = C.yellow;
57
+ C.err = C.bold + C.red;
58
+ C.border = C.gray;
59
+ C.dimText = C.dim;
60
+ C.strong = C.bold;
61
+
62
+ module.exports = { C, COLOR_ON };
package/src/canvas.js ADDED
@@ -0,0 +1,352 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 画布层:把一行文本表示成「样式片段(run)数组」,再做宽度计算、裁剪、换行、叠加。
5
+ *
6
+ * 为什么不直接拼 ANSI 字符串:一旦要在一行中间插入边框、画滚动条、或者把弹窗
7
+ * 盖在已有内容上,就必须知道「第 N 列在哪里」。run 数组让这些操作都变成纯计算,
8
+ * 也能正确处理中文/emoji 占两列的问题。
9
+ */
10
+
11
+ const { C } = require('./ansi');
12
+
13
+ const RESET = '\x1b[0m';
14
+ const ANSI_RE = /\x1b\[[0-9;?]*[a-zA-Z]/g;
15
+
16
+ /* ============================ 字符宽度 ============================ */
17
+
18
+ function isWide(cp) {
19
+ return (
20
+ (cp >= 0x1100 && cp <= 0x115f) || // 谚文字母
21
+ (cp >= 0x2e80 && cp <= 0x303e) || // 部首、注音、CJK 标点
22
+ (cp >= 0x3041 && cp <= 0x33ff) || // 假名、CJK 兼容
23
+ (cp >= 0x3400 && cp <= 0x4dbf) || // CJK 扩展 A
24
+ (cp >= 0x4e00 && cp <= 0x9fff) || // CJK 基本区
25
+ (cp >= 0xa000 && cp <= 0xa4cf) || // 彝文
26
+ (cp >= 0xa960 && cp <= 0xa97f) ||
27
+ (cp >= 0xac00 && cp <= 0xd7a3) || // 谚文音节
28
+ (cp >= 0xf900 && cp <= 0xfaff) || // CJK 兼容表意
29
+ (cp >= 0xfe10 && cp <= 0xfe19) ||
30
+ (cp >= 0xfe30 && cp <= 0xfe6f) || // 竖排/小写变体
31
+ (cp >= 0xff00 && cp <= 0xff60) || // 全角 ASCII
32
+ (cp >= 0xffe0 && cp <= 0xffe6) ||
33
+ (cp >= 0x1f000 && cp <= 0x1f2ff) || // 麻将/牌/方框 emoji
34
+ (cp >= 0x1f300 && cp <= 0x1f64f) || // 常见 emoji
35
+ (cp >= 0x1f900 && cp <= 0x1f9ff) ||
36
+ (cp >= 0x1f680 && cp <= 0x1f6ff) ||
37
+ (cp >= 0x20000 && cp <= 0x3fffd) // CJK 扩展 B+
38
+ );
39
+ }
40
+
41
+ function charWidth(ch) {
42
+ const cp = ch.codePointAt(0);
43
+ // 组合附加符号、零宽字符不占位
44
+ if (cp === 0x200b || cp === 0x200c || cp === 0x200d || cp === 0xfeff) return 0;
45
+ if (cp >= 0x0300 && cp <= 0x036f) return 0;
46
+ if (cp >= 0xfe00 && cp <= 0xfe0f) return 0; // 变体选择符单独算 0,见下
47
+ return isWide(cp) ? 2 : 1;
48
+ }
49
+
50
+ /** 字符串的显示宽度(中文按 2 列计)。传入内容不应含 ANSI。 */
51
+ function strWidth(s) {
52
+ let w = 0;
53
+ let prev = 0;
54
+ for (const ch of s) {
55
+ const cp = ch.codePointAt(0);
56
+ if (cp === 0xfe0f) {
57
+ // ⚠️ 这类「字符 + FE0F」在终端里通常按 2 列渲染
58
+ if (prev === 1) w += 1;
59
+ prev = 0;
60
+ continue;
61
+ }
62
+ const cw = charWidth(ch);
63
+ w += cw;
64
+ prev = cw;
65
+ }
66
+ return w;
67
+ }
68
+
69
+ function stripAnsi(s) {
70
+ return s.replace(ANSI_RE, '');
71
+ }
72
+
73
+ /** 含 ANSI 的字符串的可见宽度 */
74
+ function visibleWidth(s) {
75
+ return strWidth(stripAnsi(s));
76
+ }
77
+
78
+ /* ============================ run 基础操作 ============================ */
79
+
80
+ /** 造一个 run 数组 */
81
+ function runs(text, style) {
82
+ if (text === undefined || text === null || text === '') return [];
83
+ return [{ t: String(text), s: style || '' }];
84
+ }
85
+
86
+ function runsWidth(rs) {
87
+ let w = 0;
88
+ for (const r of rs) w += strWidth(r.t);
89
+ return w;
90
+ }
91
+
92
+ /** 追加一段文本;相邻同样式会合并,减少输出体积 */
93
+ function push(rs, text, style) {
94
+ if (text === undefined || text === null || text === '') return rs;
95
+ const st = style || '';
96
+ const last = rs[rs.length - 1];
97
+ if (last && last.s === st) last.t += text;
98
+ else rs.push({ t: String(text), s: st });
99
+ return rs;
100
+ }
101
+
102
+ /** 截断/补齐到恰好 width 列 */
103
+ function runsFit(rs, width) {
104
+ if (width <= 0) return [];
105
+ const out = [];
106
+ let used = 0;
107
+ for (const r of rs) {
108
+ if (used >= width) break;
109
+ let acc = '';
110
+ for (const ch of r.t) {
111
+ const cw = charWidth(ch);
112
+ if (cw === 0) {
113
+ acc += ch;
114
+ continue;
115
+ }
116
+ if (used + cw > width) break;
117
+ acc += ch;
118
+ used += cw;
119
+ }
120
+ if (acc) out.push({ t: acc, s: r.s });
121
+ if (used >= width) break;
122
+ }
123
+ if (used < width) out.push({ t: ' '.repeat(width - used), s: '' });
124
+ return out;
125
+ }
126
+
127
+ /** 取 [start, start+len) 列区间 */
128
+ function runsSlice(rs, start, len) {
129
+ const out = [];
130
+ let col = 0;
131
+ let remaining = len;
132
+ for (const r of rs) {
133
+ if (remaining <= 0) break;
134
+ let acc = '';
135
+ for (const ch of r.t) {
136
+ if (remaining <= 0) break;
137
+ const cw = charWidth(ch);
138
+ const here = col;
139
+ col += cw;
140
+ if (cw === 0) {
141
+ // 零宽字符跟着前一个字符走
142
+ if (here > start && acc !== '') acc += ch;
143
+ continue;
144
+ }
145
+ if (here + cw <= start) continue;
146
+ if (here < start) continue; // 跨边界的中文,按整字丢弃
147
+ acc += ch;
148
+ remaining -= cw;
149
+ }
150
+ if (acc) out.push({ t: acc, s: r.s });
151
+ }
152
+ return out;
153
+ }
154
+
155
+ /** 把 overlay 盖在 base 的 x 列处 */
156
+ function overlayRow(base, over, x) {
157
+ const left = runsFit(runsSlice(base, 0, x), x);
158
+ const ow = runsWidth(over);
159
+ const rightStart = x + ow;
160
+ const baseW = runsWidth(base);
161
+ const right = rightStart < baseW ? runsSlice(base, rightStart, baseW - rightStart) : [];
162
+ return [...left, ...over, ...right];
163
+ }
164
+
165
+ /** run 数组 → 可输出的字符串,只在样式切换处发 SGR */
166
+ function toAnsi(rs) {
167
+ let out = '';
168
+ let cur = '';
169
+ for (const r of rs) {
170
+ const st = r.s || '';
171
+ if (st !== cur) {
172
+ out += st || RESET;
173
+ cur = st;
174
+ }
175
+ out += r.t;
176
+ }
177
+ if (cur) out += RESET;
178
+ return out;
179
+ }
180
+
181
+ /* ============================ 断词与换行 ============================ */
182
+
183
+ const WIDE_CHAR_RE = /[\u1100-\u115f\u2e80-\u303e\u3041-\u33ff\u3400-\u4dbf\u4e00-\u9fff\ua000-\ua4cf\ua960-\ua97f\uac00-\ud7a3\uf900-\ufaff\ufe10-\ufe19\ufe30-\ufe6f\uff00-\uff60\uffe0-\uffe6\u3000-\u303f]/;
184
+
185
+ /**
186
+ * 把一段文本切成可断行的最小单位:
187
+ * 中文/全角逐字,英文按单词,空白单独成 token。
188
+ */
189
+ function tokenize(text) {
190
+ const out = [];
191
+ let cur = '';
192
+ for (const ch of text) {
193
+ if (/\s/.test(ch)) {
194
+ if (cur) {
195
+ out.push(cur);
196
+ cur = '';
197
+ }
198
+ out.push(ch);
199
+ } else if (WIDE_CHAR_RE.test(ch) || charWidth(ch) === 2) {
200
+ if (cur) {
201
+ out.push(cur);
202
+ cur = '';
203
+ }
204
+ out.push(ch);
205
+ } else {
206
+ cur += ch;
207
+ }
208
+ }
209
+ if (cur) out.push(cur);
210
+ return out;
211
+ }
212
+
213
+ function trimTrailingSpace(rs) {
214
+ let removed = 0;
215
+ while (rs.length) {
216
+ const last = rs[rs.length - 1];
217
+ const m = /[ \t]+$/.exec(last.t);
218
+ if (!m) break;
219
+ last.t = last.t.slice(0, m.index);
220
+ removed += strWidth(m[0]);
221
+ if (last.t === '') rs.pop();
222
+ }
223
+ return removed;
224
+ }
225
+
226
+ /** 取走最后一个字符(折行时为了「标点不出现在行首」要把前一个字一起带下去) */
227
+ function popLastChar(rs) {
228
+ while (rs.length) {
229
+ const last = rs[rs.length - 1];
230
+ const chars = Array.from(last.t);
231
+ if (!chars.length) {
232
+ rs.pop();
233
+ continue;
234
+ }
235
+ const ch = chars.pop();
236
+ last.t = chars.join('');
237
+ const w = charWidth(ch);
238
+ if (last.t === '') rs.pop();
239
+ return { ch, w };
240
+ }
241
+ return null;
242
+ }
243
+
244
+ // 不该出现在行首的标点
245
+ const NO_LINE_START = '。,、;:?!)]}」』】》〉”’…‥·ー~%';
246
+
247
+ /**
248
+ * 按宽度折行。输入 run 数组,输出多行 run 数组。
249
+ * 支持 \n 强制换行。
250
+ */
251
+ function wrapRuns(rs, width) {
252
+ if (width < 1) width = 1;
253
+ const lines = [];
254
+ let cur = [];
255
+ let curW = 0;
256
+
257
+ const flush = () => {
258
+ trimTrailingSpace(cur);
259
+ lines.push(cur);
260
+ cur = [];
261
+ curW = 0;
262
+ };
263
+
264
+ for (const r of rs) {
265
+ const chunks = String(r.t).split('\n');
266
+ for (let ci = 0; ci < chunks.length; ci++) {
267
+ if (ci > 0) flush();
268
+ const chunk = chunks[ci];
269
+ if (!chunk) continue;
270
+ for (const tok of tokenize(chunk)) {
271
+ if (/^\s+$/.test(tok)) {
272
+ if (curW === 0) continue; // 行首空白丢弃
273
+ const w = strWidth(tok);
274
+ if (curW + w > width) {
275
+ flush();
276
+ continue;
277
+ }
278
+ push(cur, tok, r.s);
279
+ curW += w;
280
+ continue;
281
+ }
282
+ const tw = strWidth(tok);
283
+ if (curW > 0 && curW + tw > width) {
284
+ // 中文标点不放行首:把前一个字一起挪到下一行
285
+ if (tok.length === 1 && NO_LINE_START.includes(tok) && cur.length) {
286
+ const prev = popLastChar(cur);
287
+ if (prev) {
288
+ curW -= prev.w;
289
+ flush();
290
+ push(cur, prev.ch, r.s);
291
+ curW += prev.w;
292
+ } else {
293
+ flush();
294
+ }
295
+ } else {
296
+ flush();
297
+ }
298
+ }
299
+ if (tw <= width) {
300
+ push(cur, tok, r.s);
301
+ curW += tw;
302
+ continue;
303
+ }
304
+ // 超长单词:逐字符硬切
305
+ for (const ch of tok) {
306
+ const cw = charWidth(ch);
307
+ if (curW + cw > width) {
308
+ // 中文标点不放行首:把前一个字一起挪到下一行
309
+ if (NO_LINE_START.includes(ch) && cur.length) {
310
+ const prev = popLastChar(cur);
311
+ if (prev) {
312
+ curW -= prev.w;
313
+ flush();
314
+ push(cur, prev.ch, r.s);
315
+ curW += prev.w;
316
+ } else {
317
+ flush();
318
+ }
319
+ } else {
320
+ flush();
321
+ }
322
+ }
323
+ push(cur, ch, r.s);
324
+ curW += cw;
325
+ }
326
+ }
327
+ }
328
+ }
329
+ if (cur.length || lines.length === 0) {
330
+ trimTrailingSpace(cur);
331
+ lines.push(cur);
332
+ }
333
+ return lines;
334
+ }
335
+
336
+ module.exports = {
337
+ C,
338
+ RESET,
339
+ charWidth,
340
+ strWidth,
341
+ stripAnsi,
342
+ visibleWidth,
343
+ runs,
344
+ push,
345
+ runsWidth,
346
+ runsFit,
347
+ runsSlice,
348
+ overlayRow,
349
+ toAnsi,
350
+ tokenize,
351
+ wrapRuns,
352
+ };