Skip to content

Commit

Permalink
Merge pull request #20 from fetus-hina/plain
Browse files Browse the repository at this point in the history
Add ansi_to_text method
  • Loading branch information
drudru committed Jun 7, 2015
2 parents 1246b37 + 2ae883d commit 9290483
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ansi_up.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,29 @@
};

Ansi_Up.prototype.ansi_to_html = function (txt, options) {
var self = this;
return this.process(txt, options, true);
};

Ansi_Up.prototype.ansi_to_text = function (txt) {
var options = {};
return this.process(txt, options, false);
};

Ansi_Up.prototype.process = function (txt, options, markup) {
var self = this;
var raw_text_chunks = txt.split(/\033\[/);
var first_chunk = raw_text_chunks.shift(); // the first chunk is not the result of the split

var color_chunks = raw_text_chunks.map(function (chunk) {
return self.process_chunk(chunk, options);
return self.process_chunk(chunk, options, markup);
});

color_chunks.unshift(first_chunk);

return color_chunks.join('');
};

Ansi_Up.prototype.process_chunk = function (text, options) {
Ansi_Up.prototype.process_chunk = function (text, options, markup) {

// Are we using classes or styles?
options = typeof options == 'undefined' ? {} : options;
Expand Down Expand Up @@ -144,6 +152,10 @@
return orig_txt;
}

if (!markup) {
return orig_txt;
}

var self = this;

while (nums.length > 0) {
Expand Down Expand Up @@ -284,6 +296,11 @@
return a2h.ansi_to_html(txt, options);
},

ansi_to_text: function (txt) {
var a2h = new Ansi_Up();
return a2h.ansi_to_text(txt);
},

ansi_to_html_obj: function () {
return new Ansi_Up();
}
Expand Down
17 changes: 17 additions & 0 deletions test/ansi_up-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,22 @@ describe('ansi_up', function() {
});
});
});
describe('ansi to text', function() {
it('should remove color sequence', function() {
var start = "foo \033[1;32mbar\033[0m baz";
var l = ansi_up.ansi_to_text(start);
l.should.eql("foo bar baz");
});
it('should remove unsupported sequence', function() {
var start = "foo \033[1Abar";
var l = ansi_up.ansi_to_text(start);
l.should.eql('foo bar');
});
it('should keep multiline', function() {
var start = "foo \033[1;32mbar\nbaz\033[0m qux";
var l = ansi_up.ansi_to_text(start);
l.should.eql("foo bar\nbaz qux");
});
});
});

0 comments on commit 9290483

Please sign in to comment.