Skip to content

Commit af8c51e

Browse files
committed
style(live-monitor): Fix some typos and Add description about drawEmoji process
1 parent 9179cd5 commit af8c51e

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

‎src/_terminal-utils.ts‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,29 +300,28 @@ export function centerText(text: string, width: number): string {
300300
return ' '.repeat(leftPadding) + text + ' '.repeat(rightPadding);
301301
}
302302

303+
// Using below sequences causes issues in some terminals such as NeoVim.
304+
// - ansiEscapes.cursorSavePosition (\u001B[s = Save Cursor)
305+
// - ansiEscapes.cursorRestorePosition (\u001B[u = Unsave Cursor)
306+
//
307+
// Instead, we use:
308+
// - \u001B7 = Save Cursor & Attrs
309+
// - \u001B8 = Restore Cursor & Attrs
310+
//
311+
// see: https://www2.ccs.neu.edu/research/gpc/VonaUtils/vona/terminal/vtansi.htm
312+
const SAVE_CURSOR = '\u001B7';
313+
const RESTORE_CURSOR = '\u001B8';
303314
/**
304315
* Draws an emoji with consistent 2-character width regardless of terminal behavior
305316
* @param emoji The emoji to draw
306317
* @returns A string containing ANSI escape sequences and the emoji
307318
*/
308319
export function drawEmoji(emoji: string): string {
309-
const emojiWidth = stringWidth(emoji);
310-
const content = emojiWidth === 1 ? `${emoji} ` : emoji;
311-
312-
// Using below sequences instead causes issues in some terminals such as NeoVim.
313-
// - ansiEscapes.cursorSavePosition (\u001B[s = Save Cursor)
314-
// - ansiEscapes.cursorRestorePosition (\u001B[u = Unsave Cursor)
315-
//
316-
// Instead, we use:
317-
// - \u001B7 = Save Cursor & Attrs
318-
// - \u001B8 = Restore Cursor & Attrs
319-
//
320-
// see: https://www2.ccs.neu.edu/research/gpc/VonaUtils/vona/terminal/vtansi.htm
321-
322-
const SAVE_CUROSR = '\u001B7';
323-
const RESTORE_CURSOR = '\u001B8';
324-
325-
return `${SAVE_CUROSR}${content}${RESTORE_CURSOR}${ansiEscapes.cursorForward(2)}`;
320+
// Since the width is also measured by stringWidth on the function call side,
321+
// the width consumed by the terminal and the value returned by stringWidth must be consistent.
322+
const content = stringWidth(emoji) === 1 ? `${emoji} ` : emoji;
323+
324+
return `${SAVE_CURSOR}${content}${RESTORE_CURSOR}${ansiEscapes.cursorForward(2)}`;
326325
}
327326

328327
// In-source testing

0 commit comments

Comments
 (0)