advisers 0.2.2 → 0.3.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.
Files changed (70) hide show
  1. package/LICENSE +0 -0
  2. package/NOTICE +0 -0
  3. package/README-zh.md +11 -7
  4. package/README.md +16 -8
  5. package/THIRD-PARTY-NOTICES.md +1 -1
  6. package/bin/advisers.js +6347 -542
  7. package/index.js +1 -1
  8. package/lib/agent.js +529 -80
  9. package/lib/attachments.js +0 -0
  10. package/lib/audit-anchor.js +683 -0
  11. package/lib/backup.js +1345 -0
  12. package/lib/capture.js +0 -0
  13. package/lib/context-window.js +0 -0
  14. package/lib/digest.js +0 -0
  15. package/lib/doc-review.js +0 -0
  16. package/lib/egress.js +0 -0
  17. package/lib/error-codes.js +1373 -0
  18. package/lib/evidence.js +352 -43
  19. package/lib/fs-safe.js +347 -0
  20. package/lib/ical.js +0 -0
  21. package/lib/imap.js +6 -2
  22. package/lib/importers.js +0 -0
  23. package/lib/learning.js +0 -0
  24. package/lib/ledger-read.js +608 -0
  25. package/lib/mcp-server.js +331 -50
  26. package/lib/model.js +73 -1
  27. package/lib/node-warning-filter.js +15 -0
  28. package/lib/notify.js +13 -3
  29. package/lib/nudge.js +0 -0
  30. package/lib/ocr.js +29 -9
  31. package/lib/pdf.js +0 -0
  32. package/lib/pet-brief-store.js +0 -0
  33. package/lib/pet-brief.js +126 -64
  34. package/lib/receipt-verify.js +213 -15
  35. package/lib/relative-date.js +0 -0
  36. package/lib/remind.js +91 -36
  37. package/lib/research.js +0 -0
  38. package/lib/review.js +0 -0
  39. package/lib/routine.js +0 -0
  40. package/lib/safety/classifier.js +55 -2
  41. package/lib/safety/explain.js +0 -0
  42. package/lib/safety/index.js +0 -0
  43. package/lib/safety/redact.js +0 -0
  44. package/lib/safety/rules.js +28 -0
  45. package/lib/scheduler.js +0 -0
  46. package/lib/source.js +0 -0
  47. package/lib/sqlite.js +2302 -55
  48. package/lib/sync.js +398 -71
  49. package/lib/text-safe.js +229 -0
  50. package/lib/timestamp.js +29 -9
  51. package/lib/tos/index.js +1 -1
  52. package/lib/tos/manifest.json +6 -6
  53. package/lib/tos/privacy.zh-CN.md +12 -9
  54. package/lib/tos/safety.zh-CN.md +4 -3
  55. package/lib/tos/summary.zh-CN.md +3 -3
  56. package/lib/tos/terms.zh-CN.md +8 -6
  57. package/lib/tui/color.js +164 -0
  58. package/lib/tui/index.js +33 -0
  59. package/lib/tui/json.js +124 -0
  60. package/lib/tui/labels.js +50 -0
  61. package/lib/tui/policy.js +108 -0
  62. package/lib/tui/render.js +371 -0
  63. package/lib/tui/sanitize.js +142 -0
  64. package/lib/tui/text-file.js +116 -0
  65. package/lib/tui/width.js +577 -0
  66. package/lib/ui.js +0 -0
  67. package/lib/undo.js +0 -0
  68. package/lib/untrusted.js +186 -0
  69. package/lib/vault.js +0 -0
  70. package/package.json +6 -5
@@ -0,0 +1,577 @@
1
+ /**
2
+ * Display width, grapheme segmentation, wrapping, truncation and padding.
3
+ *
4
+ * This is the 0.3.0 output kernel's only width implementation: no runtime
5
+ * dependency, no locale data on disk. It follows Unicode UAX #11 East_Asian_Width:
6
+ *
7
+ * - W (wide) and F (fullwidth) code points occupy 2 columns;
8
+ * - combining marks (Mn/Me), format characters (Cf) and variation selectors
9
+ * occupy 0 columns;
10
+ * - emoji-presentation code points occupy 2 columns (emoji ZWJ sequences count
11
+ * the cluster once); flags (two regional indicators) occupy 2 columns;
12
+ * - everything else, including East_Asian_Width=A (ambiguous), occupies 1 column
13
+ * by default. ambiguousWidth: 2 is available for terminals the user configured
14
+ * with double-width ambiguous characters; the table is an approximation and must
15
+ * not be used to align output (see docs/TUI.md).
16
+ *
17
+ * Segmentation uses Intl.Segmenter (grapheme granularity) so a CJK character, an
18
+ * emoji ZWJ family, a flag, a skin-tone modifier or e + combining acute is never
19
+ * split by wrapping. A deterministic fallback exists for runtimes without
20
+ * Intl.Segmenter (only reachable through the `segmenter: null` test hook).
21
+ *
22
+ * Wrapping rules (docs/TUI.md):
23
+ * - explicit \n is a hard break; CRLF/CR are normalised to \n;
24
+ * - tabs are expanded to spaces (terminals disagree about tab stops);
25
+ * - no line starts with closing punctuation (,。、;:!?)》」』】〕〉”’ etc.);
26
+ * - no line ends with opening punctuation ((《「『【〔〈“‘ etc.);
27
+ * - ASCII words stay together when possible and are hard-broken only when they
28
+ * exceed the width alone (paths/URLs are preferred whole, not guaranteed);
29
+ * - a single grapheme wider than the limit is emitted on its own line (a CJK
30
+ * glyph cannot be split without corrupting the character).
31
+ *
32
+ * All functions are pure and allocation-only; no I/O.
33
+ */
34
+ import { stripTerminalSequences } from '../text-safe.js'
35
+
36
+ /** Default wrap width when the caller does not pass a usable value. */
37
+ export const DEFAULT_WIDTH = 80
38
+
39
+ const WIDE_RANGES = [
40
+ [0x1100, 0x115f], // Hangul Jamo initial
41
+ [0x231a, 0x231b],
42
+ [0x2329, 0x232a],
43
+ [0x23e9, 0x23ec],
44
+ [0x23f0, 0x23f0],
45
+ [0x23f3, 0x23f3],
46
+ [0x25fd, 0x25fe],
47
+ [0x2614, 0x2615],
48
+ [0x2648, 0x2653],
49
+ [0x267f, 0x267f],
50
+ [0x2693, 0x2693],
51
+ [0x26a1, 0x26a1],
52
+ [0x26aa, 0x26ab],
53
+ [0x26bd, 0x26be],
54
+ [0x26c4, 0x26c5],
55
+ [0x26ce, 0x26ce],
56
+ [0x26d4, 0x26d4],
57
+ [0x26ea, 0x26ea],
58
+ [0x26f2, 0x26f3],
59
+ [0x26f5, 0x26f5],
60
+ [0x26fa, 0x26fa],
61
+ [0x26fd, 0x26fd],
62
+ [0x2705, 0x2705],
63
+ [0x270a, 0x270b],
64
+ [0x2728, 0x2728],
65
+ [0x274c, 0x274c],
66
+ [0x274e, 0x274e],
67
+ [0x2753, 0x2755],
68
+ [0x2757, 0x2757],
69
+ [0x2795, 0x2797],
70
+ [0x27b0, 0x27b0],
71
+ [0x27bf, 0x27bf],
72
+ [0x2b1b, 0x2b1c],
73
+ [0x2b50, 0x2b50],
74
+ [0x2b55, 0x2b55],
75
+ [0x2e80, 0x2e99],
76
+ [0x2e9b, 0x2ef3],
77
+ [0x2f00, 0x2fd5],
78
+ [0x2ff0, 0x2ffb],
79
+ [0x3000, 0x303e],
80
+ [0x3041, 0x3096],
81
+ [0x3099, 0x30ff],
82
+ [0x3105, 0x312f],
83
+ [0x3131, 0x318e],
84
+ [0x3190, 0x31e3],
85
+ [0x31f0, 0x321e],
86
+ [0x3220, 0x3247],
87
+ [0x3250, 0x4dbf],
88
+ [0x4e00, 0xa48c],
89
+ [0xa490, 0xa4c6],
90
+ [0xa960, 0xa97c],
91
+ [0xac00, 0xd7a3],
92
+ [0xf900, 0xfaff],
93
+ [0xfe10, 0xfe19],
94
+ [0xfe30, 0xfe52],
95
+ [0xfe54, 0xfe66],
96
+ [0xfe68, 0xfe6b],
97
+ [0xff01, 0xff60],
98
+ [0xffe0, 0xffe6],
99
+ [0x16fe0, 0x16fe4],
100
+ [0x16ff0, 0x16ff1],
101
+ [0x17000, 0x187f7],
102
+ [0x18800, 0x18cd5],
103
+ [0x18d00, 0x18d08],
104
+ [0x1b000, 0x1b11e],
105
+ [0x1b150, 0x1b152],
106
+ [0x1b164, 0x1b167],
107
+ [0x1b170, 0x1b2fb],
108
+ [0x1f004, 0x1f004],
109
+ [0x1f0cf, 0x1f0cf],
110
+ [0x1f18e, 0x1f18e],
111
+ [0x1f191, 0x1f19a],
112
+ [0x1f200, 0x1f202],
113
+ [0x1f210, 0x1f23b],
114
+ [0x1f240, 0x1f248],
115
+ [0x1f250, 0x1f251],
116
+ [0x1f260, 0x1f265],
117
+ [0x20000, 0x2fffd],
118
+ [0x30000, 0x3fffd],
119
+ ]
120
+
121
+ // Common East_Asian_Width=A code points (pinning the ones users hit in Chinese
122
+ // prose and CLI output). The set is not exhaustive; it is only consulted when
123
+ // ambiguousWidth is 2, and the kernel never relies on ambiguous characters for
124
+ // alignment.
125
+ const AMBIGUOUS_RANGES = [
126
+ [0x00a1, 0x00a1], [0x00a4, 0x00a4], [0x00a7, 0x00a8], [0x00aa, 0x00aa],
127
+ [0x00ad, 0x00ae], [0x00b0, 0x00b4], [0x00b6, 0x00ba], [0x00bc, 0x00bf],
128
+ [0x00c6, 0x00c6], [0x00d0, 0x00d0], [0x00d7, 0x00d8], [0x00de, 0x00e1],
129
+ [0x00e6, 0x00e6], [0x00e8, 0x00ea], [0x00ec, 0x00ed], [0x00f0, 0x00f0],
130
+ [0x00f2, 0x00f3], [0x00f7, 0x00fa], [0x00fc, 0x00fc], [0x00fe, 0x00fe],
131
+ [0x0101, 0x0101], [0x0111, 0x0111], [0x0113, 0x0113], [0x011b, 0x011b],
132
+ [0x0126, 0x0127], [0x012b, 0x012b], [0x0131, 0x0133], [0x0138, 0x0138],
133
+ [0x013f, 0x0142], [0x0144, 0x0144], [0x0148, 0x014b], [0x014d, 0x014d],
134
+ [0x0152, 0x0153], [0x0166, 0x0167], [0x016b, 0x016b], [0x01ce, 0x01ce],
135
+ [0x01d0, 0x01d0], [0x01d2, 0x01d2], [0x01d4, 0x01d4], [0x01d6, 0x01d6],
136
+ [0x01d8, 0x01d8], [0x01da, 0x01da], [0x01dc, 0x01dc], [0x0251, 0x0251],
137
+ [0x0261, 0x0261], [0x02c4, 0x02c4], [0x02c7, 0x02c7], [0x02c9, 0x02cb],
138
+ [0x02cd, 0x02cd], [0x02d0, 0x02d0], [0x02d8, 0x02db], [0x02dd, 0x02dd],
139
+ [0x02df, 0x02df], [0x0300, 0x036f], [0x0391, 0x03a1], [0x03a3, 0x03a9],
140
+ [0x03b1, 0x03c1], [0x03c3, 0x03c9], [0x0401, 0x0401], [0x0410, 0x044f],
141
+ [0x0451, 0x0451], [0x2010, 0x2010], [0x2013, 0x2016], [0x2018, 0x2019],
142
+ [0x201c, 0x201d], [0x2020, 0x2022], [0x2024, 0x2027], [0x2030, 0x2030],
143
+ [0x2032, 0x2033], [0x2035, 0x2035], [0x203b, 0x203b], [0x203e, 0x203e],
144
+ [0x2074, 0x2074], [0x207f, 0x207f], [0x2081, 0x2084], [0x20ac, 0x20ac],
145
+ [0x2103, 0x2103], [0x2105, 0x2105], [0x2109, 0x2109], [0x2113, 0x2113],
146
+ [0x2116, 0x2116], [0x2121, 0x2122], [0x2126, 0x2126], [0x212b, 0x212b],
147
+ [0x2153, 0x2154], [0x215b, 0x215e], [0x2160, 0x216b], [0x2170, 0x2179],
148
+ [0x2189, 0x2189], [0x2190, 0x2199], [0x21b8, 0x21b9], [0x21d2, 0x21d2],
149
+ [0x21d4, 0x21d4], [0x21e7, 0x21e7], [0x2200, 0x2200], [0x2202, 0x2203],
150
+ [0x2207, 0x2208], [0x220b, 0x220b], [0x220f, 0x220f], [0x2211, 0x2211],
151
+ [0x2215, 0x2215], [0x221a, 0x221a], [0x221d, 0x2220], [0x2223, 0x2223],
152
+ [0x2225, 0x2225], [0x2227, 0x222c], [0x222e, 0x222e], [0x2234, 0x2237],
153
+ [0x223c, 0x223d], [0x2248, 0x2248], [0x224c, 0x224c], [0x2252, 0x2252],
154
+ [0x2260, 0x2261], [0x2264, 0x2267], [0x226a, 0x226b], [0x226e, 0x226f],
155
+ [0x2282, 0x2283], [0x2286, 0x2287], [0x2295, 0x2295], [0x2299, 0x2299],
156
+ [0x22a5, 0x22a5], [0x22bf, 0x22bf], [0x22da, 0x22db], [0x2305, 0x2306],
157
+ [0x2312, 0x2312], [0x2318, 0x2318], [0x23b0, 0x23b1], [0x23b4, 0x23b5],
158
+ [0x23b8, 0x23b9], [0x23d0, 0x23d0], [0x23dc, 0x23e1], [0x2460, 0x24fe],
159
+ [0x2500, 0x254b],
160
+ [0x2550, 0x2573], [0x2580, 0x258f], [0x2592, 0x2595], [0x25a0, 0x25a1],
161
+ [0x25a3, 0x25a9], [0x25b2, 0x25b3], [0x25b6, 0x25b7], [0x25bc, 0x25bd],
162
+ [0x25c0, 0x25c1], [0x25c6, 0x25c8], [0x25cb, 0x25cb], [0x25ce, 0x25d1],
163
+ [0x25e2, 0x25e5], [0x25ef, 0x25ef], [0x2605, 0x2606], [0x2609, 0x2609],
164
+ [0x260e, 0x260f], [0x261c, 0x261c], [0x261e, 0x261e], [0x2620, 0x2620],
165
+ [0x2622, 0x2622], [0x2624, 0x2624], [0x2626, 0x2626], [0x2628, 0x2628],
166
+ [0x262a, 0x262a], [0x262e, 0x262f], [0x2630, 0x2630], [0x2632, 0x2632],
167
+ [0x2634, 0x2634], [0x2636, 0x2637], [0x263d, 0x2640], [0x2642, 0x2647],
168
+ [0x2654, 0x2654], [0x2660, 0x2667], [0x2669, 0x266f], [0x2671, 0x267e],
169
+ [0x2680, 0x2689], [0x2690, 0x2691], [0x2692, 0x269c], [0x269e, 0x26a0],
170
+ [0x26a2, 0x26b1], [0x26b3, 0x26bc], [0x26bf, 0x26c3], [0x26c6, 0x26c7],
171
+ [0x26c9, 0x26cc], [0x26cd, 0x26cd], [0x26cf, 0x26d3], [0x26d5, 0x26d9],
172
+ [0x26db, 0x26e1], [0x26e3, 0x26e3], [0x26e8, 0x26e9], [0x26eb, 0x26f1],
173
+ [0x26f4, 0x26f4], [0x26f6, 0x26f9], [0x26fb, 0x26fc], [0x26fe, 0x2704],
174
+ ]
175
+
176
+ const OPENING_PUNCTUATION = new Set([
177
+ '\u0028', '\u005b', '\u007b', '\uff08', '\uff3b', '\uff5b', '\u3008', '\u300a', '\u300c',
178
+ '\u300e', '\u3010', '\u3014', '\u3016', '\u3018', '\u301a', '\u2018', '\u201c', '\u2039', '\u00ab',
179
+ ])
180
+ const CLOSING_PUNCTUATION = new Set([
181
+ '\u0029', '\u005d', '\u007d', '\uff09', '\uff3d', '\uff5d', '\u3009', '\u300b', '\u300d',
182
+ '\u300f', '\u3011', '\u3015', '\u3017', '\u3019', '\u301b', '\u2019', '\u201d', '\u203a', '\u00bb',
183
+ '\u002c', '\u002e', '\u003a', '\u003b', '\u0021', '\u003f', '\u0025',
184
+ '\uff0c', '\u3002', '\u3001', '\uff1b', '\uff1a', '\uff01', '\uff1f', '\u2026',
185
+ ])
186
+
187
+ const ASCII_WORD = /^[0-9A-Za-z_]$/
188
+ const ZERO_WIDTH = /[\p{Mn}\p{Me}\p{Cf}]/u
189
+ const EMOJI_PRESENTATION = /\p{Emoji_Presentation}/u
190
+ const REGIONAL_INDICATOR_START = 0x1f1e6
191
+ const REGIONAL_INDICATOR_END = 0x1f1ff
192
+
193
+ let cachedSegmenter
194
+
195
+ function getSegmenter() {
196
+ if (cachedSegmenter !== undefined) return cachedSegmenter
197
+ cachedSegmenter = typeof Intl === 'object' && typeof Intl.Segmenter === 'function'
198
+ ? new Intl.Segmenter('zh', { granularity: 'grapheme' })
199
+ : null
200
+ return cachedSegmenter
201
+ }
202
+
203
+ /**
204
+ * Split text into user-perceived characters (grapheme clusters).
205
+ *
206
+ * @param {unknown} value
207
+ * @param {{ segmenter?: Intl.Segmenter | null }} [options] `segmenter: null` forces the
208
+ * deterministic fallback (test hook; production always uses Intl.Segmenter).
209
+ * @returns {string[]}
210
+ */
211
+ export function graphemes(value, { segmenter } = {}) {
212
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
213
+ if (text.length === 0) return []
214
+ const active = segmenter === undefined ? getSegmenter() : segmenter
215
+ if (active === null || typeof active === 'undefined') return fallbackGraphemes(text)
216
+ const out = []
217
+ for (const part of active.segment(text)) out.push(part.segment)
218
+ return out
219
+ }
220
+
221
+ function fallbackGraphemes(text) {
222
+ const codePoints = Array.from(text)
223
+ const out = []
224
+ let joinNext = false
225
+ for (const codePoint of codePoints) {
226
+ const code = codePoint.codePointAt(0)
227
+ const previous = out.length > 0 ? out[out.length - 1] : null
228
+ const previousCode = previous === null ? -1 : previous.codePointAt(0)
229
+ const joinsPrevious = previous !== null && (
230
+ joinNext ||
231
+ isZeroWidthCodePoint(code) ||
232
+ code === 0xfe0f ||
233
+ code === 0xfe0e ||
234
+ code === 0x20e3 ||
235
+ (code >= 0x1f3fb && code <= 0x1f3ff) ||
236
+ (isRegionalIndicator(previousCode) && isRegionalIndicator(code) && previous.length === 2)
237
+ )
238
+ if (joinsPrevious) out[out.length - 1] = previous + codePoint
239
+ else out.push(codePoint)
240
+ joinNext = code === 0x200d
241
+ }
242
+ return out
243
+ }
244
+
245
+ /**
246
+ * Width of one code point in columns: 0 for combining/format/control, 2 for
247
+ * East_Asian_Width W/F and emoji presentation, else 1.
248
+ *
249
+ * @param {number} codePoint
250
+ * @param {{ ambiguousWidth?: 1 | 2 }} [options]
251
+ * @returns {number}
252
+ */
253
+ export function codePointWidth(codePoint, { ambiguousWidth = 1 } = {}) {
254
+ if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) return 0
255
+ if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) return 0
256
+ if (isZeroWidthCodePoint(codePoint)) return 0
257
+ if (isWideCodePoint(codePoint)) return 2
258
+ if (ambiguousWidth === 2 && inRanges(AMBIGUOUS_RANGES, codePoint)) return 2
259
+ return 1
260
+ }
261
+
262
+ /**
263
+ * Width of a grapheme cluster in columns.
264
+ *
265
+ * The cluster counts once: e + combining acute = 1, a ZWJ emoji family = 2,
266
+ * a flag = 2, a keycap = 2, a text-presentation variation selector forces 1.
267
+ *
268
+ * @param {unknown} cluster
269
+ * @param {{ ambiguousWidth?: 1 | 2 }} [options]
270
+ * @returns {number}
271
+ */
272
+ export function graphemeWidth(cluster, { ambiguousWidth = 1 } = {}) {
273
+ const text = cluster === null || cluster === undefined ? '' : String(cluster)
274
+ if (text.length === 0) return 0
275
+ if (text.includes('\ufe0e')) return 1
276
+ const codePoints = Array.from(text)
277
+ if (codePoints.length === 0) return 0
278
+ if (text.includes('\ufe0f') || text.includes('\u20e3')) return 2
279
+ const first = codePoints[0].codePointAt(0)
280
+ const second = codePoints.length > 1 ? codePoints[1].codePointAt(0) : -1
281
+ if (isRegionalIndicator(first) && isRegionalIndicator(second)) return 2
282
+ for (const codePoint of codePoints) {
283
+ const width = codePointWidth(codePoint.codePointAt(0), { ambiguousWidth })
284
+ if (width > 0) return width
285
+ }
286
+ return 0
287
+ }
288
+
289
+ /**
290
+ * Width of a string in terminal columns. Escape sequences are ignored (they are
291
+ * stripped before measuring); newlines and tabs are 0 columns.
292
+ *
293
+ * @param {unknown} value
294
+ * @param {{ ambiguousWidth?: 1 | 2, segmenter?: Intl.Segmenter | null }} [options]
295
+ * @returns {number}
296
+ */
297
+ export function strWidth(value, { ambiguousWidth = 1, segmenter } = {}) {
298
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
299
+ if (text.length === 0) return 0
300
+ const visible = stripTerminalSequences(text)
301
+ if (visible.length === 0) return 0
302
+ let width = 0
303
+ for (const cluster of graphemes(visible, { segmenter })) width += graphemeWidth(cluster, { ambiguousWidth })
304
+ return width
305
+ }
306
+
307
+ /** Alias kept for callers that prefer the longer name. */
308
+ export const stringWidth = strWidth
309
+
310
+ /**
311
+ * Width of a single character/code point (0-2 columns).
312
+ *
313
+ * @param {string | number} value a code point or a string (first grapheme is measured)
314
+ * @param {{ ambiguousWidth?: 1 | 2 }} [options]
315
+ * @returns {number}
316
+ */
317
+ export function charWidth(value, options = {}) {
318
+ if (typeof value === 'number') return codePointWidth(value, options)
319
+ if (typeof value !== 'string' || value.length === 0) return 0
320
+ const [first] = graphemes(value)
321
+ return first === undefined ? 0 : graphemeWidth(first, options)
322
+ }
323
+
324
+ /**
325
+ * Expand tabs to spaces using the display column; terminals disagree about tab
326
+ * stops, so wrapped output never contains a tab.
327
+ *
328
+ * @param {unknown} value
329
+ * @param {number} [tabSize]
330
+ * @returns {string}
331
+ */
332
+ export function expandTabs(value, tabSize = 4) {
333
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
334
+ if (text.indexOf('\t') === -1) return text
335
+ const size = Number.isInteger(tabSize) && tabSize > 0 ? tabSize : 4
336
+ let out = ''
337
+ let column = 0
338
+ for (const cluster of graphemes(text)) {
339
+ if (cluster === '\t') {
340
+ const spaces = size - (column % size)
341
+ out += ' '.repeat(spaces)
342
+ column += spaces
343
+ continue
344
+ }
345
+ out += cluster
346
+ column += graphemeWidth(cluster)
347
+ }
348
+ return out
349
+ }
350
+
351
+ /**
352
+ * Hard-wrap text to `width` columns. Returns display lines (no trailing newline).
353
+ *
354
+ * Explicit newlines are preserved (as line breaks); CRLF/CR are normalised. Tabs are
355
+ * expanded. A single grapheme wider than the limit is emitted alone (column overflow
356
+ * is unavoidable without corrupting the character).
357
+ *
358
+ * @param {unknown} value
359
+ * @param {number} width
360
+ * @param {{ ambiguousWidth?: 1 | 2, tabSize?: number, segmenter?: Intl.Segmenter | null }} [options]
361
+ * @returns {string[]}
362
+ */
363
+ export function wrap(value, width, { ambiguousWidth = 1, tabSize = 4, segmenter } = {}) {
364
+ const limit = Math.max(1, Math.floor(Number.isFinite(width) ? width : DEFAULT_WIDTH))
365
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
366
+ const normalized = text.replace(/\r\n?/g, '\n')
367
+ const out = []
368
+ for (const sourceLine of normalized.split('\n')) {
369
+ const expanded = expandTabs(sourceLine, tabSize)
370
+ if (expanded.length === 0) {
371
+ out.push('')
372
+ continue
373
+ }
374
+ for (const line of wrapLine(expanded, limit, { ambiguousWidth, segmenter })) out.push(line)
375
+ }
376
+ return out.length > 0 ? out : ['']
377
+ }
378
+
379
+ function wrapLine(line, limit, { ambiguousWidth, segmenter }) {
380
+ const clusters = graphemes(line, { segmenter })
381
+ if (clusters.length === 0) return ['']
382
+ const widths = clusters.map((cluster) => graphemeWidth(cluster, { ambiguousWidth }))
383
+ const lines = []
384
+ let start = 0
385
+ while (start < clusters.length) {
386
+ let end = start
387
+ let column = 0
388
+ let lastBreak = -1
389
+ while (end < clusters.length && column + widths[end] <= limit) {
390
+ column += widths[end]
391
+ end += 1
392
+ if (end < clusters.length && breakAllowed(clusters, end)) lastBreak = end
393
+ }
394
+ if (end >= clusters.length) {
395
+ lines.push(trimEndSpaces(clusters.slice(start).join('')))
396
+ break
397
+ }
398
+ let cut = lastBreak
399
+ if (cut <= start) cut = end > start ? end : start + 1
400
+ lines.push(trimEndSpaces(clusters.slice(start, cut).join('')))
401
+ start = cut
402
+ while (start < clusters.length && isSpaceCluster(clusters[start])) start += 1
403
+ }
404
+ return lines.length > 0 ? lines : ['']
405
+ }
406
+
407
+ function breakAllowed(clusters, index) {
408
+ if (index <= 0 || index >= clusters.length) return false
409
+ const previous = clusters[index - 1]
410
+ const next = clusters[index]
411
+ if (OPENING_PUNCTUATION.has(previous)) return false
412
+ if (CLOSING_PUNCTUATION.has(next)) return false
413
+ if (isSpaceCluster(previous)) return true
414
+ if (isSpaceCluster(next)) return false
415
+ if (ASCII_WORD.test(previous) && ASCII_WORD.test(next)) return false
416
+ return true
417
+ }
418
+
419
+ function isSpaceCluster(cluster) {
420
+ return cluster === ' ' || cluster === '\t'
421
+ }
422
+
423
+ function trimEndSpaces(text) {
424
+ return text.replace(/[ \t]+$/, '')
425
+ }
426
+
427
+ /**
428
+ * Truncate to a column budget, appending an ellipsis when content is lost.
429
+ *
430
+ * @param {unknown} value
431
+ * @param {number} maxWidth
432
+ * @param {{ ellipsis?: string, ambiguousWidth?: 1 | 2, segmenter?: Intl.Segmenter | null }} [options]
433
+ * @returns {string}
434
+ */
435
+ export function truncateToWidth(value, maxWidth, { ellipsis = '\u2026', ambiguousWidth = 1, segmenter } = {}) {
436
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
437
+ const limit = Math.max(0, Math.floor(Number.isFinite(maxWidth) ? maxWidth : DEFAULT_WIDTH))
438
+ if (limit === 0) return ''
439
+ if (strWidth(text, { ambiguousWidth, segmenter }) <= limit) return text
440
+ const markWidth = strWidth(ellipsis, { ambiguousWidth })
441
+ if (markWidth >= limit) {
442
+ const markClusters = graphemes(ellipsis, { segmenter })
443
+ let out = ''
444
+ let used = 0
445
+ for (const cluster of markClusters) {
446
+ const width = graphemeWidth(cluster, { ambiguousWidth })
447
+ if (used + width > limit) break
448
+ out += cluster
449
+ used += width
450
+ }
451
+ return out
452
+ }
453
+ const budget = limit - markWidth
454
+ let out = ''
455
+ let used = 0
456
+ for (const cluster of graphemes(text, { segmenter })) {
457
+ const width = graphemeWidth(cluster, { ambiguousWidth })
458
+ if (used + width > budget) break
459
+ out += cluster
460
+ used += width
461
+ }
462
+ return out + ellipsis
463
+ }
464
+
465
+ /**
466
+ * Truncate in the middle, keeping the start and the end (paths, commands, digests).
467
+ *
468
+ * @param {unknown} value
469
+ * @param {number} maxWidth
470
+ * @param {{ ellipsis?: string, ambiguousWidth?: 1 | 2, segmenter?: Intl.Segmenter | null }} [options]
471
+ * @returns {string}
472
+ */
473
+ export function truncateMiddle(value, maxWidth, { ellipsis = '\u2026', ambiguousWidth = 1, segmenter } = {}) {
474
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
475
+ const limit = Math.max(0, Math.floor(Number.isFinite(maxWidth) ? maxWidth : DEFAULT_WIDTH))
476
+ if (limit === 0) return ''
477
+ if (strWidth(text, { ambiguousWidth, segmenter }) <= limit) return text
478
+ const markWidth = strWidth(ellipsis, { ambiguousWidth })
479
+ if (markWidth >= limit - 1) return truncateToWidth(text, limit, { ellipsis, ambiguousWidth, segmenter })
480
+ const budget = limit - markWidth
481
+ const headBudget = Math.ceil(budget / 2)
482
+ const tailBudget = budget - headBudget
483
+ const clusters = graphemes(text, { segmenter })
484
+ let head = ''
485
+ let used = 0
486
+ for (const cluster of clusters) {
487
+ const width = graphemeWidth(cluster, { ambiguousWidth })
488
+ if (used + width > headBudget) break
489
+ head += cluster
490
+ used += width
491
+ }
492
+ let tail = ''
493
+ used = 0
494
+ for (let index = clusters.length - 1; index >= 0; index -= 1) {
495
+ const cluster = clusters[index]
496
+ const width = graphemeWidth(cluster, { ambiguousWidth })
497
+ if (used + width > tailBudget) break
498
+ tail = cluster + tail
499
+ used += width
500
+ }
501
+ return head + ellipsis + tail
502
+ }
503
+
504
+ /**
505
+ * Pad text to exactly `width` columns (no truncation: wider text is returned as is).
506
+ *
507
+ * @param {unknown} value
508
+ * @param {number} width
509
+ * @param {{ align?: 'left' | 'right' | 'center', padChar?: string, ambiguousWidth?: 1 | 2 }} [options]
510
+ * @returns {string}
511
+ */
512
+ export function padToWidth(value, width, { align = 'left', padChar = ' ', ambiguousWidth = 1 } = {}) {
513
+ const text = value === null || value === undefined ? '' : typeof value === 'string' ? value : String(value)
514
+ const target = Math.max(0, Math.floor(Number.isFinite(width) ? width : 0))
515
+ const current = strWidth(text, { ambiguousWidth })
516
+ if (current >= target) return text
517
+ const fill = String(padChar === undefined || padChar === '' ? ' ' : padChar)
518
+ let count = target - current
519
+ let padding = ''
520
+ while (count > 0) {
521
+ padding += fill
522
+ count -= Math.max(1, strWidth(fill, { ambiguousWidth }))
523
+ }
524
+ padding = truncateToWidth(padding, target - current, { ellipsis: ' ', ambiguousWidth })
525
+ if (align === 'right') return padding + text
526
+ if (align === 'center') {
527
+ const left = Math.floor(strWidth(padding, { ambiguousWidth }) / 2)
528
+ const leftPadding = truncateToWidth(padding, left, { ellipsis: ' ', ambiguousWidth })
529
+ const rightPadding = truncateToWidth(padding, strWidth(padding) - strWidth(leftPadding), { ellipsis: ' ', ambiguousWidth })
530
+ return leftPadding + text + rightPadding
531
+ }
532
+ return text + padding
533
+ }
534
+
535
+ /** Alias for padToWidth (research API name). */
536
+ export const pad = padToWidth
537
+
538
+ /**
539
+ * Clamp a terminal column count to the shell's rendering range.
540
+ *
541
+ * @param {unknown} value
542
+ * @param {{ min?: number, max?: number, fallback?: number }} [options]
543
+ * @returns {number}
544
+ */
545
+ export function clampColumns(value, { min = 40, max = 200, fallback = DEFAULT_WIDTH } = {}) {
546
+ const number = typeof value === 'number' ? value : Number.parseInt(String(value ?? ''), 10)
547
+ const start = Number.isFinite(number) ? Math.floor(number) : fallback
548
+ return Math.min(max, Math.max(min, start))
549
+ }
550
+
551
+ function isZeroWidthCodePoint(codePoint) {
552
+ if (codePoint === 0x200b || codePoint === 0x200c || codePoint === 0x200d || codePoint === 0x2060 || codePoint === 0xfeff) return true
553
+ return ZERO_WIDTH.test(String.fromCodePoint(codePoint))
554
+ }
555
+
556
+ function isWideCodePoint(codePoint) {
557
+ if (EMOJI_PRESENTATION.test(String.fromCodePoint(codePoint))) return true
558
+ return inRanges(WIDE_RANGES, codePoint)
559
+ }
560
+
561
+ function isRegionalIndicator(codePoint) {
562
+ return codePoint >= REGIONAL_INDICATOR_START && codePoint <= REGIONAL_INDICATOR_END
563
+ }
564
+
565
+ function inRanges(ranges, codePoint) {
566
+ let low = 0
567
+ let high = ranges.length - 1
568
+ while (low <= high) {
569
+ const middle = (low + high) >> 1
570
+ const [start, end] = ranges[middle]
571
+ if (codePoint < start) high = middle - 1
572
+ else if (codePoint > end) low = middle + 1
573
+ else return true
574
+ }
575
+ return false
576
+ }
577
+
package/lib/ui.js CHANGED
File without changes
package/lib/undo.js CHANGED
File without changes