Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add line clearing #7

Closed
wants to merge 3 commits into from

2 participants

@henrikhodne

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.

Any suggestions on how to fix the bug?

@henrikhodne henrikhodne 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.
4633903
@henrikhodne

Figured out the error, pushing now code in a bit.

henrikhodne added some commits
@henrikhodne henrikhodne Fix bug with line clearing
Whenever a control sequence is reached, a new result is created.
However, for line clearing, we don't necessarily want this, so let's
undo it.
5707b44
@henrikhodne henrikhodne Add ability to clear entire line 71a9712
@henrikhodne henrikhodne referenced this pull request
Open

Erase line #4

@henrikhodne

So, while this works, the behaviour is actually not completely right. The cursor position shouldn't change when \e[K is used, but the script as it behaves now doesn't store the cursor position, so this would require a big change, and I don't know how to add that to the script.

@mmalecki mmalecki referenced this pull request in travis-ci/travis-ci
Closed

Some weird ANSI codes could look better #386

@Mithgol

@mmalecki What would happen to logs on Travis (travis-ci/travis-ci#386) if this pull request is accepted “as is”?

@henrikhodne

@Mithgol Nothing, really, as I don't think we're using ansiparse anymore.

@henrikhodne henrikhodne deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2012
  1. @henrikhodne

    Add line clearing

    henrikhodne authored
    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.
  2. @henrikhodne

    Fix bug with line clearing

    henrikhodne authored
    Whenever a control sequence is reached, a new result is created.
    However, for line clearing, we don't necessarily want this, so let's
    undo it.
  3. @henrikhodne
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 0 deletions.
  1. +34 −0 lib/ansiparse.js
  2. +12 −0 test/ansiparse-test.js
View
34 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,28 @@ ansiparse = function (str) {
});
ansiState = [];
}
+ else if (str[i] == 'K') {
+ //
+ // `K` finished line clearing
+ //
+
+ // Undo emitting the last text
+ state = result.pop();
+ matchingText = state.text;
+ delete state.text;
+
+ if (matchingData == '1') {
+ while ((result.length > 0 || matchingText.length > 0) && lastChar() != '\n') eraseChar();
+ } else if (matchingData == '2') {
+ while ((result.length > 0 || matchingText.length > 0) && lastChar() != '\n') eraseChar();
+ while (str[i+1] != '\n' && str[i+1] != null) i++;
+ } else {
+ while (str[i+1] != '\n' && str[i+1] != null) i++;
+ }
+
+ matchingData = null;
+ matchingControl = null;
+ }
else {
matchingData += str[i];
}
View
12 test/ansiparse-test.js
@@ -70,6 +70,18 @@ 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' }]
+ },
+ 'clear entire line': {
+ input: 'hello world\nthis entire\033[2Kline will be removed\nbut not this one',
+ output: [{ text: 'hello world\n\nbut not this one' }]
}
};
Something went wrong with that request. Please try again.