Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for backspace characters

  • Loading branch information...
commit 1d18db69cc641ef9c6a4dd0e096b156470eff0bf 1 parent e4f3b8f
@Seldaek Seldaek authored
Showing with 36 additions and 2 deletions.
  1. +28 −2 lib/ansiparse.js
  2. +8 −0 test/ansiparse-test.js
View
30 lib/ansiparse.js
@@ -7,7 +7,8 @@ ansiparse = function (str) {
matchingText = '',
ansiState = [],
result = [],
- state = {};
+ state = {},
+ eraseText;
//
// General workflow for this thing is:
@@ -20,6 +21,29 @@ ansiparse = function (str) {
// In further steps we hope it's all going to be fine. It usually is.
//
+ //
+ // Erases a char from the output
+ //
+ eraseChar = function () {
+ var index, text;
+ if (matchingText.length) {
+ matchingText = matchingText.substr(0, matchingText.length - 1);
+ }
+ else if (result.length) {
+ index = result.length - 1;
+ text = result[index].text;
+ if (text.length === 1) {
+ //
+ // A result bit was fully deleted, pop it out to simplify the final output
+ //
+ result.pop();
+ }
+ else {
+ result[index].text = text.substr(0, text.length - 1);
+ }
+ }
+ };
+
for (var i = 0; i < str.length; i++) {
if (matchingControl != null) {
if (matchingControl == '\033' && str[i] == '\[') {
@@ -111,7 +135,9 @@ ansiparse = function (str) {
if (str[i] == '\033') {
matchingControl = str[i];
-
+ }
+ else if (str[i] == '\u0008') {
+ eraseChar();
}
else {
matchingText += str[i];
View
8 test/ansiparse-test.js
@@ -62,6 +62,14 @@ var dataSets = {
'malformed control sequence': {
input: '\033A string between two ESC\033',
output: [ { text: '\033A string between two ESC\033' } ]
+ },
+ 'simple backspace': {
+ input: 'hello '.green + 'worz\bld'.red,
+ output: [{ text: 'hello ', foreground: 'green' }, { text: 'world', foreground: 'red' }]
+ },
+ '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' }]
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.