I'm building a simple terminal emulator using the pty4j library. My program has a print()
method that renders the text to a canvas using the GraphicsContext.fillText()
method from javafx. I connect the emulator to an instance of cmd and read ouptput from a buffered reader. Now sadly when it recieves text it also includes ANSI-escape characters (see image). However If i print the ouput to the IDE or system console it works fine.
I tried using the readLine()
method from the BufferedReader and then applying a regex, but because not all input recieved from the terminal is terminated by a \n
it blocks on the last line.
Thread terminalReaderThread = new Thread() {
public void run() {
try {
int c;
while (terminal.isRunning() && (c = terminal.getReader().read()) != -1) {
if(c != 0){
print(Character.toString((char)c));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
terminalReaderThread.start();
Is there an effective way to filter these escape codes from the inputStream?