1+ import stringWidth from "string-width" ;
12import {
23 ANSI_COMPAT_CONTROL_SEQUENCE_PATTERN ,
34 ANSI_OSC_INTRODUCER_PATTERN ,
@@ -179,102 +180,27 @@ export function sanitizeForLog(v: string): string {
179180 return stripAnsi ( v ) . replace ( controlCharsRegex , "" ) ;
180181}
181182
182- function isZeroWidthCodePoint ( codePoint : number ) : boolean {
183- return (
184- ( codePoint <= 0x1f && codePoint !== 0x09 ) ||
185- ( codePoint >= 0x7f && codePoint <= 0x9f ) ||
186- ( codePoint >= 0x0300 && codePoint <= 0x036f ) ||
187- ( codePoint >= 0x1ab0 && codePoint <= 0x1aff ) ||
188- ( codePoint >= 0x1dc0 && codePoint <= 0x1dff ) ||
189- ( codePoint >= 0x20d0 && codePoint <= 0x20ff ) ||
190- ( codePoint >= 0xfe20 && codePoint <= 0xfe2f ) ||
191- ( codePoint >= 0xfe00 && codePoint <= 0xfe0f ) ||
192- codePoint === 0x200d
193- ) ;
194- }
195-
196- function isFullWidthCodePoint ( codePoint : number ) : boolean {
197- if ( codePoint < 0x1100 ) {
198- return false ;
199- }
200- return (
201- codePoint <= 0x115f ||
202- codePoint === 0x2329 ||
203- codePoint === 0x232a ||
204- ( codePoint >= 0x2e80 && codePoint <= 0x3247 && codePoint !== 0x303f ) ||
205- ( codePoint >= 0x3250 && codePoint <= 0x4dbf ) ||
206- ( codePoint >= 0x4e00 && codePoint <= 0xa4c6 ) ||
207- ( codePoint >= 0xa960 && codePoint <= 0xa97c ) ||
208- ( codePoint >= 0xac00 && codePoint <= 0xd7a3 ) ||
209- ( codePoint >= 0xf900 && codePoint <= 0xfaff ) ||
210- ( codePoint >= 0xfe10 && codePoint <= 0xfe19 ) ||
211- ( codePoint >= 0xfe30 && codePoint <= 0xfe6b ) ||
212- ( codePoint >= 0xff01 && codePoint <= 0xff60 ) ||
213- ( codePoint >= 0xffe0 && codePoint <= 0xffe6 ) ||
214- ( codePoint >= 0x1aff0 && codePoint <= 0x1aff3 ) ||
215- ( codePoint >= 0x1aff5 && codePoint <= 0x1affb ) ||
216- ( codePoint >= 0x1affd && codePoint <= 0x1affe ) ||
217- ( codePoint >= 0x1b000 && codePoint <= 0x1b2ff ) ||
218- ( codePoint >= 0x1f200 && codePoint <= 0x1f251 ) ||
219- ( codePoint >= 0x20000 && codePoint <= 0x3fffd )
220- ) ;
221- }
222-
223- const rgiEmojiPattern = new RegExp ( "^\\p{RGI_Emoji}$" , "v" ) ;
224- const emojiPresentationPattern = / \p{ Emoji_Presentation} / u;
225- const regionalIndicatorPattern = / \p{ Regional_Indicator} / u;
226- const unqualifiedKeycapPattern = / ^ [ # * 0 - 9 ] \u20E3 $ / u;
227- const extendedPictographicPattern = / \p{ Extended_Pictographic} / gu;
228-
229- function isWideEmojiGrapheme ( grapheme : string ) : boolean {
230- const isRgiEmoji = rgiEmojiPattern . test ( grapheme ) ;
231- // RGI recognizes paired flags while keeping a lone regional indicator narrow.
232- if ( regionalIndicatorPattern . test ( grapheme ) ) {
233- return isRgiEmoji ;
234- }
235- if (
236- emojiPresentationPattern . test ( grapheme ) ||
237- isRgiEmoji ||
238- unqualifiedKeycapPattern . test ( grapheme )
239- ) {
240- return true ;
241- }
242- // Minimally qualified ZWJ sequences still shape as one wide emoji in terminals.
243- return (
244- grapheme . includes ( "\u200D" ) && ( grapheme . match ( extendedPictographicPattern ) ?. length ?? 0 ) >= 2
245- ) ;
246- }
247-
248- function graphemeWidth ( grapheme : string ) : number {
249- if ( ! grapheme ) {
250- return 0 ;
251- }
252- if ( isWideEmojiGrapheme ( grapheme ) ) {
253- return 2 ;
254- }
255-
256- let sawPrintable = false ;
257- for ( const char of grapheme ) {
258- const codePoint = char . codePointAt ( 0 ) ;
259- if ( codePoint == null ) {
260- continue ;
261- }
262- if ( isZeroWidthCodePoint ( codePoint ) ) {
263- continue ;
264- }
265- if ( isFullWidthCodePoint ( codePoint ) ) {
266- return 2 ;
267- }
268- sawPrintable = true ;
183+ function textWidth ( text : string ) : number {
184+ // POSIX renders these default-ignorable Hangul fillers as wide/halfwidth cells;
185+ // same-shaping representatives and well-formed surrogates preserve terminal output.
186+ const printable = / [ \u115F \u3164 \uFFA0 \uD800 - \uDFFF ] / u. test ( text )
187+ ? text
188+ . replace ( / [ \uD800 - \uDFFF ] / gu, "\uFFFD" )
189+ . replaceAll ( "\u115F" , "\u1100" )
190+ . replaceAll ( "\u3164" , "\u3131" )
191+ . replaceAll ( "\uFFA0" , "\uFF8A" )
192+ : text ;
193+ // OpenClaw owns ANSI parsing; upstream must not reinterpret malformed sequences.
194+ let width = stringWidth ( printable , { countAnsiEscapeCodes : true } ) ;
195+ // Tabs execute inside CSI too; string-width intentionally treats them as zero-width.
196+ for ( let index = text . indexOf ( "\t" ) ; index !== - 1 ; index = text . indexOf ( "\t" , index + 1 ) ) {
197+ width += 1 ;
269198 }
270- return sawPrintable ? 1 : 0 ;
199+ return width ;
271200}
272201
273202export function visibleWidth ( input : string ) : number {
274- return splitGraphemes ( stripAnsi ( input ) ) . reduce (
275- ( sum , grapheme ) => sum + graphemeWidth ( grapheme ) ,
276- 0 ,
277- ) ;
203+ return textWidth ( stripAnsi ( input ) ) ;
278204}
279205
280206/**
@@ -288,7 +214,9 @@ export function truncateToVisibleWidth(input: string, maxWidth: number): string
288214 if ( maxWidth <= 0 ) {
289215 return "" ;
290216 }
291- if ( visibleWidth ( input ) <= maxWidth ) {
217+ const plainInput = stripAnsi ( input ) ;
218+ const inputWidth = textWidth ( plainInput ) ;
219+ if ( inputWidth <= maxWidth ) {
292220 return input ;
293221 }
294222 let out = "" ;
@@ -301,20 +229,89 @@ export function truncateToVisibleWidth(input: string, maxWidth: number): string
301229 if ( budgetSpent ) {
302230 return ;
303231 }
304- for ( const grapheme of splitGraphemes ( segment ) ) {
305- const width = graphemeWidth ( grapheme ) ;
306- if ( used + width > maxWidth ) {
307- budgetSpent = true ;
308- return ;
309- }
310- out += grapheme ;
232+ const remaining = maxWidth - used ;
233+ const width = segment === plainInput ? inputWidth : textWidth ( segment ) ;
234+ if ( width <= remaining ) {
235+ out += segment ;
311236 used += width ;
237+ return ;
238+ }
239+
240+ const graphemes = splitGraphemes ( segment ) ;
241+ let offset = 0 ;
242+ const offsets = [ offset ] ;
243+ for ( const grapheme of graphemes ) {
244+ offset += grapheme . length ;
245+ offsets . push ( offset ) ;
246+ }
247+ let start = 0 ;
248+ let fittedWidth = 0 ;
249+ if ( remaining <= width / 2 ) {
250+ let end = Math . max (
251+ 1 ,
252+ Math . min ( graphemes . length - 1 , Math . floor ( ( remaining * graphemes . length ) / width ) ) ,
253+ ) ;
254+ let stride = 1 ;
255+ // Estimate the cell boundary first; gallop handles uneven/zero-width clusters.
256+ while ( end < graphemes . length ) {
257+ const candidateWidth = textWidth ( segment . slice ( 0 , offsets [ end ] ) ) ;
258+ if ( candidateWidth > remaining ) {
259+ break ;
260+ }
261+ start = end ;
262+ fittedWidth = candidateWidth ;
263+ end = Math . min ( graphemes . length , end + stride ) ;
264+ stride *= 2 ;
265+ }
266+ while ( start + 1 < end ) {
267+ const middle = Math . floor ( ( start + end ) / 2 ) ;
268+ const candidateWidth = textWidth ( segment . slice ( 0 , offsets [ middle ] ) ) ;
269+ if ( candidateWidth <= remaining ) {
270+ start = middle ;
271+ fittedWidth = candidateWidth ;
272+ } else {
273+ end = middle ;
274+ }
275+ }
276+ } else {
277+ const overflow = width - remaining ;
278+ let tooShort = 0 ;
279+ let removed = Math . min ( graphemes . length , 1 ) ;
280+ let removedWidth = width ;
281+ // Near-end cuts search short complete-grapheme suffixes, not repeated full prefixes.
282+ while ( removed < graphemes . length ) {
283+ removedWidth = textWidth ( segment . slice ( offsets [ graphemes . length - removed ] ) ) ;
284+ if ( removedWidth >= overflow ) {
285+ break ;
286+ }
287+ tooShort = removed ;
288+ removed = Math . min ( graphemes . length , removed * 2 ) ;
289+ }
290+ if ( removed === graphemes . length ) {
291+ removedWidth = width ;
292+ }
293+ while ( tooShort + 1 < removed ) {
294+ const middle = Math . floor ( ( tooShort + removed ) / 2 ) ;
295+ const candidateWidth = textWidth ( segment . slice ( offsets [ graphemes . length - middle ] ) ) ;
296+ if ( candidateWidth >= overflow ) {
297+ removed = middle ;
298+ removedWidth = candidateWidth ;
299+ } else {
300+ tooShort = middle ;
301+ }
302+ }
303+ start = graphemes . length - removed ;
304+ fittedWidth = width - removedWidth ;
312305 }
306+ out += segment . slice ( 0 , offsets [ start ] ) ;
307+ used += fittedWidth ;
308+ budgetSpent = true ;
313309 } ;
314310 for ( const segment of splitAnsiSegments ( input ) ) {
315311 if ( segment . kind === "ansi" ) {
316- const widthControls = segment . controls . filter ( ( control ) => graphemeWidth ( control ) > 0 ) ;
317- const controlWidth = widthControls . reduce ( ( sum , control ) => sum + graphemeWidth ( control ) , 0 ) ;
312+ // CSI retains only C0/DEL controls; TAB is the sole visible-width member.
313+ const widthControls = segment . controls . filter ( ( control ) => control === "\t" ) ;
314+ const controlWidth = widthControls . length ;
318315 if ( ! budgetSpent && used + controlWidth <= maxWidth ) {
319316 out += segment . value ;
320317 used += controlWidth ;
0 commit comments