If you want just the ANSI cursor movement you can easily create an interpreter using NPM module node-ansiparser. I used the same module for my JavaScript library: jQuery Terminal, where I parse and process ANSI escapes and overtyping (output of man command).
You can take a look at how I used the parser on GitHub unix_formatting.js (the file includes the ansi-parser). You can try to modify the code to only process text and ignore the colors (the jQuery Terminal formatting syntax). It should not be that hard.
If you want something quick you can create a script that will just process the file and strip the formatting that is used by jQuery Terminal. Here is a quick hack that does the trick (but you probably be better modifying the unix_formatting.js file and remove everything that is not needed).
To start with jQuery Terminal use this:
mkdir ansi-text
cd ansi-text
npm init -y # you need nodeJS installed
npm install jquery.terminal
then create this file in same directory:
ansi-text.js
#!/usr/bin/env node
// mock jQuery that is not actually required
const $ = global.$ = global.jQuery = {
fn: {
extend: function(obj) {
Object.assign(global.jQuery.fn, obj);
}
},
extend: Object.assign
};
global.navigator = {
userAgent: 'Node'
};
require('jquery.terminal')(global, global.$);
require('jquery.terminal/js/unix_formatting')(global, global.$);
read_stdin().then(function(buff) {
const str = buff.toString();
const formatted = $.terminal.apply_formatters(str);
console.log($.terminal.strip(formatted));
});
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));
});
});
}
You can then create testing script:
test.sh
tput cup 2 3;
echo HELLO;
tput cup 5 20;
echo WORLD;
And you can see that it works using:
unbuffer ./test.sh | ./ansi-text.js
Output:
kuba@jcubic:~/projects/jcubic/ansi-text$ unbuffer test.sh | ./ansi-text.js
HELLO
WORLD
kuba@jcubic:~/projects/jcubic/ansi-text$
unbuffer will make the script think that it's TTY (it's part of expect).
The JS code doesn't handle the width of the terminal but it can be easily added after you transform ANSI escapes. To split the output use: $.terminal.split_equal(string, cols)
but you probably can use something simpler since you don't need to handle jQuery Terminal formatting, that is the main purpose of the function.