Added decorators for output text

This commit is contained in:
Mike Cifelli 2017-02-11 13:33:34 -05:00
parent ce1547d71a
commit b3ae5c9122
9 changed files with 141 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package environment; package environment;
import java.io.*; import java.io.*;
import java.util.function.Function;
import error.ErrorManager; import error.ErrorManager;
@ -16,6 +17,12 @@ public class RuntimeEnvironment {
private InputStream input; private InputStream input;
private PrintStream output; private PrintStream output;
private PrintStream errorOutput; private PrintStream errorOutput;
private Function<String, String> outputDecorator;
private Function<String, String> valueOutputDecorator;
private Function<String, String> warningOutputDecorator;
private Function<String, String> errorOutputDecorator;
private Function<String, String> criticalOutputDecorator;
private Runnable terminationFunction; private Runnable terminationFunction;
private Runnable errorTerminationFunction; private Runnable errorTerminationFunction;
private ErrorManager errorManager; private ErrorManager errorManager;
@ -50,6 +57,26 @@ public class RuntimeEnvironment {
this.errorManager = errorManager; this.errorManager = errorManager;
} }
public void setOutputDecorator(Function<String, String> outputDecorator) {
this.outputDecorator = outputDecorator;
}
public void setValueOutputDecorator(Function<String, String> valueOutputDecorator) {
this.valueOutputDecorator = valueOutputDecorator;
}
public void setWarningOutputDecorator(Function<String, String> warningOutputDecorator) {
this.warningOutputDecorator = warningOutputDecorator;
}
public void setErrorOutputDecorator(Function<String, String> errorOutputDecorator) {
this.errorOutputDecorator = errorOutputDecorator;
}
public void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator) {
this.criticalOutputDecorator = criticalOutputDecorator;
}
public String getInputName() { public String getInputName() {
return inputName; return inputName;
} }
@ -78,4 +105,24 @@ public class RuntimeEnvironment {
return errorManager; return errorManager;
} }
public String decorateOutput(String output) {
return outputDecorator.apply(output);
}
public String decorateValueOutput(String valueOutput) {
return valueOutputDecorator.apply(valueOutput);
}
public String decorateWarningOutput(String warningOutput) {
return warningOutputDecorator.apply(warningOutput);
}
public String decorateErrorOutput(String errorOutput) {
return errorOutputDecorator.apply(errorOutput);
}
public String decorateCriticalOutput(String criticalOutput) {
return criticalOutputDecorator.apply(criticalOutput);
}
} }

View File

@ -12,11 +12,6 @@ import environment.RuntimeEnvironment;
*/ */
public class ErrorManager { public class ErrorManager {
private static final String ANSI_RESET = "\u001B[0m";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_PURPLE = "\u001B[35m";
private static final String ANSI_YELLOW = "\u001B[33m";
private RuntimeEnvironment environment; private RuntimeEnvironment environment;
public ErrorManager() { public ErrorManager() {
@ -44,10 +39,10 @@ public class ErrorManager {
private String formatMessage(LispException lispException) { private String formatMessage(LispException lispException) {
Severity severity = lispException.getSeverity(); Severity severity = lispException.getSeverity();
String color = severity.getMessageColor();
String prefix = severity.toDisplayString(); String prefix = severity.toDisplayString();
String message = MessageFormat.format("{0}: {1}", prefix, lispException.getMessage());
return MessageFormat.format("{0}{1}: {2}{3}", color, prefix, lispException.getMessage(), ANSI_RESET); return severity.decorate(message, environment);
} }
private boolean isCritical(LispException lispException) { private boolean isCritical(LispException lispException) {
@ -57,8 +52,8 @@ public class ErrorManager {
public static enum Severity { public static enum Severity {
WARNING { WARNING {
public String getMessageColor() { public String decorate(String warningOutput, RuntimeEnvironment environment) {
return ANSI_YELLOW; return environment.decorateWarningOutput(warningOutput);
} }
public String toDisplayString() { public String toDisplayString() {
@ -67,8 +62,8 @@ public class ErrorManager {
}, },
ERROR { ERROR {
public String getMessageColor() { public String decorate(String errorOutput, RuntimeEnvironment environment) {
return ANSI_RED; return environment.decorateErrorOutput(errorOutput);
} }
public String toDisplayString() { public String toDisplayString() {
@ -77,8 +72,8 @@ public class ErrorManager {
}, },
CRITICAL { CRITICAL {
public String getMessageColor() { public String decorate(String criticalOutput, RuntimeEnvironment environment) {
return ANSI_PURPLE; return environment.decorateCriticalOutput(criticalOutput);
} }
public String toDisplayString() { public String toDisplayString() {
@ -86,7 +81,7 @@ public class ErrorManager {
} }
}; };
public abstract String getMessageColor(); public abstract String decorate(String output, RuntimeEnvironment environment);
public abstract String toDisplayString(); public abstract String toDisplayString();
} }

View File

@ -1,7 +1,5 @@
package interpreter; package interpreter;
import java.text.MessageFormat;
import environment.RuntimeEnvironment; import environment.RuntimeEnvironment;
import error.*; import error.*;
import function.builtin.EVAL; import function.builtin.EVAL;
@ -10,9 +8,6 @@ import sexpression.SExpression;
public class LispInterpreter { public class LispInterpreter {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
protected RuntimeEnvironment environment; protected RuntimeEnvironment environment;
protected ErrorManager errorManager; protected ErrorManager errorManager;
private LispParser parser; private LispParser parser;
@ -47,7 +42,7 @@ public class LispInterpreter {
private void printValueOfNextSExpressionWithException() { private void printValueOfNextSExpressionWithException() {
SExpression sExpression = parser.getNextSExpression(); SExpression sExpression = parser.getNextSExpression();
String result = MessageFormat.format("{0}{1}{2}", ANSI_GREEN, EVAL.eval(sExpression), ANSI_RESET); String result = environment.decorateValueOutput(String.valueOf(EVAL.eval(sExpression)));
erasePrompt(); erasePrompt();
environment.getOutput().println(result); environment.getOutput().println(result);

View File

@ -1,6 +1,7 @@
package interpreter; package interpreter;
import java.io.*; import java.io.*;
import java.util.function.Function;
public interface LispInterpreterBuilder { public interface LispInterpreterBuilder {
@ -18,6 +19,16 @@ public interface LispInterpreterBuilder {
void useFile(String fileName); void useFile(String fileName);
void setOutputDecorator(Function<String, String> outputDecorator);
void setValueOutputDecorator(Function<String, String> valueOutputDecorator);
void setWarningOutputDecorator(Function<String, String> warningOutputDecorator);
void setErrorOutputDecorator(Function<String, String> errorOutputDecorator);
void setCriticalOutputDecorator(Function<String, String> criticalOutputDecorator);
LispInterpreter build(); LispInterpreter build();
} }

View File

@ -1,6 +1,7 @@
package interpreter; package interpreter;
import java.io.*; import java.io.*;
import java.util.function.Function;
import environment.RuntimeEnvironment; import environment.RuntimeEnvironment;
import error.*; import error.*;
@ -19,6 +20,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
private PrintStream errorOutputStream; private PrintStream errorOutputStream;
private Runnable terminationFunction; private Runnable terminationFunction;
private Runnable errorTerminationFunction; private Runnable errorTerminationFunction;
private Function<String, String> outputDecorator;
private Function<String, String> valueOutputDecorator;
private Function<String, String> warningOutputDecorator;
private Function<String, String> errorOutputDecorator;
private Function<String, String> criticalOutputDecorator;
private RuntimeEnvironment environment; private RuntimeEnvironment environment;
private boolean isInteractive; private boolean isInteractive;
private boolean isBuilt; private boolean isBuilt;
@ -28,6 +34,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
this.inputName = ""; this.inputName = "";
this.isInteractive = true; this.isInteractive = true;
this.isBuilt = false; this.isBuilt = false;
this.outputDecorator = s -> s;
this.valueOutputDecorator = s -> s;
this.warningOutputDecorator = s -> s;
this.errorOutputDecorator = s -> s;
this.criticalOutputDecorator = s -> s;
} }
@Override @Override
@ -67,6 +78,33 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
this.isInteractive = false; this.isInteractive = false;
} }
@Override
public void setOutputDecorator(Function<String, String> decorator) {
this.outputDecorator = decorator;
}
@Override
public void setValueOutputDecorator(Function<String, String> decorator) {
this.valueOutputDecorator = decorator;
}
@Override
public void setWarningOutputDecorator(Function<String, String> decorator) {
this.warningOutputDecorator = decorator;
}
@Override
public void setErrorOutputDecorator(Function<String, String> decorator) {
this.errorOutputDecorator = decorator;
}
@Override
public void setCriticalOutputDecorator(Function<String, String> decorator) {
this.criticalOutputDecorator = decorator;
}
@Override @Override
public LispInterpreter build() { public LispInterpreter build() {
if (!isBuilt) if (!isBuilt)
@ -91,6 +129,11 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder {
environment.setTerminationFunction(terminationFunction); environment.setTerminationFunction(terminationFunction);
environment.setErrorTerminationFunction(errorTerminationFunction); environment.setErrorTerminationFunction(errorTerminationFunction);
environment.setErrorManager(errorManager); environment.setErrorManager(errorManager);
environment.setOutputDecorator(outputDecorator);
environment.setValueOutputDecorator(valueOutputDecorator);
environment.setWarningOutputDecorator(warningOutputDecorator);
environment.setErrorOutputDecorator(errorOutputDecorator);
environment.setCriticalOutputDecorator(criticalOutputDecorator);
configureInput(errorManager); configureInput(errorManager);
} }

View File

@ -1,9 +1,17 @@
package main; package main;
import java.util.function.Function;
import interpreter.*; import interpreter.*;
public class LispMain { public class LispMain {
private static final String ANSI_RESET = "\u001B[0m";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_GREEN = "\u001B[32m";
private static final String ANSI_YELLOW = "\u001B[33m";
private static final String ANSI_PURPLE = "\u001B[35m";
private LispMain() {} private LispMain() {}
public static void main(String[] args) { public static void main(String[] args) {
@ -13,11 +21,15 @@ public class LispMain {
private static LispInterpreter buildInterpreter(String[] args) { private static LispInterpreter buildInterpreter(String[] args) {
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance(); LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
configureInput(args, builder);
builder.setOutput(System.out); builder.setOutput(System.out);
builder.setErrorOutput(System.err); builder.setErrorOutput(System.err);
builder.setTerminationFunction(() -> System.exit(0)); builder.setTerminationFunction(() -> System.exit(0));
builder.setErrorTerminationFunction(() -> System.exit(1)); builder.setErrorTerminationFunction(() -> System.exit(1));
configureInput(args, builder); builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));
return builder.build(); return builder.build();
} }
@ -31,4 +43,14 @@ public class LispMain {
} }
} }
private static Function<String, String> makeColorDecorator(String color) {
return new Function<String, String>() {
@Override
public String apply(String s) {
return color + s + ANSI_RESET;
}
};
}
} }

View File

@ -29,6 +29,9 @@ public class ErrorManagerTester {
environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED)); environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED));
environment.setErrorOutput(new PrintStream(errorOutputStream)); environment.setErrorOutput(new PrintStream(errorOutputStream));
environment.setOutput(new PrintStream(outputStream)); environment.setOutput(new PrintStream(outputStream));
environment.setWarningOutputDecorator(s -> s);
environment.setErrorOutputDecorator(s -> s);
environment.setCriticalOutputDecorator(s -> s);
return new ErrorManager(); return new ErrorManager();
} }

View File

@ -45,6 +45,9 @@ public class LOADTester {
environment.setOutput(new PrintStream(outputStream)); environment.setOutput(new PrintStream(outputStream));
environment.setErrorOutput(new PrintStream(errorOutputStream)); environment.setErrorOutput(new PrintStream(errorOutputStream));
environment.setErrorManager(new ErrorManager()); environment.setErrorManager(new ErrorManager());
environment.setOutputDecorator(s -> s);
environment.setWarningOutputDecorator(s -> s);
environment.setErrorOutputDecorator(s -> s);
} }
@Test @Test

View File

@ -31,6 +31,7 @@ public class DEFUNTester {
environment.setOutput(new PrintStream(outputStream)); environment.setOutput(new PrintStream(outputStream));
environment.setErrorManager(new ErrorManager()); environment.setErrorManager(new ErrorManager());
environment.setWarningOutputDecorator(s -> s);
FunctionTable.reset(); FunctionTable.reset();
} }