omega
omega

Reputation: 43893

How to convert ConPTY output to HTML

I'm using the ConPTY node api to simulate a terminal and capture the text. Before that I used the regular powershell.exe terminal and used this module to convert it to html which worked perfectly.

https://www.npmjs.com/package/ansi-to-html

But now with ConPTY, the format is different a little and it doesn't work as well. I see some strange text. Does anyone know what is the best way to convert it into friendly HTML?

https://github.com/microsoft/node-pty

Upvotes: 1

Views: 341

Answers (1)

omega
omega

Reputation: 43893

Figured it out

const pty = require('node-pty');
const AnsiTerminal = require('node-ansiterminal').AnsiTerminal;
const AnsiParser = require('node-ansiparser');
const Convert = require('ansi-to-html');

const shell = ptyProcess = pty.spawn('powershell.exe', ['-executionpolicy', 'remotesigned', '-File', 'test.ps1'], { name: 'test', cols: terminal_columns, rows: 30, cwd: __dirname, env: process.env });

var convert = new Convert({
    newline:true
});

const terminal_columns = 160;

var terminal = new AnsiTerminal(terminal_columns, 30, 500);
var parser = new AnsiParser(terminal);

ptyProcess.onData((data) => {
    const msg = data.toString();
    parser.parse(msg);
    const str = terminal.toString().trim() + "\n\n";
    var html_str = convert.toHtml(str);      
});

ptyProcess.onExit(function(data) {  
    // exit event
});

Upvotes: 2

Related Questions