@uncarved/jade 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +490 -29
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +36 -14
  14. package/plugins/approval/policy.ts +15 -10
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +42 -9
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +47 -12
  50. package/plugins/models/index.ts +27 -1
  51. package/plugins/models/provider.ts +24 -0
  52. package/plugins/models/saved.ts +1 -1
  53. package/plugins/modes/index.ts +13 -2
  54. package/plugins/print/index.ts +7 -4
  55. package/plugins/prompts/index.ts +0 -0
  56. package/plugins/renderers/index.ts +10 -5
  57. package/plugins/repeats/index.ts +8 -8
  58. package/plugins/schedule/index.ts +5 -1
  59. package/plugins/screenshot/browser.ts +10 -0
  60. package/plugins/sessions/index.ts +48 -2
  61. package/plugins/shell/index.ts +27 -28
  62. package/plugins/shell/plugin.json +1 -1
  63. package/plugins/shell/renderers.ts +9 -7
  64. package/plugins/shell/shell.ts +3 -1
  65. package/plugins/skills/index.ts +28 -19
  66. package/plugins/skills/project.ts +12 -7
  67. package/plugins/steer/index.ts +5 -4
  68. package/plugins/task/index.ts +80 -50
  69. package/plugins/task/plugin.json +6 -2
  70. package/plugins/task/style.ts +13 -14
  71. package/plugins/task/subagent.ts +67 -184
  72. package/plugins/todo/index.ts +39 -9
  73. package/plugins/tools/index.ts +9 -2
  74. package/plugins/tui/frontend.ts +139 -89
  75. package/plugins/tui/index.ts +2 -3
  76. package/plugins/tui/mentions.ts +11 -80
  77. package/plugins/tui/plugin.json +1 -1
  78. package/plugins/tui/screen.ts +55 -5
  79. package/plugins/tui/transcript.ts +17 -7
  80. package/plugins/tui/tui.ts +337 -101
  81. package/plugins/tui/vendor.ts +1 -1
  82. package/plugins/undo/checkpoint.ts +8 -4
  83. package/plugins/undo/index.ts +170 -31
  84. package/plugins/undo/plugin.json +1 -0
  85. package/plugins/undo/snapshot.ts +87 -14
  86. package/plugins/undo/style.ts +13 -14
  87. package/plugins/web/index.ts +7 -18
  88. package/plugins/web/plugin.json +1 -1
  89. package/plugins-dist/approval/index.js +363 -418
  90. package/plugins-dist/approval/policy.js +391 -508
  91. package/plugins-dist/approval/review.js +132 -158
  92. package/plugins-dist/approval/schema.js +50 -55
  93. package/plugins-dist/ask/index.js +115 -129
  94. package/plugins-dist/budget/index.js +27 -31
  95. package/plugins-dist/checks/checks.js +293 -329
  96. package/plugins-dist/checks/index.js +119 -139
  97. package/plugins-dist/checks/plugin.json +2 -1
  98. package/plugins-dist/checks/project.js +57 -65
  99. package/plugins-dist/commands/index.js +47 -29
  100. package/plugins-dist/compact/compact.js +233 -249
  101. package/plugins-dist/compact/index.js +98 -95
  102. package/plugins-dist/compact/style.js +9 -14
  103. package/plugins-dist/completions/index.js +30 -0
  104. package/plugins-dist/completions/plugin.json +9 -0
  105. package/plugins-dist/continue/index.js +40 -52
  106. package/plugins-dist/copy/index.js +40 -0
  107. package/plugins-dist/copy/plugin.json +9 -0
  108. package/plugins-dist/cost/index.js +98 -115
  109. package/plugins-dist/custom-commands/index.js +55 -72
  110. package/plugins-dist/editor/index.js +64 -72
  111. package/plugins-dist/fs/diff.js +111 -124
  112. package/plugins-dist/fs/fs.js +426 -469
  113. package/plugins-dist/fs/grep.js +262 -253
  114. package/plugins-dist/fs/index.js +20 -34
  115. package/plugins-dist/fs/plugin.json +1 -2
  116. package/plugins-dist/fs/renderers.js +100 -112
  117. package/plugins-dist/fs/style.js +11 -15
  118. package/plugins-dist/fs/sys.js +31 -43
  119. package/plugins-dist/hooks/index.js +107 -127
  120. package/plugins-dist/keys/index.js +27 -31
  121. package/plugins-dist/mcp/index.js +271 -199
  122. package/plugins-dist/mcp/install.js +123 -137
  123. package/plugins-dist/mcp/mcp.js +638 -577
  124. package/plugins-dist/mcp/plugin.json +3 -1
  125. package/plugins-dist/mcp/project.js +98 -115
  126. package/plugins-dist/memory/index.js +83 -88
  127. package/plugins-dist/memory/memory.js +111 -128
  128. package/plugins-dist/memory/style.js +9 -14
  129. package/plugins-dist/mentions/attach.js +91 -99
  130. package/plugins-dist/mentions/candidates.js +49 -0
  131. package/plugins-dist/mentions/index.js +71 -81
  132. package/plugins-dist/mentions/plugin.json +2 -1
  133. package/plugins-dist/mentions/style.js +9 -14
  134. package/plugins-dist/models/config.js +29 -43
  135. package/plugins-dist/models/configure.js +421 -474
  136. package/plugins-dist/models/context.js +14 -22
  137. package/plugins-dist/models/index.js +182 -189
  138. package/plugins-dist/models/provider.js +263 -307
  139. package/plugins-dist/models/saved.js +60 -72
  140. package/plugins-dist/modes/index.js +138 -165
  141. package/plugins-dist/plugins/index.js +34 -34
  142. package/plugins-dist/print/index.js +192 -175
  143. package/plugins-dist/prompts/index.js +86 -89
  144. package/plugins-dist/prompts/notes.js +31 -37
  145. package/plugins-dist/prompts/prompts.js +155 -193
  146. package/plugins-dist/prompts/results.js +40 -53
  147. package/plugins-dist/prompts/sys.js +39 -50
  148. package/plugins-dist/renderers/index.js +32 -32
  149. package/plugins-dist/repeats/index.js +131 -174
  150. package/plugins-dist/schedule/cron.js +49 -56
  151. package/plugins-dist/schedule/index.js +230 -247
  152. package/plugins-dist/schedule/store.js +93 -109
  153. package/plugins-dist/scratch/index.js +12 -18
  154. package/plugins-dist/screenshot/browser.js +136 -154
  155. package/plugins-dist/screenshot/index.js +37 -46
  156. package/plugins-dist/screenshot/sys.js +53 -65
  157. package/plugins-dist/screenshot/tool.js +98 -126
  158. package/plugins-dist/sessions/index.js +149 -117
  159. package/plugins-dist/shell/index.js +117 -135
  160. package/plugins-dist/shell/plugin.json +1 -1
  161. package/plugins-dist/shell/renderers.js +28 -33
  162. package/plugins-dist/shell/service.js +17 -23
  163. package/plugins-dist/shell/shell.js +307 -362
  164. package/plugins-dist/shell/sys.js +64 -79
  165. package/plugins-dist/skills/index.js +193 -211
  166. package/plugins-dist/skills/install.js +122 -149
  167. package/plugins-dist/skills/project.js +65 -73
  168. package/plugins-dist/skills/skills.js +135 -171
  169. package/plugins-dist/steer/index.js +108 -125
  170. package/plugins-dist/suggest/index.js +235 -259
  171. package/plugins-dist/task/index.js +130 -117
  172. package/plugins-dist/task/plugin.json +5 -2
  173. package/plugins-dist/task/style.js +9 -14
  174. package/plugins-dist/task/subagent.js +156 -279
  175. package/plugins-dist/todo/index.js +79 -63
  176. package/plugins-dist/tools/index.js +36 -42
  177. package/plugins-dist/tui/editor.js +145 -160
  178. package/plugins-dist/tui/frontend.js +639 -678
  179. package/plugins-dist/tui/history.js +34 -37
  180. package/plugins-dist/tui/index.js +16 -18
  181. package/plugins-dist/tui/markdown.js +203 -247
  182. package/plugins-dist/tui/mascot.js +266 -355
  183. package/plugins-dist/tui/mentions.js +67 -144
  184. package/plugins-dist/tui/plugin.json +1 -2
  185. package/plugins-dist/tui/render.js +35 -45
  186. package/plugins-dist/tui/screen.js +197 -180
  187. package/plugins-dist/tui/transcript.js +436 -468
  188. package/plugins-dist/tui/tui.js +1554 -1656
  189. package/plugins-dist/tui/vendor.js +1088 -1388
  190. package/plugins-dist/undo/checkpoint.js +97 -118
  191. package/plugins-dist/undo/index.js +281 -192
  192. package/plugins-dist/undo/plugin.json +3 -0
  193. package/plugins-dist/undo/snapshot.js +134 -99
  194. package/plugins-dist/undo/style.js +9 -14
  195. package/plugins-dist/update/index.js +26 -28
  196. package/plugins-dist/web/env.js +55 -68
  197. package/plugins-dist/web/index.js +80 -110
  198. package/plugins-dist/web/plugin.json +1 -2
  199. package/plugins-dist/web/renderers.js +4 -5
  200. package/plugins-dist/web/web.js +514 -593
  201. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  202. package/sdk/python/jade_plugin.py +9 -2
  203. package/src/api.ts +1 -1
  204. package/src/host.ts +39 -4
  205. package/src/remote.ts +6 -1
  206. package/src/session.ts +496 -36
@@ -1,49 +1,43 @@
1
- // jade/tools:给嵌套 Agent(子 Agent 之类)用的工具的注册表。
2
- //
3
- // 主会话里的工具对象是各插件私有的,另起一个 Agent 的插件拿不到。提供工具的插件在这里另登记一套实例,
4
- // 要用的插件(jade/task,或任何第三方插件)自己来取、自己筛选、自己把关:登记的一方不用知道谁在用。
5
1
  import { definePlugin } from "@uncarved/jade-api";
6
- /** 登记的工具,按登记顺序 */
7
2
  const registry = [];
8
- /** 同名的取最后登记的那个,when 返回 false 或出错的不给 */
9
3
  function available() {
10
- const latest = new Map();
11
- for (const r of registry) {
12
- latest.delete(r.tool.name);
13
- latest.set(r.tool.name, r);
4
+ const latest = new Map;
5
+ for (const r of registry) {
6
+ latest.delete(r.tool.name);
7
+ latest.set(r.tool.name, r);
8
+ }
9
+ return [...latest.values()].flatMap((r) => {
10
+ try {
11
+ return !r.when || r.when() ? [r.tool] : [];
12
+ } catch {
13
+ return [];
14
14
  }
15
- return [...latest.values()].flatMap((r) => {
16
- try {
17
- return !r.when || r.when() ? [r.tool] : [];
18
- }
19
- catch {
20
- return [];
21
- }
22
- });
15
+ });
23
16
  }
24
17
  export default definePlugin({
25
- process(ctx) {
26
- ctx.provide("tools@1", (caller) => ({
27
- register(tool, opts) {
28
- if (typeof tool?.name !== "string" || typeof tool.execute !== "function")
29
- throw new Error("tools.register: a tool needs a name and execute");
30
- // 同一个插件重复登记是正常的:换会话时新会话先登记,旧会话随后撤销它的那份;取最后登记的
31
- const taken = registry.find((r) => r.tool.name === tool.name && r.plugin !== caller.plugin);
32
- if (taken)
33
- throw new Error(`tools.register: ${tool.name} is already registered by ${taken.plugin}`);
34
- if (opts?.when !== undefined && typeof opts.when !== "function")
35
- throw new Error("tools.register: when must be a function");
36
- const entry = { tool, plugin: caller.plugin, ...(opts?.when ? { when: opts.when } : {}) };
37
- registry.push(entry);
38
- const revoke = () => {
39
- const i = registry.indexOf(entry);
40
- if (i !== -1)
41
- registry.splice(i, 1);
42
- };
43
- caller.onDispose(revoke);
44
- return revoke;
45
- },
46
- list: available,
47
- }));
48
- },
18
+ process(ctx) {
19
+ ctx.provide("tools@1", (caller) => ({
20
+ register(tool, opts) {
21
+ if (typeof tool?.name !== "string" || typeof tool.execute !== "function")
22
+ throw new Error("tools.register: a tool needs a name and execute");
23
+ if (opts?.when !== undefined && typeof opts.when !== "function")
24
+ throw new Error("tools.register: when must be a function");
25
+ const taken = registry.find((r) => r.tool.name === tool.name && r.plugin !== caller.plugin);
26
+ if (taken) {
27
+ ctx.report(`${tool.name} from ${caller.plugin} was skipped: ${taken.plugin} already registered it`);
28
+ return () => {};
29
+ }
30
+ const entry = { tool, plugin: caller.plugin, ...opts?.when ? { when: opts.when } : {} };
31
+ registry.push(entry);
32
+ const revoke = () => {
33
+ const i = registry.indexOf(entry);
34
+ if (i !== -1)
35
+ registry.splice(i, 1);
36
+ };
37
+ caller.onDispose(revoke);
38
+ return revoke;
39
+ },
40
+ list: available
41
+ }));
42
+ }
49
43
  });
@@ -1,184 +1,169 @@
1
- // 输入框的编辑操作和折行。都是纯函数,便于测试。
2
- // cursor 是 value 中的下标(UTF-16),操作按码点移动,不会把代理对拆开。
3
1
  import stringWidth from "string-width";
4
2
  export const empty = { value: "", cursor: 0 };
5
- /** 光标前一个码点的起始下标 */
6
3
  function prev(value, i) {
7
- if (i <= 0)
8
- return 0;
9
- const c = value.charCodeAt(i - 1);
10
- return c >= 0xdc00 && c <= 0xdfff && i >= 2 ? i - 2 : i - 1;
4
+ if (i <= 0)
5
+ return 0;
6
+ const c = value.charCodeAt(i - 1);
7
+ return c >= 56320 && c <= 57343 && i >= 2 ? i - 2 : i - 1;
11
8
  }
12
- /** 光标后一个码点的结束下标 */
13
9
  function next(value, i) {
14
- if (i >= value.length)
15
- return value.length;
16
- const c = value.charCodeAt(i);
17
- return c >= 0xd800 && c <= 0xdbff && i + 1 < value.length ? i + 2 : i + 1;
10
+ if (i >= value.length)
11
+ return value.length;
12
+ const c = value.charCodeAt(i);
13
+ return c >= 55296 && c <= 56319 && i + 1 < value.length ? i + 2 : i + 1;
18
14
  }
19
- const lineStart = (value, i) => value.lastIndexOf("\n", i - 1) + 1;
15
+ const lineStart = (value, i) => value.lastIndexOf(`
16
+ `, i - 1) + 1;
20
17
  const lineEnd = (value, i) => {
21
- const n = value.indexOf("\n", i);
22
- return n === -1 ? value.length : n;
18
+ const n = value.indexOf(`
19
+ `, i);
20
+ return n === -1 ? value.length : n;
23
21
  };
24
22
  export const edit = {
25
- insert: (s, text) => ({
26
- value: s.value.slice(0, s.cursor) + text + s.value.slice(s.cursor),
27
- cursor: s.cursor + text.length,
28
- }),
29
- backspace: (s) => {
30
- const p = prev(s.value, s.cursor);
31
- return { value: s.value.slice(0, p) + s.value.slice(s.cursor), cursor: p };
32
- },
33
- del: (s) => ({ value: s.value.slice(0, s.cursor) + s.value.slice(next(s.value, s.cursor)), cursor: s.cursor }),
34
- left: (s) => ({ ...s, cursor: prev(s.value, s.cursor) }),
35
- right: (s) => ({ ...s, cursor: next(s.value, s.cursor) }),
36
- home: (s) => ({ ...s, cursor: lineStart(s.value, s.cursor) }),
37
- end: (s) => ({ ...s, cursor: lineEnd(s.value, s.cursor) }),
38
- /** 光标移到下一个词的末尾 / 上一个词的开头,同 shell 的 Alt+F / Alt+B(macOS 终端里 Option+→ / Option+←) */
39
- wordRight: (s) => ({ ...s, cursor: s.cursor + /^\s*\S*/u.exec(s.value.slice(s.cursor))[0].length }),
40
- wordLeft: (s) => ({ ...s, cursor: s.value.slice(0, s.cursor).replace(/\S*\s*$/u, "").length }),
41
- /** 删除光标前的一个词(连同它和光标之间的空白),同 shell 的 Ctrl+W */
42
- deleteWord: (s) => {
43
- const before = s.value.slice(0, s.cursor);
44
- const kept = before.replace(/\S*\s*$/u, "");
45
- return { value: kept + s.value.slice(s.cursor), cursor: kept.length };
46
- },
47
- /** 删除光标到行首 */
48
- killBefore: (s) => {
49
- const start = lineStart(s.value, s.cursor);
50
- return { value: s.value.slice(0, start) + s.value.slice(s.cursor), cursor: start };
51
- },
52
- /** 删除光标到行尾 */
53
- killAfter: (s) => ({ value: s.value.slice(0, s.cursor) + s.value.slice(lineEnd(s.value, s.cursor)), cursor: s.cursor }),
54
- /** 光标所在行是第一行 / 最后一行(上下键在多行输入里移动光标,到边上才翻历史) */
55
- onFirstLine: (s) => !s.value.slice(0, s.cursor).includes("\n"),
56
- onLastLine: (s) => !s.value.slice(s.cursor).includes("\n"),
57
- up: (s) => {
58
- const start = lineStart(s.value, s.cursor);
59
- if (start === 0)
60
- return s;
61
- const col = s.cursor - start;
62
- const prevStart = lineStart(s.value, start - 1);
63
- return { ...s, cursor: Math.min(prevStart + col, start - 1) };
64
- },
65
- down: (s) => {
66
- const end = lineEnd(s.value, s.cursor);
67
- if (end === s.value.length)
68
- return s;
69
- const col = s.cursor - lineStart(s.value, s.cursor);
70
- return { ...s, cursor: Math.min(end + 1 + col, lineEnd(s.value, end + 1)) };
71
- },
23
+ insert: (s, text) => ({
24
+ value: s.value.slice(0, s.cursor) + text + s.value.slice(s.cursor),
25
+ cursor: s.cursor + text.length
26
+ }),
27
+ backspace: (s) => {
28
+ const p = prev(s.value, s.cursor);
29
+ return { value: s.value.slice(0, p) + s.value.slice(s.cursor), cursor: p };
30
+ },
31
+ del: (s) => ({ value: s.value.slice(0, s.cursor) + s.value.slice(next(s.value, s.cursor)), cursor: s.cursor }),
32
+ left: (s) => ({ ...s, cursor: prev(s.value, s.cursor) }),
33
+ right: (s) => ({ ...s, cursor: next(s.value, s.cursor) }),
34
+ home: (s) => ({ ...s, cursor: lineStart(s.value, s.cursor) }),
35
+ end: (s) => ({ ...s, cursor: lineEnd(s.value, s.cursor) }),
36
+ wordRight: (s) => ({ ...s, cursor: s.cursor + /^\s*\S*/u.exec(s.value.slice(s.cursor))[0].length }),
37
+ wordLeft: (s) => ({ ...s, cursor: s.value.slice(0, s.cursor).replace(/\S*\s*$/u, "").length }),
38
+ deleteWord: (s) => {
39
+ const before = s.value.slice(0, s.cursor);
40
+ const kept = before.replace(/\S*\s*$/u, "");
41
+ return { value: kept + s.value.slice(s.cursor), cursor: kept.length };
42
+ },
43
+ killBefore: (s) => {
44
+ const start = lineStart(s.value, s.cursor);
45
+ return { value: s.value.slice(0, start) + s.value.slice(s.cursor), cursor: start };
46
+ },
47
+ killAfter: (s) => ({ value: s.value.slice(0, s.cursor) + s.value.slice(lineEnd(s.value, s.cursor)), cursor: s.cursor }),
48
+ onFirstLine: (s) => !s.value.slice(0, s.cursor).includes(`
49
+ `),
50
+ onLastLine: (s) => !s.value.slice(s.cursor).includes(`
51
+ `),
52
+ up: (s) => {
53
+ const start = lineStart(s.value, s.cursor);
54
+ if (start === 0)
55
+ return s;
56
+ const col = s.cursor - start;
57
+ const prevStart = lineStart(s.value, start - 1);
58
+ return { ...s, cursor: Math.min(prevStart + col, start - 1) };
59
+ },
60
+ down: (s) => {
61
+ const end = lineEnd(s.value, s.cursor);
62
+ if (end === s.value.length)
63
+ return s;
64
+ const col = s.cursor - lineStart(s.value, s.cursor);
65
+ return { ...s, cursor: Math.min(end + 1 + col, lineEnd(s.value, end + 1)) };
66
+ }
72
67
  };
73
- /** 单个字符的显示宽度;每次按键都要把整段输入过一遍,stringWidth 较慢,ASCII 直接算,其余缓存 */
74
- const widths = new Map();
68
+ const widths = new Map;
75
69
  export function charWidth(ch) {
76
- const c = ch.charCodeAt(0);
77
- if (c >= 0x20 && c < 0x7f)
78
- return 1;
79
- let w = widths.get(ch);
80
- if (w === undefined)
81
- widths.set(ch, (w = stringWidth(ch)));
82
- return w;
70
+ const c = ch.charCodeAt(0);
71
+ if (c >= 32 && c < 127)
72
+ return 1;
73
+ let w = widths.get(ch);
74
+ if (w === undefined)
75
+ widths.set(ch, w = stringWidth(ch));
76
+ return w;
83
77
  }
84
- /** 光标可以停的每个位置(码点边界)在折行后的行和列,和 layout 的折法一致 */
85
78
  function stops(value, width) {
86
- width = Math.max(2, width);
87
- const out = [];
88
- let row = 0;
89
- let w = 0;
90
- let i = 0;
91
- // 正好停在满行末尾时算下一行开头(同 layout)
92
- const stop = (at) => out.push(w >= width ? { i: at, row: row + 1, col: 0 } : { i: at, row, col: w });
93
- for (const ch of value) {
94
- if (ch === "\n") {
95
- stop(i);
96
- row++;
97
- w = 0;
98
- }
99
- else {
100
- const cw = charWidth(ch);
101
- if (w + cw > width) {
102
- row++;
103
- w = 0;
104
- }
105
- stop(i);
106
- w += cw;
107
- }
108
- i += ch.length;
79
+ width = Math.max(2, width);
80
+ const out = [];
81
+ let row = 0;
82
+ let w = 0;
83
+ let i = 0;
84
+ const stop = (at) => out.push(w >= width ? { i: at, row: row + 1, col: 0 } : { i: at, row, col: w });
85
+ for (const ch of value) {
86
+ if (ch === `
87
+ `) {
88
+ stop(i);
89
+ row++;
90
+ w = 0;
91
+ } else {
92
+ const cw = charWidth(ch);
93
+ if (w + cw > width) {
94
+ row++;
95
+ w = 0;
96
+ }
97
+ stop(i);
98
+ w += cw;
109
99
  }
110
- stop(value.length);
111
- return out;
100
+ i += ch.length;
101
+ }
102
+ stop(value.length);
103
+ return out;
112
104
  }
113
- /** 折行后第 row 行里最接近第 col 列(不超过它)的光标位置;没有这一行时返回 undefined */
114
105
  export function offsetAt(value, width, row, col) {
115
- const inRow = stops(value, width).filter((s) => s.row === row);
116
- if (inRow.length === 0)
117
- return undefined;
118
- return (inRow.findLast((s) => s.col <= col) ?? inRow[0]).i;
106
+ const inRow = stops(value, width).filter((s) => s.row === row);
107
+ if (inRow.length === 0)
108
+ return;
109
+ return (inRow.findLast((s) => s.col <= col) ?? inRow[0]).i;
119
110
  }
120
- /**
121
- * 按折行后的屏幕行上下移动光标,保持显示列(中文占两列)。已经在第一行 / 最后一行时返回 undefined,
122
- * 由调用方去翻历史
123
- */
124
111
  export function moveRow(s, width, by) {
125
- const all = stops(s.value, width);
126
- const here = all.find((x) => x.i === s.cursor) ?? all.at(-1);
127
- const target = here.row + by;
128
- if (target < 0 || target > all.at(-1).row)
129
- return undefined;
130
- const cursor = offsetAt(s.value, width, target, here.col);
131
- return cursor === undefined ? undefined : { ...s, cursor };
112
+ const all = stops(s.value, width);
113
+ const here = all.find((x) => x.i === s.cursor) ?? all.at(-1);
114
+ const target = here.row + by;
115
+ if (target < 0 || target > all.at(-1).row)
116
+ return;
117
+ const cursor = offsetAt(s.value, width, target, here.col);
118
+ return cursor === undefined ? undefined : { ...s, cursor };
132
119
  }
133
- /** 按显示宽度折行(按字符折,不按词),并算出光标位置。width 至少为 2 */
134
120
  export function layout(value, cursor, width) {
135
- width = Math.max(2, width);
136
- const rows = [];
137
- const starts = [0];
138
- let row = 0;
139
- let col = 0;
140
- let current = "";
141
- let currentWidth = 0;
142
- let i = 0;
143
- const place = () => {
144
- row = rows.length;
145
- col = currentWidth;
146
- };
147
- for (const ch of value) {
121
+ width = Math.max(2, width);
122
+ const rows = [];
123
+ const starts = [0];
124
+ let row = 0;
125
+ let col = 0;
126
+ let current = "";
127
+ let currentWidth = 0;
128
+ let i = 0;
129
+ const place = () => {
130
+ row = rows.length;
131
+ col = currentWidth;
132
+ };
133
+ for (const ch of value) {
134
+ if (i === cursor)
135
+ place();
136
+ if (ch === `
137
+ `) {
138
+ rows.push(current);
139
+ starts.push(i + 1);
140
+ current = "";
141
+ currentWidth = 0;
142
+ } else {
143
+ const w = charWidth(ch);
144
+ if (currentWidth + w > width) {
145
+ rows.push(current);
146
+ starts.push(i);
147
+ current = "";
148
+ currentWidth = 0;
148
149
  if (i === cursor)
149
- place();
150
- if (ch === "\n") {
151
- rows.push(current);
152
- starts.push(i + 1);
153
- current = "";
154
- currentWidth = 0;
155
- }
156
- else {
157
- const w = charWidth(ch);
158
- if (currentWidth + w > width) {
159
- rows.push(current);
160
- starts.push(i);
161
- current = "";
162
- currentWidth = 0;
163
- if (i === cursor)
164
- place();
165
- }
166
- current += ch;
167
- currentWidth += w;
168
- }
169
- i += ch.length;
150
+ place();
151
+ }
152
+ current += ch;
153
+ currentWidth += w;
170
154
  }
171
- if (cursor >= value.length)
172
- place();
173
- rows.push(current);
174
- // 光标正好停在满行末尾时放到下一行开头
175
- if (col >= width) {
176
- row++;
177
- col = 0;
178
- if (row >= rows.length) {
179
- rows.push("");
180
- starts.push(value.length);
181
- }
155
+ i += ch.length;
156
+ }
157
+ if (cursor >= value.length)
158
+ place();
159
+ rows.push(current);
160
+ if (col >= width) {
161
+ row++;
162
+ col = 0;
163
+ if (row >= rows.length) {
164
+ rows.push("");
165
+ starts.push(value.length);
182
166
  }
183
- return { rows, starts, row, col };
167
+ }
168
+ return { rows, starts, row, col };
184
169
  }