Skip to content

Commit

Permalink
fix from_ansi and add option --ansi -a #670
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jul 6, 2021
1 parent 56d8c31 commit 561d2b3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## 2.27.1
### Bugfix
* fix `from_ansi` and add option `--ansi -a`

## 2.27.0
### Feature
* add CSS and option `ansi` to echo to properly render ANSI art
* add typing animation to set_prompt [#673](https://github.com/jcubic/jquery.terminal/issues/673)
* return promise from `set_prompt` and `echo` when using typing animation
* add `from_ansi` executable [#670](https://github.com/jcubic/jquery.terminal/issues/670)
* new emoji
### Bugfix
* fix bug in cache (when echo same string two times in a row) [#672](https://github.com/jcubic/jquery.terminal/issues/672)
Expand Down
44 changes: 27 additions & 17 deletions bin/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ansi = require('ansidec');
const iconv = require('iconv-lite');
const lily = require('@jcubic/lily');

const options = lily(process.argv.slice(2));
const options = lily(process.argv.slice(2), { boolean: ['a', 'ansi'] });

function read_stdin() {
return new Promise((resolve) => {
Expand All @@ -42,9 +42,10 @@ 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>
console.log(`usage:\n\t${bin} [--help] [-h] [--input] [-i] <file> [--output] [-o] <file> [-a] [--ansi]
--input -i <file> input ANSI art file
--ansi -a if this flag is set it will read file or STDIN as ANSI Art (CP437 encoding)
--input -i <file> input ANSI file
--output -o <file> output jQuery Terminal formatting file
If no input specified it will read from STDIN
Expand All @@ -65,14 +66,20 @@ function process_buffer(buff) {
}

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;
if (options.ansi || options.a) {
var meta = ansi.meta(buff);
let cols = 80;
if (meta) {
buff = buff.slice(0, meta.fileSize);
cols = meta.tInfo[0];
}
text = iconv.decode(buff, 'CP437');
return format_lines(text, cols);
} else {
text = buff.toString();
return format_lines(text);
}
var text = iconv.decode(buff, 'CP437');
return format_lines(text, cols).join('\n');
}

function format_lines(str, len) {
Expand All @@ -81,12 +88,15 @@ function format_lines(str, len) {
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();
var lines;
if (len) {
var lines = $.terminal.split_equal(str, len);
// unix formatting don't handle \r\n at the end
if (lines[lines.length - 1] === '') {
lines.pop();
}
return lines.join('\n');
} else {
return str;
}
return lines;
}


26 changes: 24 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@
}
},
"dependencies": {
"@jcubic/lily": "^0.1.0",
"@types/jquery": "^3.5.5",
"ansidec": "^0.3.4",
"jquery": "^3.6.0",
"prismjs": "^1.24.0",
"wcwidth": "^1.0.1"
Expand Down
2 changes: 2 additions & 0 deletions templates/package.in
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@
}
},
"dependencies": {
"@jcubic/lily": "^0.1.0",
"@types/jquery": "^3.5.5",
"ansidec": "^0.3.4",
"jquery": "^3.6.0",
"prismjs": "^1.24.0",
"wcwidth": "^1.0.1"
Expand Down

0 comments on commit 561d2b3

Please sign in to comment.