diff --git a/src/org/rascalmpl/debug/IRascalMonitor.java b/src/org/rascalmpl/debug/IRascalMonitor.java index 3a885f00007..2c653dd5e5d 100644 --- a/src/org/rascalmpl/debug/IRascalMonitor.java +++ b/src/org/rascalmpl/debug/IRascalMonitor.java @@ -12,10 +12,18 @@ *******************************************************************************/ package org.rascalmpl.debug; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.function.Function; import java.util.function.Supplier; +import org.rascalmpl.interpreter.ConsoleRascalMonitor; +import org.rascalmpl.interpreter.NullRascalMonitor; +import org.rascalmpl.repl.TerminalProgressBarMonitor; + import io.usethesource.vallang.ISourceLocation; +import jline.TerminalFactory; public interface IRascalMonitor { /** @@ -93,4 +101,25 @@ default void jobStep(String name, String message) { * Inform (about a warning */ public void warning(String message, ISourceLocation src); + + /** + * Convenience method will produce a monitor with ANSI progress bars if possible, + * and otherwise default to a dumn terminal console progress logger. + * @return + */ + @SuppressWarnings("unchecked") + public static T buildConsoleMonitor(InputStream in, OutputStream out) { + return (T) ((System.console() != null) + ? new TerminalProgressBarMonitor(out, in, TerminalFactory.get()) + : new ConsoleRascalMonitor(new PrintStream(out)) + ); + } + + /** + * Convenience method will produce a monitor that eats up all events without logging + * or reporting + */ + public default IRascalMonitor buildNullMonitor() { + return new NullRascalMonitor(); + } } diff --git a/src/org/rascalmpl/ideservices/BasicIDEServices.java b/src/org/rascalmpl/ideservices/BasicIDEServices.java index b59ab0cc079..f799fdecb4a 100644 --- a/src/org/rascalmpl/ideservices/BasicIDEServices.java +++ b/src/org/rascalmpl/ideservices/BasicIDEServices.java @@ -15,14 +15,12 @@ import java.awt.Desktop; import java.io.File; import java.io.IOException; -import java.io.OutputStream; import java.io.PrintWriter; import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; import org.rascalmpl.debug.IRascalMonitor; -import org.rascalmpl.repl.TerminalProgressBarMonitor; import org.rascalmpl.uri.URIResolverRegistry; import org.rascalmpl.uri.URIUtil; diff --git a/src/org/rascalmpl/interpreter/Evaluator.java b/src/org/rascalmpl/interpreter/Evaluator.java index a5f3b47e213..8c7d75acbb3 100755 --- a/src/org/rascalmpl/interpreter/Evaluator.java +++ b/src/org/rascalmpl/interpreter/Evaluator.java @@ -230,6 +230,14 @@ public void decCallNesting() { private static final Object dummy = new Object(); + /** + * Promotes the monitor to the outputstream automatically if so required. + */ + public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, IRascalMonitor monitor, ModuleEnvironment scope, GlobalEnvironment heap) { + this(f, input, stderr, monitor instanceof OutputStream ? (OutputStream) monitor : stdout, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); + setMonitor(monitor); + } + public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap) { this(f, input, stderr, stdout, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); } @@ -304,6 +312,20 @@ private Evaluator(Evaluator source, ModuleEnvironment scope) { setEventTrigger(AbstractInterpreterEventTrigger.newNullEventTrigger()); } + /** + * This constructor detects if we are running a terminal or not and changes streams and monitors + * accordingly + * @param valueFactory + * @param root + * @param heap2 + */ + public Evaluator(IValueFactory vf, ModuleEnvironment root, GlobalEnvironment heap) { + this(vf, System.in, System.err, IRascalMonitor.buildConsoleMonitor(System.in, System.out), root, heap); + if (defStdout instanceof TerminalProgressBarMonitor) { + setMonitor((TerminalProgressBarMonitor) defStdout); + } + } + public void resetJavaBridge() { this.javaBridge = new JavaBridge(classLoaders, vf, config); } @@ -317,6 +339,7 @@ public IRascalMonitor setMonitor(IRascalMonitor monitor) { interrupt = false; IRascalMonitor old = this.monitor; this.monitor = monitor; + return old; } diff --git a/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java b/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java index e5baa57ed85..4d4749bb09a 100644 --- a/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java +++ b/src/org/rascalmpl/repl/TerminalProgressBarMonitor.java @@ -6,14 +6,12 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.time.Duration; import java.time.Instant; import java.util.LinkedList; import java.util.List; -import org.fusesource.jansi.Ansi; import org.rascalmpl.debug.IRascalMonitor; import io.usethesource.vallang.ISourceLocation; import jline.Terminal; diff --git a/src/org/rascalmpl/shell/REPLRunner.java b/src/org/rascalmpl/shell/REPLRunner.java index cfa589f3cdc..414201c56c7 100755 --- a/src/org/rascalmpl/shell/REPLRunner.java +++ b/src/org/rascalmpl/shell/REPLRunner.java @@ -8,7 +8,6 @@ import java.net.URISyntaxException; import java.util.Map; -import org.rascalmpl.ideservices.BasicIDEServices; import org.rascalmpl.ideservices.IDEServices; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.repl.BaseREPL; diff --git a/src/org/rascalmpl/shell/RascalShell.java b/src/org/rascalmpl/shell/RascalShell.java index f244da5df6e..c03b16fe7f6 100644 --- a/src/org/rascalmpl/shell/RascalShell.java +++ b/src/org/rascalmpl/shell/RascalShell.java @@ -21,10 +21,7 @@ import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.ideservices.BasicIDEServices; import org.rascalmpl.ideservices.IDEServices; -import org.rascalmpl.interpreter.ConsoleRascalMonitor; import org.rascalmpl.interpreter.utils.RascalManifest; -import org.rascalmpl.repl.TerminalProgressBarMonitor; - import jline.Terminal; import jline.TerminalFactory; import jline.TerminalSupport; @@ -68,13 +65,10 @@ public static void main(String[] args) throws IOException { term = new EclipseTerminalConnection(term, Integer.parseInt(sneakyRepl)); } - IRascalMonitor monitor - = System.console() != null - ? new TerminalProgressBarMonitor(System.out, System.in, term) - : new ConsoleRascalMonitor(); + IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out); IDEServices services = new BasicIDEServices(new PrintWriter(System.err), monitor); - runner = new REPLRunner(System.in, System.err, System.console() != null ? (OutputStream) monitor : System.out, term, services); + runner = new REPLRunner(System.in, System.err, monitor instanceof OutputStream ? (OutputStream) monitor : System.out, term, services); } runner.run(args); diff --git a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java index 450d4958567..773871cb8ff 100644 --- a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java +++ b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java @@ -23,8 +23,6 @@ import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -import jline.TerminalFactory; - import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.Runner; @@ -40,7 +38,6 @@ import org.rascalmpl.interpreter.load.StandardLibraryContributor; import org.rascalmpl.interpreter.result.AbstractFunction; import org.rascalmpl.interpreter.utils.RascalManifest; -import org.rascalmpl.repl.TerminalProgressBarMonitor; import org.rascalmpl.uri.URIUtil; import org.rascalmpl.values.ValueFactoryFactory; @@ -56,9 +53,7 @@ * */ public class RascalJUnitParallelRecursiveTestRunner extends Runner { - private final static IRascalMonitor monitor = System.console() != null - ? new TerminalProgressBarMonitor(System.out, System.in, TerminalFactory.get()) - : new NullRascalMonitor(); + private final static IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out); private final int numberOfWorkers; private final Semaphore importsCompleted = new Semaphore(0); @@ -306,9 +301,7 @@ private void initializeEvaluator() { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___junit_test___", heap)); - var outStream = System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out; - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap); - evaluator.setMonitor(monitor); + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.getConfiguration().setErrors(true); diff --git a/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java b/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java index 5b2d8968cd8..342c5bf8137 100644 --- a/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java +++ b/src/org/rascalmpl/test/infrastructure/RascalJUnitTestRunner.java @@ -25,7 +25,6 @@ import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; -import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.ITestResultListener; import org.rascalmpl.interpreter.NullRascalMonitor; @@ -37,7 +36,6 @@ import org.rascalmpl.interpreter.utils.RascalManifest; import org.rascalmpl.library.util.PathConfig; import org.rascalmpl.library.util.PathConfig.RascalConfigMode; -import org.rascalmpl.repl.TerminalProgressBarMonitor; import org.rascalmpl.shell.ShellEvaluatorFactory; import org.rascalmpl.uri.URIResolverRegistry; import org.rascalmpl.uri.URIUtil; @@ -48,8 +46,6 @@ import io.usethesource.vallang.ISourceLocation; import io.usethesource.vallang.IValue; -import jline.Terminal; -import jline.TerminalFactory; public class RascalJUnitTestRunner extends Runner { private static Evaluator evaluator; @@ -65,18 +61,8 @@ public class RascalJUnitTestRunner extends Runner { try { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___junit_test___", heap)); - - Terminal tm = TerminalFactory.get(); - IRascalMonitor monitor = System.console() != null - ? new TerminalProgressBarMonitor(System.out, System.in, tm) - : new NullRascalMonitor(); - - var outStream = System.console() != null - ? (TerminalProgressBarMonitor) monitor - : System.out; - - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, outStream, root, heap); - evaluator.setMonitor(System.console() != null ? monitor : new NullRascalMonitor()); + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap); + evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.getConfiguration().setErrors(true); } diff --git a/src/org/rascalmpl/test/infrastructure/TestFramework.java b/src/org/rascalmpl/test/infrastructure/TestFramework.java index ccc60e9b803..2f66d5ed94e 100644 --- a/src/org/rascalmpl/test/infrastructure/TestFramework.java +++ b/src/org/rascalmpl/test/infrastructure/TestFramework.java @@ -30,13 +30,11 @@ import org.junit.After; import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; -import org.rascalmpl.interpreter.NullRascalMonitor; import org.rascalmpl.interpreter.env.GlobalEnvironment; import org.rascalmpl.interpreter.env.ModuleEnvironment; import org.rascalmpl.interpreter.load.StandardLibraryContributor; import org.rascalmpl.interpreter.result.Result; import org.rascalmpl.interpreter.staticErrors.StaticError; -import org.rascalmpl.repl.TerminalProgressBarMonitor; import org.rascalmpl.uri.URIResolverRegistry; import org.rascalmpl.uri.URIUtil; import io.usethesource.vallang.IBool; @@ -44,15 +42,11 @@ import io.usethesource.vallang.IValue; import io.usethesource.vallang.exceptions.FactTypeUseException; import io.usethesource.vallang.type.TypeFactory; -import jline.TerminalFactory; - import org.rascalmpl.values.ValueFactoryFactory; public class TestFramework { - private final static IRascalMonitor monitor = System.console() != null - ? new TerminalProgressBarMonitor(System.out, System.in, TerminalFactory.get()) - : new NullRascalMonitor(); + private final static IRascalMonitor monitor = IRascalMonitor.buildConsoleMonitor(System.in, System.out); private final static Evaluator evaluator; private final static GlobalEnvironment heap; @@ -65,10 +59,10 @@ public class TestFramework { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___test___", heap)); - stderr = new PrintWriter(System.err, true); - stdout = System.console() != null ? new PrintWriter((TerminalProgressBarMonitor) monitor) : new PrintWriter(System.out, true); - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.console() != null ? (TerminalProgressBarMonitor) monitor : System.out , root, heap); - evaluator.setMonitor(monitor); + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, monitor, root, heap); + + stdout = evaluator.getOutPrinter(); + stderr = evaluator.getErrorPrinter(); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); RascalJUnitTestRunner.configureProjectEvaluator(evaluator, RascalJUnitTestRunner.inferProjectRoot(TestFramework.class));