Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add line clearing

This is WIP, since there's a bug that causes the code to split
newlines into different result objects. The tests fail, so you can
see the bugs in the test result.
  • Loading branch information...
commit 46339033759ad2deec5ffb5fe9eadcd4cc557388 1 parent 185d842
@henrikhodne authored
Showing with 33 additions and 0 deletions.
  1. +25 −0 lib/ansiparse.js
  2. +8 −0 test/ansiparse-test.js
View
25 lib/ansiparse.js
@@ -44,6 +44,18 @@ ansiparse = function (str) {
}
};
+ lastChar = function () {
+ var index, text;
+ if (matchingText.length) {
+ return matchingText.substr(-1);
+ }
+ else if (result.length) {
+ index = result.length - 1;
+ text = result[index].text;
+ return text.substr(-1);
+ }
+ };
+
for (var i = 0; i < str.length; i++) {
if (matchingControl != null) {
if (matchingControl == '\033' && str[i] == '\[') {
@@ -127,6 +139,19 @@ ansiparse = function (str) {
});
ansiState = [];
}
+ else if (str[i] == 'K') {
+ //
+ // `K` finished line clearing
+ //
+ if (matchingData == '1') {
+ while ((result.length > 0 || matchingText.length > 0) && lastChar() != '\n') eraseChar();
+ } else {
+ while (str[i+1] != '\n' && str[i+1] != null) i++;
+ }
+
+ matchingData = null;
+ matchingControl = null;
+ }
else {
matchingData += str[i];
}
View
8 test/ansiparse-test.js
@@ -70,6 +70,14 @@ var dataSets = {
'backspace across blocks': {
input: 'hello'.green + ' ' + 'worz\b\b\b\b\bl \b\b\b\b'.red,
output: [{ text: 'hello', foreground: 'green' }, { text: 'l', foreground: 'red' }]
+ },
+ 'clear to beginning of line': {
+ input: 'hello\nthis will be removed\033\[1Kworld',
+ output: [{ text: 'hello\nworld'}]
+ },
+ 'clear to end of line': {
+ input: 'hello world\033\[0Kthis will be removed\nThis should still be here',
+ output: [{ text: 'hello world\nThis should still be here' }]
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.