不正确的stringparsing

我试图从控制台输出转换成HTML的ANSI颜色代码。 我有一个脚本来做到这一点,但我似乎无法让它parsing节点js内的string。 我曾试图JSON.stringify它也包括特殊的字符,但它不工作。

forever list [32minfo[39m: Forever processes running [90mscript[39m [37mforever[39m [37mpid[39m [37mid[39m [90mdata[39m: [37m [39m [37muid[39m [90mcommand[39m 

我从js的节点ssh2shell中得到这样的输出。 我有一个脚本: https : //github.com/pixelb/scripts/blob/master/scripts/ansi2html.sh

这是应该将上述转换为HTML并添加适当的颜色代码。 它正常工作正常的terminal输出,例如:

 npm install --color=always | ansi2html.sh > npminstall.html 

这是在Linux机器上输出到文件的原始输出。 看来JSstring在console.log中显示时缺less这些转义符,但是它们在这里也缺less换行符。 也许是因为我把它们直接连接成string,并删除特殊字符?

 total 24 -rwxr-xr-x 1 admin admin 17002 May 13 02:52 ^[[0m^[[38;5;34mansi2html.sh^[[0m drwxr-xr-x 4 admin admin 4096 May 13 00:00 ^[[38;5;27mgit^[[0m -rw-r--r-- 1 admin admin 0 May 13 02:57 ls.html 

希望有一些这是有道理的。

谢谢

SSH2shell应用于命令的输出中有几个filter。 第一个从响应中删除非标准的ASCII,然后颜色格式代码被删除。

在v1.6.0中,我添加了pipe()/ unpipe(),这两个事件都暴露了stream.on('data',function(data){})事件,所以你可以直接访问stream输出而不用SSH2shell交互它以任何方式。 这应该通过给你访问原始数据来解决从SSH2shell获得正确输出的问题。

 var fs = require('fs') var host = { server: { host: mydomain.com, port: 22, userName: user, password: password:) }, commands: [ "`Test session text message: passed`", "msg:console test notification: passed", "ls -la" ], } //until npm published use the cloned dir path. var SSH2Shell = require ('ssh2shell') //run the commands in the shell session var SSH = new SSH2Shell(host), callback = function( sessionText ){ console.log ( "-----Callback session text:\n" + sessionText); console.log ( "-----Callback end" ); }, firstLog = fs.createWriteStream('first.log'), secondLog = fs.createWriteStream('second.log'), buffer = "" //multiple pipes can be added but they wont be bound to the stream until the connection is established SSH.pipe(firstLog).pipe(secondLog); SSH.on('data', function(data){ //do something with the data chunk console.log(data) }) SSH.connect(callback) 

试过这个?

https://github.com/hughsk/ansi-html-stream

 var spawn = require('child_process').spawn , ansi = require('ansi-html-stream') , fs = require('fs') var npm = spawn('npm', ['install', 'browserify', '--color', 'always'], { cwd: process.cwd() }) var stream = ansi({ chunked: false }) , file = fs.createWriteStream('browserify.html', 'utf8') npm.stdout.pipe(stream) npm.stderr.pipe(stream) stream.pipe(file, { end: false }) stream.once('end', function() { file.end('</pre>\n') }) file.write('<pre>\n');