字符串解析不正确
Improper parsing of strings
我正在尝试将控制台输出的ansi颜色代码转换为HTML。我找到了一个脚本来做这件事,但我似乎无法让它解析nodejs中的字符串。我尝试过将其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并添加适当的颜色代码。它与正常的终端输出配合良好,例如:
npm install --color=always | ansi2html.sh > npminstall.html
这是linux机器上通过管道传输到文件的原始输出。当JS字符串显示在console.log中时,它们似乎缺少这些转义符,但它们也缺少换行符。也许是因为我将它们直接连接到字符串中,并删除了特殊字符?
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对命令的输出应用了几个过滤器。首先从响应中删除非标准ASCII,然后删除彩色格式代码。
在v1.6.0中,我添加了pipe()/unpipe(),用于两者的事件,并公开了stream.on('data',function(data){})事件,因此您可以直接访问流输出,而无需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');
相关文章:
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 字符串解析不正确
- 由于布尔值的变化,Django视图中的字符串格式不正确
- 为什么不'将字符串转换为int不正确(parseInt)
- 格式为nnnnnnnn的字符串在电子表格中显示为日期或不正确
- 自定义查询字符串分析器返回不正确的结果
- jQuery从HTML字符串构建不正确的DOM
- 计算字符串时答案不正确
- XML 解析错误:使用浏览器内置解析器解析 XML 字符串时格式不正确
- 处理“不正确的字符串值”
- 读取具有“'”的 JSON 字符串(格式不正确?
- 如果JSON不正确,如何验证JSON字符串并返回错误
- 字符串中存在多个空格时,字数不正确
- ES6:创建ASCII字符串的字符转义序列不正确
- MD5字符串与html()一起显示不正确
- 时间转换为ISO字符串不正确,显示文本字段
- 对象.创建,JSON时不正确.对对象进行字符串化
- Jquery if条件,如果字符串格式不正确,将调用Alert
- 主干js,JSON字符串存储不正确
- Safari浏览器Javascript数组似乎对字符串排序不正确