如何将 ConPTY 输出转换为 HTML [英] How to convert ConPTY output to HTML

查看:35
本文介绍了如何将 ConPTY 输出转换为 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ConPTY 节点 api 来模拟终端并捕获文本.在此之前,我使用了常规的 powershell.exe 终端,并使用此模块将其转换为 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

但是现在使用 ConPTY,格式略有不同,而且效果不佳.我看到一些奇怪的文字.有谁知道将其转换为友好 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

推荐答案

想通了

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
});

这篇关于如何将 ConPTY 输出转换为 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆