2017-03-19 12:54:35 -04:00
|
|
|
package terminal;
|
|
|
|
|
2017-03-22 11:26:53 -04:00
|
|
|
import static java.lang.Character.isDigit;
|
|
|
|
import static terminal.ControlSequenceLookup.lookupControlSequence;
|
|
|
|
import static util.Characters.*;
|
|
|
|
|
|
|
|
import terminal.SafeStream.SafeInputStream;
|
2017-03-19 12:54:35 -04:00
|
|
|
|
|
|
|
class ControlSequenceHandler {
|
2017-03-20 11:18:36 -04:00
|
|
|
|
2017-03-22 11:26:53 -04:00
|
|
|
private static final char ESCAPE = '\u001B';
|
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
public static final boolean isEscape(char c) {
|
2017-03-22 11:26:53 -04:00
|
|
|
return c == ESCAPE;
|
2017-03-19 12:54:35 -04:00
|
|
|
}
|
2017-03-20 11:18:36 -04:00
|
|
|
|
2017-03-22 11:26:53 -04:00
|
|
|
private SafeInputStream input;
|
|
|
|
private String code;
|
|
|
|
private int currentCharacter;
|
2017-03-19 12:54:35 -04:00
|
|
|
|
|
|
|
public ControlSequenceHandler() {
|
2017-03-22 11:26:53 -04:00
|
|
|
this.input = null;
|
|
|
|
this.code = "";
|
|
|
|
this.currentCharacter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ControlSequence parse(SafeInputStream inputStream) {
|
|
|
|
input = inputStream;
|
|
|
|
code = "";
|
|
|
|
|
|
|
|
readCharacter();
|
|
|
|
if (isExpectedFirstCharacter())
|
|
|
|
readCode();
|
|
|
|
|
|
|
|
return lookupControlSequence((char) currentCharacter, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void readCharacter() {
|
|
|
|
currentCharacter = input.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isExpectedFirstCharacter() {
|
|
|
|
return isCharacter() && isLeftBracket();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isCharacter() {
|
|
|
|
return currentCharacter != EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isLeftBracket() {
|
|
|
|
return (char) currentCharacter == LEFT_SQUARE_BRACKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void readCode() {
|
|
|
|
for (readCharacter(); isPartOfCode(); readCharacter())
|
|
|
|
code += (char) currentCharacter;
|
2017-03-19 12:54:35 -04:00
|
|
|
}
|
2017-03-20 11:18:36 -04:00
|
|
|
|
2017-03-22 11:26:53 -04:00
|
|
|
private boolean isPartOfCode() {
|
|
|
|
return isCharacter() && isDigit((char) currentCharacter);
|
2017-03-19 12:54:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|