package com.googlecode.lanterna.terminal;
import com.googlecode.lanterna.SGR;
import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.graphics.TextGraphics;
import com.googlecode.lanterna.input.KeyStroke;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class IOSafeTerminalAdapter implements IOSafeTerminal {
private interface ExceptionHandler {
void onException(IOException e);
}
private static class ConvertToRuntimeException implements ExceptionHandler {
@Override
public void onException(IOException e) {
throw new RuntimeException(e);
}
}
private static class DoNothingAndOrReturnNull implements ExceptionHandler {
@Override
public void onException(IOException e) { }
}
public static IOSafeTerminal createRuntimeExceptionConvertingAdapter(Terminal terminal) {
if (terminal instanceof ExtendedTerminal) {
return createRuntimeExceptionConvertingAdapter((ExtendedTerminal)terminal);
} else {
return new IOSafeTerminalAdapter(terminal, new ConvertToRuntimeException());
}
}
public static IOSafeExtendedTerminal createRuntimeExceptionConvertingAdapter(ExtendedTerminal terminal) {
return new IOSafeTerminalAdapter.Extended(terminal, new ConvertToRuntimeException());
}
public static IOSafeTerminal createDoNothingOnExceptionAdapter(Terminal terminal) {
if (terminal instanceof ExtendedTerminal) {
return createDoNothingOnExceptionAdapter((ExtendedTerminal)terminal);
} else {
return new IOSafeTerminalAdapter(terminal, new DoNothingAndOrReturnNull());
}
}
public static IOSafeExtendedTerminal createDoNothingOnExceptionAdapter(ExtendedTerminal terminal) {
return new IOSafeTerminalAdapter.Extended(terminal, new DoNothingAndOrReturnNull());
}
private final Terminal backend;
final ExceptionHandler exceptionHandler;
@SuppressWarnings("WeakerAccess")
public IOSafeTerminalAdapter(Terminal backend, ExceptionHandler exceptionHandler) {
this.backend = backend;
this.exceptionHandler = exceptionHandler;
}
@Override
public void enterPrivateMode() {
try {
backend.enterPrivateMode();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void exitPrivateMode() {
try {
backend.exitPrivateMode();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void clearScreen() {
try {
backend.clearScreen();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setCursorPosition(int x, int y) {
try {
backend.setCursorPosition(x, y);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setCursorPosition(TerminalPosition position) {
try {
backend.setCursorPosition(position);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public TerminalPosition getCursorPosition() {
try {
return backend.getCursorPosition();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
@Override
public void setCursorVisible(boolean visible) {
try {
backend.setCursorVisible(visible);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void putCharacter(char c) {
try {
backend.putCharacter(c);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void putString(String string) {
try {
backend.putString(string);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public TextGraphics newTextGraphics() {
try {
return backend.newTextGraphics();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
@Override
public void enableSGR(SGR sgr) {
try {
backend.enableSGR(sgr);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void disableSGR(SGR sgr) {
try {
backend.disableSGR(sgr);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void resetColorAndSGR() {
try {
backend.resetColorAndSGR();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setForegroundColor(TextColor color) {
try {
backend.setForegroundColor(color);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setBackgroundColor(TextColor color) {
try {
backend.setBackgroundColor(color);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void addResizeListener(TerminalResizeListener listener) {
backend.addResizeListener(listener);
}
@Override
public void removeResizeListener(TerminalResizeListener listener) {
backend.removeResizeListener(listener);
}
@Override
public TerminalSize getTerminalSize() {
try {
return backend.getTerminalSize();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
@Override
public byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit) {
try {
return backend.enquireTerminal(timeout, timeoutUnit);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
@Override
public void bell() {
try {
backend.bell();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void flush() {
try {
backend.flush();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void close() {
try {
backend.close();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public KeyStroke pollInput() {
try {
return backend.pollInput();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
@Override
public KeyStroke readInput() {
try {
return backend.readInput();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
return null;
}
public static class Extended extends IOSafeTerminalAdapter implements IOSafeExtendedTerminal {
private final ExtendedTerminal backend;
public Extended(ExtendedTerminal backend, ExceptionHandler exceptionHandler) {
super(backend, exceptionHandler);
this.backend = backend;
}
@Override
public void setTerminalSize(int columns, int rows) {
try {
backend.setTerminalSize(columns, rows);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setTitle(String title) {
try {
backend.setTitle(title);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void pushTitle() {
try {
backend.pushTitle();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void popTitle() {
try {
backend.popTitle();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void iconify() {
try {
backend.iconify();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void deiconify() {
try {
backend.deiconify();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void maximize() {
try {
backend.maximize();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void unmaximize() {
try {
backend.unmaximize();
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void setMouseCaptureMode(MouseCaptureMode mouseCaptureMode) {
try {
backend.setMouseCaptureMode(mouseCaptureMode);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
@Override
public void scrollLines(int firstLine, int lastLine, int distance) {
try {
backend.scrollLines(firstLine, lastLine, distance);
}
catch(IOException e) {
exceptionHandler.onException(e);
}
}
}
}