package terminal; import static java.lang.Character.isDigit; import static terminal.ControlSequenceLookup.lookupControlSequence; import static util.Characters.*; import terminal.SafeStream.SafeInputStream; class ControlSequenceHandler { private static final char ESCAPE = '\u001B'; public static final boolean isEscape(char c) { return c == ESCAPE; } private SafeInputStream input; private String code; private int currentCharacter; public ControlSequenceHandler() { 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; } private boolean isPartOfCode() { return isCharacter() && isDigit((char) currentCharacter); } }