Skip to content

Commit

Permalink
Fix ansi stream state after an exception is thrown, fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 1, 2020
1 parent c77ec6c commit 0926754
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/main/java/org/fusesource/jansi/AnsiOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void write(int data) throws IOException {
} else if ('=' == data) {
options.add('=');
} else {
reset(ap.processEscapeCommand(options, data));
processEscapeCommand(data);
}
break;
default:
Expand All @@ -143,7 +143,7 @@ public void write(int data) throws IOException {
if (data == ';') {
state = LOOKING_FOR_NEXT_ARG;
} else {
reset(ap.processEscapeCommand(options, data));
processEscapeCommand(data);
}
}
break;
Expand All @@ -156,7 +156,7 @@ public void write(int data) throws IOException {
if (data == ';') {
state = LOOKING_FOR_NEXT_ARG;
} else {
reset(ap.processEscapeCommand(options, data));
processEscapeCommand(data);
}
}
break;
Expand Down Expand Up @@ -192,7 +192,7 @@ public void write(int data) throws IOException {
if (BEL == data) {
String value = new String(buffer, startOfValue, (pos - 1) - startOfValue, cs);
options.add(value);
reset(ap.processOperatingSystemCommand(options));
processOperatingSystemCommand();
} else if (FIRST_ESC_CHAR == data) {
state = LOOKING_FOR_ST;
} else {
Expand All @@ -205,15 +205,15 @@ public void write(int data) throws IOException {
if (SECOND_ST_CHAR == data) {
String value = new String(buffer, startOfValue, (pos - 2) - startOfValue, cs);
options.add(value);
reset(ap.processOperatingSystemCommand(options));
processOperatingSystemCommand();
} else {
state = LOOKING_FOR_OSC_PARAM;
}
break;

case LOOKING_FOR_CHARSET:
options.add((char) data);
reset(ap.processCharsetSelect(options));
processCharsetSelect();
break;
}

Expand All @@ -223,6 +223,33 @@ public void write(int data) throws IOException {
}
}

private void processCharsetSelect() throws IOException {
try {
reset(ap.processCharsetSelect(options));
} catch (RuntimeException e) {
reset(true);
throw e;
}
}

private void processOperatingSystemCommand() throws IOException {
try {
reset(ap.processOperatingSystemCommand(options));
} catch (RuntimeException e) {
reset(true);
throw e;
}
}

private void processEscapeCommand(int data) throws IOException {
try {
reset(ap.processEscapeCommand(options, data));
} catch (RuntimeException e) {
reset(true);
throw e;
}
}

/**
* Resets all state to continue with regular parsing
* @param skipBuffer if current buffer should be skipped or written to out
Expand Down

0 comments on commit 0926754

Please sign in to comment.