Skip to content

Commit

Permalink
executable for formatting ANSI escapes jcubic#670
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jul 5, 2021
1 parent 4079c83 commit a65b86a
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 24 deletions.
92 changes: 92 additions & 0 deletions bin/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env node

const $ = global.$ = global.jQuery = {
fn: {
extend: function(obj) {
Object.assign(global.jQuery.fn, obj);
}
},
extend: Object.assign
};

global.navigator = {
userAgent: 'Node'
};

require('../js/jquery.terminal-src')(global, global.$);
require('../js/unix_formatting')(global, global.$);

const fs = require('fs').promises;
const path = require('path');
const ansi = require('ansidec');
const iconv = require('iconv-lite');
const lily = require('@jcubic/lily');

const options = lily(process.argv.slice(2));

function read_stdin() {
return new Promise((resolve) => {
const buff = [];

process.stdin.on('data', data => {
buff.push(data);
}).on('end', () => {
var len = buff.map(x => x.length).reduce((acc, e) => acc + e);
resolve(Buffer.concat(buff, len));
});
});
}

const input = options.i || options.input;
const output = options.o || options.output;

if (options.h || options.help) {
const bin = path.basename(process.argv[1]);
console.log(`usage:\n\t${bin} [--help] [-h] [--input] [-i] <file> [--output] [-o] <file>
--input -i <file> input ANSI art file
--output -o <file> output jQuery Terminal formatting file
If no input specified it will read from STDIN
If no output specified it will print to STDOUT`);
} else if (input) {
fs.readFile(input).then(process_buffer);
} else {
read_stdin().then(process_buffer);
}

function process_buffer(buff) {
var text = format(buff);
if (output) {
fs.writeFile(output, text);
} else {
console.log(text);
}
}

function format(buff) {
var meta = ansi.meta(buff);
let cols = 80;
if (meta) {
buff = buff.slice(0, meta.fileSize);
cols = meta.tInfo[0];
}
var text = iconv.decode(buff, 'CP437');
return format_lines(text, cols).join('\n');
}

function format_lines(str, len) {
str = $.terminal.apply_formatters(str, {
unixFormatting: {
ansiArt: true
}
});
var lines = $.terminal.split_equal(str, len || 80);
// unix formatting don't handle \r\n at the end
if (lines[lines.length - 1] === '') {
lines.pop();
}
return lines;
}


53 changes: 31 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
"prompt",
"console",
"keyboard",
"linux",
"unix",
"bash",
"typing",
"type",
"rpc",
"input",
"ui"
],
"bin": {
"from-ansi": "./bin/convert.js"
},
"scripts": {
"test": "make test"
},
"author": {
"name": "Jakub Jankiewicz",
"name": "Jakub T. Jankiewicz",
"email": "[email protected]",
"url": "https://jcubic.pl/me"
},
Expand Down
9 changes: 8 additions & 1 deletion templates/package.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
"prompt",
"console",
"keyboard",
"linux",
"unix",
"bash",
"typing",
"type",
"rpc",
"input",
"ui"
],
"bin": {
"from-ansi": "./bin/convert.js"
},
"scripts": {
"test": "make test"
},
"author": {
"name": "Jakub Jankiewicz",
"name": "Jakub T. Jankiewicz",
"email": "[email protected]",
"url": "https://jcubic.pl/me"
},
Expand Down

0 comments on commit a65b86a

Please sign in to comment.