From bec4f266eff4fbaca598e29fe30235cb11203c0c Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Fri, 5 Dec 2025 12:13:39 -0300 Subject: [PATCH 1/2] Fix line wrapping issue Fixed a bug where multi-line output would fail to clear properly on standard terminals (macOS Terminal, iTerm2, gnome-terminal, konsole, xterm), causing previous output to stack and "bleed" upward, overwriting earlier terminal content. This only occurred when output exceeded terminal width and soft-wrapped across multiple physical lines. Root Cause: The clearing logic used ANSI escape sequence \x1b[1000D (move left 1000 columns) before clearing each line. However, when content soft-wraps across multiple physical lines without explicit newlines, most VT100-compatible terminals treat this as a single logical line. The MOVE_UP command (\x1b[1A) then operates on logical lines rather than physical wrapped lines, causing the cursor to target wrong screen positions during clearing. Solution: Replaced \x1b[1000D (move left 1000 columns) with \r (carriage return) in the line clearing sequence. Carriage return is a standard ASCII control character that reliably moves the cursor to column 0 of the current physical line across all terminal emulators, regardless of soft-wrap behavior. Also fixed log.clear() to output a newline instead of an empty string, ensuring the cursor properly moves to a new line after clearing, preventing the bash prompt from appearing on the same line as cleared content. Changes: - Line 27: Replace MOVE_LEFT with \r for universal column 0 positioning - Line 43: Change log.clear() to output \n for proper cursor positioning Fixes issues with soft-wrapped line clearing on all standard POSIX terminals. --- index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index c1201be..3c8eedf 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -var MOVE_LEFT = new Buffer('1b5b3130303044', 'hex').toString(); var MOVE_UP = new Buffer('1b5b3141', 'hex').toString(); var CLEAR_LINE = new Buffer('1b5b304b', 'hex').toString(); var stringWidth = require('string-width'); @@ -23,9 +22,9 @@ module.exports = function(stream) { str = ''; var nextStr = Array.prototype.join.call(arguments, ' '); - // Clear screen + // Move to beginning of line and clear, then move up for each previous line for (var i=0; i Date: Fri, 5 Dec 2025 12:34:17 -0300 Subject: [PATCH 2/2] Add terminal-width wrapping test case --- test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test.js b/test.js index c8a2e79..ced5f84 100644 --- a/test.js +++ b/test.js @@ -12,6 +12,8 @@ setInterval(function() { if (i < 30) s += '\nline 3 - ' + Math.random(); if (i < 20) s += '\nline 4 - ' + Math.random(); + if (i < 10) s += '\nline 5 - Long line - ' + '*'.repeat(process.stdout.columns) + ' - ' + Math.random(); + log(s); if (i === 50) {