rburns / ansi-to-html

Convert ansi escaped text streams to html.
MIT License
357 stars 48 forks source link

Support for Handling Cursor Movement ANSI Sequences (e.g., \u001b[1A, \u001b[2K) #112

Open kentcdodds opened 2 months ago

kentcdodds commented 2 months ago

Thanks for this package! It's great!

Currently, ansi-to-html correctly handles styling-related ANSI escape sequences (such as color and text formatting) but does not handle certain cursor movement or clearing sequences like \u001b[1A (move cursor up one line) and \u001b[2K (clear line).

When terminal output contains these sequences, they are incorrectly rendered as characters such as "A" in the HTML output. These escape sequences should be handled by ansi-to-html so that the generated HTML more accurately reflects the original terminal output.

Minimal Reproduction

Here's a minimal Node.js example that reproduces the issue (based on my actual use case):

const AnsiToHtml = require('ansi-to-html');
const convert = new AnsiToHtml();

const data = `
\u001b[1A\u001b[2K[WebServer] 🚀 We have liftoff!
\u001b[2mRunning \u001b[22m1\u001b[2m test using \u001b[22m1\u001b[2m worker\u001b[22m
`;

const htmlOutput = convert.toHtml(data);
console.log(htmlOutput);

Expected Behavior

In the output HTML, the cursor movement and line-clearing sequences (\u001b[1A, \u001b[2K) should be correctly interpreted or ignored, resulting in HTML that displays only the actual terminal content without the extra "A" characters.

Actual Behavior

Instead of handling these sequences properly, they result in the "A" character being rendered in the HTML output. Here's an example of what the HTML output looks like with the current version of ansi-to-html:

A[WebServer] 🚀 We have liftoff!
Running 1 test using 1 worker

Possible Solution

Support for handling cursor movement and line-clearing ANSI sequences (e.g., \u001b[1A, \u001b[2K) should be added. These sequences should either be ignored (when applicable) or processed to reflect the corresponding terminal behavior in the HTML output.