mocode-ai 1.8.4 → 1.8.5
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/dist/repl/message-format.js +93 -3
- package/dist/ui/markdown.js +123 -36
- package/dist/ui/render.js +110 -4
- package/package.json +1 -1
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
1
2
|
import { ui } from '../ui/theme.js';
|
|
3
|
+
import { renderFileChange, renderMultiFileChange } from '../ui/diff.js';
|
|
4
|
+
import { isToolErrorOutput } from '../tools/result.js';
|
|
5
|
+
import { jailResolve } from '../sandbox/index.js';
|
|
2
6
|
import * as layout from '../ui/layout.js';
|
|
3
7
|
import * as batch from '../ui/batch.js';
|
|
4
8
|
import { displayWidth, padEndDisplay, summarizeToolCall, summarizeToolResult } from '../ui/render.js';
|
|
@@ -25,6 +29,84 @@ export function formatUserMessage(lines, trailingBlank = true) {
|
|
|
25
29
|
})
|
|
26
30
|
.join('\n') + (trailingBlank ? '\n\n' : '\n'));
|
|
27
31
|
}
|
|
32
|
+
const toLf = (s) => s.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
33
|
+
/** 回放用的行号定位:在**当前**文件里找 text 的唯一命中行(edit 已落盘,找新文本);找不到/不唯一退回 1。 */
|
|
34
|
+
function locateLine(filePath, text) {
|
|
35
|
+
if (!text)
|
|
36
|
+
return 1;
|
|
37
|
+
try {
|
|
38
|
+
const data = toLf(readFileSync(jailResolve(filePath), 'utf8'));
|
|
39
|
+
const needle = toLf(text);
|
|
40
|
+
const first = data.indexOf(needle);
|
|
41
|
+
if (first < 0 || data.indexOf(needle, first + needle.length) !== -1)
|
|
42
|
+
return 1;
|
|
43
|
+
return data.slice(0, first).split('\n').length;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 由历史里的工具参数重建 edit_file/write_file 的 diff 块(回放时自动展开用)。
|
|
51
|
+
* history 只存参数不存执行前快照,所以是尽力还原:
|
|
52
|
+
* - write_file 无旧内容 → 按 Create 展示全部为新增行;
|
|
53
|
+
* - edit_file 行号在当前文件里按新文本定位,定位不到退回相对行号 1;
|
|
54
|
+
* - 行号区间模式(无 old_string)、失败结果、参数非法 → 返回 null,退回单行摘要。
|
|
55
|
+
*/
|
|
56
|
+
export function buildReplayDiff(name, rawArgs, output) {
|
|
57
|
+
if (isToolErrorOutput(output))
|
|
58
|
+
return null;
|
|
59
|
+
let parsed;
|
|
60
|
+
try {
|
|
61
|
+
const v = JSON.parse(rawArgs);
|
|
62
|
+
if (!v || typeof v !== 'object')
|
|
63
|
+
return null;
|
|
64
|
+
parsed = v;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const filePath = String(parsed.path ?? '');
|
|
70
|
+
if (!filePath)
|
|
71
|
+
return null;
|
|
72
|
+
if (name === 'write_file') {
|
|
73
|
+
if (typeof parsed.content !== 'string')
|
|
74
|
+
return null;
|
|
75
|
+
return renderFileChange({ path: filePath, kind: 'write', oldStr: null, newStr: parsed.content, startLine: 1 });
|
|
76
|
+
}
|
|
77
|
+
if (name !== 'edit_file')
|
|
78
|
+
return null;
|
|
79
|
+
if (Array.isArray(parsed.replacements) && parsed.replacements.length > 0) {
|
|
80
|
+
const hunks = [];
|
|
81
|
+
for (const item of parsed.replacements) {
|
|
82
|
+
const rec = item;
|
|
83
|
+
if (!rec || typeof rec.old_string !== 'string' || typeof rec.new_string !== 'string')
|
|
84
|
+
return null;
|
|
85
|
+
hunks.push({
|
|
86
|
+
oldStr: rec.old_string,
|
|
87
|
+
newStr: rec.new_string,
|
|
88
|
+
startLine: locateLine(filePath, rec.new_string),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return (renderMultiFileChange({ path: filePath, hunks }) ??
|
|
92
|
+
renderFileChange({
|
|
93
|
+
path: filePath,
|
|
94
|
+
kind: 'edit',
|
|
95
|
+
oldStr: hunks.map((h) => h.oldStr).join('\n'),
|
|
96
|
+
newStr: hunks.map((h) => h.newStr).join('\n'),
|
|
97
|
+
startLine: hunks[0].startLine,
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
if (typeof parsed.old_string !== 'string' || typeof parsed.new_string !== 'string')
|
|
101
|
+
return null;
|
|
102
|
+
return renderFileChange({
|
|
103
|
+
path: filePath,
|
|
104
|
+
kind: 'edit',
|
|
105
|
+
oldStr: parsed.old_string,
|
|
106
|
+
newStr: parsed.new_string,
|
|
107
|
+
startLine: locateLine(filePath, parsed.new_string),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
28
110
|
/** 把任意消息 content 拍平成字符串(OpenAI 可能 string / null / 多模态数组)。 */
|
|
29
111
|
export function textOf(c) {
|
|
30
112
|
if (typeof c === 'string')
|
|
@@ -58,6 +140,7 @@ export function queryHistoryFromMessages(messages) {
|
|
|
58
140
|
*/
|
|
59
141
|
export function renderHistory(history) {
|
|
60
142
|
const idToEntry = new Map();
|
|
143
|
+
const idToArgs = new Map(); // tool_call id → 原始参数 JSON,供重建 mutation diff
|
|
61
144
|
// 普通工具可跨 assistant 步聚合;mutation 各自占一个 group,并切断前后普通工具。
|
|
62
145
|
let pendingBatches = [];
|
|
63
146
|
let normalBatch = null;
|
|
@@ -95,6 +178,9 @@ export function renderHistory(history) {
|
|
|
95
178
|
}
|
|
96
179
|
if (m.role === 'user') {
|
|
97
180
|
flushBatch(); // 上一轮 batch(若有)收尾
|
|
181
|
+
// 上一条 agent 正文 → 本条用户气泡:contentWriteMdOnce 会裁掉 markdown 尾部空行,
|
|
182
|
+
// 不归一的话气泡紧贴正文(实时对话由 REPL 循环顶部的 normalizeInputBoundary 保证恰好 1 条空行)。
|
|
183
|
+
layout.normalizeInputBoundary();
|
|
98
184
|
const lines = textOf(m.content).split('\n');
|
|
99
185
|
layout.contentWrite(formatUserMessage(lines));
|
|
100
186
|
const atts = messageAttachments.get(idx);
|
|
@@ -151,8 +237,10 @@ export function renderHistory(history) {
|
|
|
151
237
|
}
|
|
152
238
|
normalBatch.push(entry);
|
|
153
239
|
}
|
|
154
|
-
if (tc?.id)
|
|
240
|
+
if (tc?.id) {
|
|
155
241
|
idToEntry.set(tc.id, entry);
|
|
242
|
+
idToArgs.set(tc.id, args);
|
|
243
|
+
}
|
|
156
244
|
}
|
|
157
245
|
continue; // 跳过后续 tool 消息处理循环(由下一分支填 result)
|
|
158
246
|
}
|
|
@@ -165,9 +253,11 @@ export function renderHistory(history) {
|
|
|
165
253
|
const target = idToEntry.get(id);
|
|
166
254
|
const name = target?.name ?? '';
|
|
167
255
|
const output = textOf(m.content);
|
|
168
|
-
const preview = summarizeToolResult(name, output);
|
|
169
256
|
if (target) {
|
|
170
|
-
|
|
257
|
+
// mutation 成功:重建 diff 块,回放时自动展开(与实时一致);否则退回单行摘要。
|
|
258
|
+
const diff = batch.isMutationToolName(name) ? buildReplayDiff(name, idToArgs.get(id) ?? '', output) : null;
|
|
259
|
+
target.diffBlock = diff;
|
|
260
|
+
target.resultSummary = diff ? '' : summarizeToolResult(name, output);
|
|
171
261
|
target.fullOutput = output;
|
|
172
262
|
}
|
|
173
263
|
// 不直接写屏——等 flushBatch 时出单行摘要
|
package/dist/ui/markdown.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// clipAnsiLine 兜底,绝不溢出。颜色一律嵌入式 ANSI(ui.*)写进字符串。
|
|
14
14
|
import { getCliHighlight } from './cli-highlight-lazy.js';
|
|
15
15
|
import { ui, getThemeVersion } from './theme.js';
|
|
16
|
-
import {
|
|
16
|
+
import { graphemeAt, displayWidth, ansiDisplayWidth, stripAnsi } from './render.js';
|
|
17
17
|
const RESET = '\x1b[0m';
|
|
18
18
|
// theme 无 italic/strike,自备(TTY 感知:!isTTY 退化为空串,同 ui.* 契约)。
|
|
19
19
|
const ITALIC = ui.isTTY ? '\x1b[3m' : '';
|
|
@@ -34,13 +34,16 @@ function clipAnsiLine(str, width) {
|
|
|
34
34
|
out += parts[i];
|
|
35
35
|
continue;
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const seg = parts[i];
|
|
38
|
+
for (let gi = 0; gi < seg.length;) {
|
|
39
|
+
const g = graphemeAt(seg, gi);
|
|
40
|
+
const ch = seg.slice(gi, g.end);
|
|
41
|
+
gi = g.end;
|
|
42
|
+
if (g.width > 0 && w + g.width > width) {
|
|
40
43
|
return out + RESET;
|
|
41
44
|
}
|
|
42
|
-
if (
|
|
43
|
-
w +=
|
|
45
|
+
if (g.width > 0)
|
|
46
|
+
w += g.width;
|
|
44
47
|
out += ch;
|
|
45
48
|
}
|
|
46
49
|
}
|
|
@@ -262,8 +265,11 @@ function wrapAnsiString(str, width) {
|
|
|
262
265
|
activeSgr += tok;
|
|
263
266
|
continue;
|
|
264
267
|
}
|
|
265
|
-
for (
|
|
266
|
-
const
|
|
268
|
+
for (let gi = 0; gi < tok.length;) {
|
|
269
|
+
const g = graphemeAt(tok, gi);
|
|
270
|
+
const ch = tok.slice(gi, g.end);
|
|
271
|
+
gi = g.end;
|
|
272
|
+
const cw = g.width;
|
|
267
273
|
if (cw <= 0) {
|
|
268
274
|
cur += ch;
|
|
269
275
|
continue;
|
|
@@ -311,9 +317,41 @@ function parseTableRow(line) {
|
|
|
311
317
|
let s = line.trim();
|
|
312
318
|
if (s.startsWith('|'))
|
|
313
319
|
s = s.slice(1);
|
|
314
|
-
|
|
320
|
+
// 末尾 | 只有在未被转义时才是边框(`a \|` 结尾的 \| 是单元格内容)
|
|
321
|
+
if (s.endsWith('|') && !s.endsWith('\\|'))
|
|
315
322
|
s = s.slice(0, -1);
|
|
316
|
-
|
|
323
|
+
// 按「未转义、且不在行内代码里」的 | 切分;\| 还原成 |(GFM 规则:转义管道符在代码里也生效)
|
|
324
|
+
const cells = [];
|
|
325
|
+
let cur = '';
|
|
326
|
+
let ticks = 0; // 当前打开的行内代码反引号数(0 = 不在代码里)
|
|
327
|
+
for (let k = 0; k < s.length; k++) {
|
|
328
|
+
const ch = s[k];
|
|
329
|
+
if (ch === '\\' && s[k + 1] === '|') {
|
|
330
|
+
cur += '|';
|
|
331
|
+
k++;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (ch === '`') {
|
|
335
|
+
let run = 1;
|
|
336
|
+
while (s[k + run] === '`')
|
|
337
|
+
run++;
|
|
338
|
+
if (ticks === 0)
|
|
339
|
+
ticks = run;
|
|
340
|
+
else if (ticks === run)
|
|
341
|
+
ticks = 0;
|
|
342
|
+
cur += '`'.repeat(run);
|
|
343
|
+
k += run - 1;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
if (ch === '|' && ticks === 0) {
|
|
347
|
+
cells.push(cur.trim());
|
|
348
|
+
cur = '';
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
cur += ch;
|
|
352
|
+
}
|
|
353
|
+
cells.push(cur.trim());
|
|
354
|
+
return cells;
|
|
317
355
|
}
|
|
318
356
|
/** 分隔行每 cell :?-+:? → 对齐(left 默认)。 */
|
|
319
357
|
function parseAlign(sep) {
|
|
@@ -324,6 +362,33 @@ function parseAlign(sep) {
|
|
|
324
362
|
return l && r ? 'center' : r ? 'right' : 'left';
|
|
325
363
|
});
|
|
326
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* 宽容匹配 GFM 表格:src[i] 为表头、src[i+1] 为分隔行。
|
|
367
|
+
* 分隔行本身带 |、每格只含 `-` 与 `:`、至少一格含 `-` 即可(模型常写出列数与表头不一致的分隔行、
|
|
368
|
+
* 漏写竖线的粘连格 `--:----:`、单列表格);列数以表头为准,缺的列按左对齐。
|
|
369
|
+
* 不带 | 的 `---`(hr / setext)与 `~ | ==` 等非法写法返回 null,仍走普通文本。
|
|
370
|
+
* 数据行读到空行或不含 | 的行为止。next 为表格后第一行的下标。
|
|
371
|
+
*/
|
|
372
|
+
function matchTable(src, i) {
|
|
373
|
+
if (i + 1 >= src.length || !src[i].includes('|') || !src[i + 1].includes('|'))
|
|
374
|
+
return null;
|
|
375
|
+
const header = parseTableRow(src[i]);
|
|
376
|
+
const sepCells = parseTableRow(src[i + 1]);
|
|
377
|
+
if (header.length < 1)
|
|
378
|
+
return null;
|
|
379
|
+
if (!sepCells.some((c) => c.includes('-')) || !sepCells.every((c) => /^[:-]+$/.test(c)))
|
|
380
|
+
return null;
|
|
381
|
+
const rows = [];
|
|
382
|
+
let j = i + 2;
|
|
383
|
+
while (j < src.length) {
|
|
384
|
+
const r = src[j];
|
|
385
|
+
if (r.trim() === '' || !r.includes('|'))
|
|
386
|
+
break;
|
|
387
|
+
rows.push(parseTableRow(r));
|
|
388
|
+
j++;
|
|
389
|
+
}
|
|
390
|
+
return { header, aligns: parseAlign(sepCells), rows, next: j };
|
|
391
|
+
}
|
|
327
392
|
/** ANSI 感知按可见宽度 pad(左/右/中),补空格;不截断(超宽原样返回,由 clipAnsiLine 兜底)。 */
|
|
328
393
|
function padAnsi(str, width, align) {
|
|
329
394
|
const w = ansiDisplayWidth(str);
|
|
@@ -576,11 +641,33 @@ function renderMarkdownImpl(text, cols) {
|
|
|
576
641
|
flushPara();
|
|
577
642
|
const prefix = ` ${ui.dim}│${ui.reset} `;
|
|
578
643
|
const cont = ' '.repeat(ansiDisplayWidth(prefix));
|
|
579
|
-
|
|
580
|
-
|
|
644
|
+
// 收齐连续的引用行,去掉 > 前缀后再识别表格(否则引用块里的表格会逐行显示成竖线原文)
|
|
645
|
+
const inner = [];
|
|
646
|
+
let j = i;
|
|
647
|
+
while (j < src.length) {
|
|
648
|
+
const m = src[j].match(/^>\s?(.*)$/);
|
|
649
|
+
if (!m)
|
|
650
|
+
break;
|
|
651
|
+
inner.push(m[1]);
|
|
652
|
+
j++;
|
|
653
|
+
}
|
|
654
|
+
let k = 0;
|
|
655
|
+
while (k < inner.length) {
|
|
656
|
+
const t = matchTable(inner, k);
|
|
657
|
+
if (t) {
|
|
658
|
+
const tw = Math.max(1, cols - ansiDisplayWidth(prefix));
|
|
659
|
+
for (const l of renderTable(t.header, t.aligns, t.rows, tw))
|
|
660
|
+
out.push(clipAnsiLine(prefix + l, cols));
|
|
661
|
+
k = t.next;
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
for (const l of wrapWithPrefix(renderInline(inner[k]), prefix, cont, cols)) {
|
|
665
|
+
out.push(l);
|
|
666
|
+
}
|
|
667
|
+
k++;
|
|
581
668
|
}
|
|
582
669
|
prevBlank = false;
|
|
583
|
-
i
|
|
670
|
+
i = j;
|
|
584
671
|
continue;
|
|
585
672
|
}
|
|
586
673
|
// 无序列表 - * +
|
|
@@ -612,30 +699,30 @@ function renderMarkdownImpl(text, cols) {
|
|
|
612
699
|
i++;
|
|
613
700
|
continue;
|
|
614
701
|
}
|
|
615
|
-
// GFM 表格:表头
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
prevBlank = true;
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
702
|
+
// GFM 表格:表头 + 紧跟分隔行 → 收齐整块渲染(宽容匹配规则见 matchTable / renderTable)
|
|
703
|
+
const tbl = matchTable(src, i);
|
|
704
|
+
if (tbl) {
|
|
705
|
+
flushPara();
|
|
706
|
+
for (const l of renderTable(tbl.header, tbl.aligns, tbl.rows, cols))
|
|
707
|
+
out.push(l);
|
|
708
|
+
out.push('');
|
|
709
|
+
prevBlank = true;
|
|
710
|
+
i = tbl.next;
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
// 没有合法分隔行的竖线行(模型漏写分隔行 / 写法非法):保持逐行显示,
|
|
714
|
+
// 不能并进段落——段落会把多行 join 成一行,竖线文字糊成一团。
|
|
715
|
+
if (/^\s*\|/.test(line)) {
|
|
716
|
+
flushPara();
|
|
717
|
+
while (i < src.length && /^\s*\|/.test(src[i]) && !(i > 0 && matchTable(src, i))) {
|
|
718
|
+
const segs = renderInline(src[i].trim());
|
|
719
|
+
for (const l of wrapAnsiString(segsToAnsi(segs), cols))
|
|
720
|
+
out.push(clipAnsiLine(l, cols));
|
|
721
|
+
i++;
|
|
638
722
|
}
|
|
723
|
+
out.push('');
|
|
724
|
+
prevBlank = true;
|
|
725
|
+
continue;
|
|
639
726
|
}
|
|
640
727
|
// 段落:累积(连续非空非特殊行 join ' ' 后渲染)
|
|
641
728
|
para.push(line);
|
package/dist/ui/render.js
CHANGED
|
@@ -60,14 +60,120 @@ export function charWidth(cp) {
|
|
|
60
60
|
return 2; // 表情
|
|
61
61
|
if (cp >= 0x20000 && cp <= 0x2fffd)
|
|
62
62
|
return 2; // CJK 扩展 B-F
|
|
63
|
-
if (cp
|
|
64
|
-
return 2; // ⚡
|
|
63
|
+
if (isWideBmpEmoji(cp))
|
|
64
|
+
return 2; // ⚡ ✅ ❌ ⭐ 等:终端按 emoji 2 格渲染,算 1 会与 chip 重叠产生残影、表格列错位
|
|
65
65
|
return 1;
|
|
66
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* BMP 内 East Asian Width = W 的 emoji(Emoji_Presentation=Yes,无需 VS16 就按 2 格画)。
|
|
69
|
+
* ✔ (U+2714) / ⚠ (U+26A0) 等文本呈现符号默认是窄的,不在此表。
|
|
70
|
+
* 补充平面(U+1F300+)已由上面的区间覆盖。
|
|
71
|
+
*/
|
|
72
|
+
const WIDE_BMP_EMOJI = [
|
|
73
|
+
[0x231a, 0x231b],
|
|
74
|
+
[0x23e9, 0x23ec],
|
|
75
|
+
[0x23f0, 0x23f0],
|
|
76
|
+
[0x23f3, 0x23f3],
|
|
77
|
+
[0x25fd, 0x25fe],
|
|
78
|
+
[0x2614, 0x2615],
|
|
79
|
+
[0x2648, 0x2653],
|
|
80
|
+
[0x267f, 0x267f],
|
|
81
|
+
[0x2693, 0x2693],
|
|
82
|
+
[0x26a1, 0x26a1],
|
|
83
|
+
[0x26aa, 0x26ab],
|
|
84
|
+
[0x26bd, 0x26be],
|
|
85
|
+
[0x26c4, 0x26c5],
|
|
86
|
+
[0x26ce, 0x26ce],
|
|
87
|
+
[0x26d4, 0x26d4],
|
|
88
|
+
[0x26ea, 0x26ea],
|
|
89
|
+
[0x26f2, 0x26f3],
|
|
90
|
+
[0x26f5, 0x26f5],
|
|
91
|
+
[0x26fa, 0x26fa],
|
|
92
|
+
[0x26fd, 0x26fd],
|
|
93
|
+
[0x2705, 0x2705],
|
|
94
|
+
[0x270a, 0x270b],
|
|
95
|
+
[0x2728, 0x2728],
|
|
96
|
+
[0x274c, 0x274c],
|
|
97
|
+
[0x274e, 0x274e],
|
|
98
|
+
[0x2753, 0x2755],
|
|
99
|
+
[0x2757, 0x2757],
|
|
100
|
+
[0x2795, 0x2797],
|
|
101
|
+
[0x27b0, 0x27b0],
|
|
102
|
+
[0x27bf, 0x27bf],
|
|
103
|
+
[0x2b1b, 0x2b1c],
|
|
104
|
+
[0x2b50, 0x2b50],
|
|
105
|
+
[0x2b55, 0x2b55],
|
|
106
|
+
];
|
|
107
|
+
function isWideBmpEmoji(cp) {
|
|
108
|
+
if (cp < 0x231a || cp > 0x2b55)
|
|
109
|
+
return false;
|
|
110
|
+
for (const [lo, hi] of WIDE_BMP_EMOJI) {
|
|
111
|
+
if (cp >= lo && cp <= hi)
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 读 str 中 UTF-16 下标 i 处的一个「显示簇」,返回它占的终端列数与簇末下标(不含)。
|
|
118
|
+
* 终端会把下面几类码点序列合成一个字形,只画 emoji 的 2 格;逐码点累加宽度会算宽,表格边框就凹进去:
|
|
119
|
+
* - 区域旗帜:两个 Regional Indicator(🇨🇳)→ 2 格,而不是 2+2
|
|
120
|
+
* - ZWJ 序列:👨💻 / 👩👧 / ❤️🔥 → 以首个字符为准,ZWJ 后并入的 emoji 不再计宽
|
|
121
|
+
* - 肤色修饰符:👍🏽 → 并入前一字符
|
|
122
|
+
* - 后随 FE0F(VS16):⚠️ ✔️ ❤️ 默认文本呈现的窄符号切成 emoji 呈现 → 2 格;keycap(1️⃣)同为 2 格
|
|
123
|
+
* 组合符 / 控制符仍按 charWidth 算 0 宽。ZWJ 只与 U+2190 以上的符号 / emoji 合并,
|
|
124
|
+
* 避免把印地语等文字里的 ZWJ 误当 emoji 连接。
|
|
125
|
+
*/
|
|
126
|
+
export function graphemeAt(str, i) {
|
|
127
|
+
const cp = str.codePointAt(i) ?? 0;
|
|
128
|
+
let end = i + (cp > 0xffff ? 2 : 1);
|
|
129
|
+
if (cp >= 0x1f1e6 && cp <= 0x1f1ff) {
|
|
130
|
+
const next = str.codePointAt(end);
|
|
131
|
+
if (next !== undefined && next >= 0x1f1e6 && next <= 0x1f1ff)
|
|
132
|
+
end += 2;
|
|
133
|
+
return { width: 2, end };
|
|
134
|
+
}
|
|
135
|
+
let width = charWidth(cp);
|
|
136
|
+
for (;;) {
|
|
137
|
+
const n = str.codePointAt(end);
|
|
138
|
+
if (n === undefined)
|
|
139
|
+
break;
|
|
140
|
+
if (n === 0xfe0f) {
|
|
141
|
+
if (width === 1 && cp >= 0x80)
|
|
142
|
+
width = 2;
|
|
143
|
+
end += 1;
|
|
144
|
+
}
|
|
145
|
+
else if (n === 0xfe0e) {
|
|
146
|
+
end += 1;
|
|
147
|
+
}
|
|
148
|
+
else if (n === 0x20e3) {
|
|
149
|
+
if (width > 0)
|
|
150
|
+
width = 2;
|
|
151
|
+
end += 1;
|
|
152
|
+
}
|
|
153
|
+
else if (n >= 0x1f3fb && n <= 0x1f3ff && width > 0) {
|
|
154
|
+
end += 2;
|
|
155
|
+
}
|
|
156
|
+
else if (n === 0x200d) {
|
|
157
|
+
const m = str.codePointAt(end + 1);
|
|
158
|
+
if (m !== undefined && cp >= 0x2190 && m >= 0x2190)
|
|
159
|
+
end += 1 + (m > 0xffff ? 2 : 1);
|
|
160
|
+
else
|
|
161
|
+
end += 1;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { width, end };
|
|
168
|
+
}
|
|
67
169
|
export function displayWidth(str) {
|
|
68
170
|
let w = 0;
|
|
69
|
-
|
|
70
|
-
|
|
171
|
+
let i = 0;
|
|
172
|
+
while (i < str.length) {
|
|
173
|
+
const g = graphemeAt(str, i);
|
|
174
|
+
w += g.width;
|
|
175
|
+
i = g.end;
|
|
176
|
+
}
|
|
71
177
|
return w;
|
|
72
178
|
}
|
|
73
179
|
/**
|