Skip to content

Commit

Permalink
instead of switching between plaintext and html output, we always out…
Browse files Browse the repository at this point in the history
…put both channels with both mimetypes. the UI decides which output to show and how
  • Loading branch information
jurgenvinju committed Jan 28, 2021
1 parent 70f7fc5 commit 3c030f1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 31 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
"vmArgs": "-ea",
"projectName": "rascal"
},
{
"type": "java",
"name": "Attach RascalNotebook",
"request": "attach",
"hostName": "localhost",
"port": 8000,
"projectName": "rascal-notebook"
},
{
"type": "java",
"name": "Launch RascalShell in rascal-core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TutorCommandExecutor(PathConfig pcfg) throws IOException, URISyntaxExcept
shellHTMLOutput = new ByteArrayOutputStream();
shellInputNotUsed = new ByteArrayInputStream("***this inputstream should not be used***".getBytes());

repl = new RascalInterpreterREPL(false, false, false, null) {
repl = new RascalInterpreterREPL(false, false, null) {
@Override
protected Evaluator constructEvaluator(InputStream input, OutputStream stdout, OutputStream stderr) {
Evaluator eval = ShellEvaluatorFactory.getDefaultEvaluator(input, stdout, stderr);
Expand Down
2 changes: 0 additions & 2 deletions src/org/rascalmpl/library/util/TermREPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public static class TheREPL implements ILanguageProtocol {
private final IValueFactory vf;
private final AbstractFunction stacktrace;


public TheREPL(IValueFactory vf, IString title, IString welcome, IString prompt, IString quit, ISourceLocation history,
IFunction handler, IFunction completor, IValue stacktrace, InputStream input, OutputStream stderr, OutputStream stdout) {
this.vf = vf;
Expand All @@ -98,7 +97,6 @@ public TheREPL(IValueFactory vf, IString title, IString welcome, IString prompt,
this.stacktrace = (AbstractFunction) stacktrace;
this.currentPrompt = prompt.getValue();
this.quit = quit.getValue();

}

@Override
Expand Down
20 changes: 6 additions & 14 deletions src/org/rascalmpl/repl/BaseRascalREPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ protected State getState() {
protected String currentPrompt = ReadEvalPrintDialogMessages.PROMPT;
private StringBuffer currentCommand;
protected final StandardTextWriter indentedPrettyPrinter;
private final boolean htmlOutput;
private final boolean allowColors;
private final static IValueFactory VF = ValueFactoryFactory.getValueFactory();
protected final REPLContentServerManager contentManager = new REPLContentServerManager();

public BaseRascalREPL(boolean prettyPrompt, boolean allowColors, boolean htmlOutput) throws IOException, URISyntaxException {
this.htmlOutput = htmlOutput;
public BaseRascalREPL(boolean prettyPrompt, boolean allowColors) throws IOException, URISyntaxException {
this.allowColors = allowColors;

if (allowColors) {
Expand Down Expand Up @@ -152,13 +150,11 @@ private static interface OutputWriter {
}

protected void printResult(IRascalResult result, Map<String, InputStream> output, Map<String, String> metadata) throws IOException {
String mimeType = "text/" + (htmlOutput ? "html" : "plain");

if (result == null || result.getValue() == null) {
output.put(mimeType, stringStream("ok\n"));
output.put("text/plain", stringStream("ok\n"));
return;
}
else if (result.getStaticType() == RascalValueFactory.Content) {
else if (result.getStaticType().isSubtypeOf(RascalValueFactory.Content) && !result.getStaticType().isBottom()) {
// we have interactive output in HTML form to serve
serveContent(result, output, metadata);
output.put("text/plain", stringStream("ok\n"));
Expand All @@ -185,10 +181,10 @@ public void finishOutput() {
writeOutput(result, writer);

if (out.getBuffer().length() == 0) {
output.put(mimeType, stringStream("ok\n"));
output.put("text/plain", stringStream("ok\n"));
}
else {
output.put(mimeType, stringStream(out.toString()));
output.put("text/plain", stringStream(out.toString()));
}
}
}
Expand Down Expand Up @@ -216,11 +212,7 @@ private void serveContent(IRascalResult result, Map<String, InputStream> output,

metadata.put("url", URL);

if (!htmlOutput) {
// if there is HTML output we can see it inline
getOutputWriter().println("Serving \'" + id + "\' at |" + URL + "|");
}

output.put("text/plain", stringStream("Serving \'" + id + "\' at |" + URL + "|"));
output.put("text/html", stringStream("<iframe class=\"rascal-content-frame\" style=\"display: block; width: 100%; height: 100%; resize: both\" src=\""+ URL +"\"></iframe>"));
}

Expand Down
17 changes: 6 additions & 11 deletions src/org/rascalmpl/repl/RascalInterpreterREPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,25 @@ public abstract class RascalInterpreterREPL extends BaseRascalREPL {
protected Evaluator eval;
private boolean measureCommandTime;

public RascalInterpreterREPL(boolean prettyPrompt, boolean allowColors, boolean htmlOutput, File persistentHistory)
public RascalInterpreterREPL(boolean prettyPrompt, boolean allowColors, File persistentHistory)
throws IOException, URISyntaxException {
super(prettyPrompt, allowColors, htmlOutput);
super(prettyPrompt, allowColors);
}

public void cleanEnvironment() {
eval.getCurrentModuleEnvironment().reset();
}

public RascalInterpreterREPL() throws IOException, URISyntaxException{
super(true, true, false);
super(true, true);
}

public RascalInterpreterREPL(boolean htmlOutput) throws IOException, URISyntaxException{
super(true, true, htmlOutput);
}

@Override
protected Function<IValue, IValue> liftProviderFunction(IFunction callback) {

return (t) -> {
synchronized(eval) {
return callback.call(t);
}
synchronized(eval) {
return callback.call(t);
}
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/rascalmpl/shell/REPLRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static File getHistoryFile() throws IOException {

public REPLRunner(InputStream stdin, OutputStream stderr, OutputStream stdout, Terminal term)
throws IOException, URISyntaxException {
super(makeInterpreter(stdin, stderr, stdout, true, term.isAnsiSupported(), false, getHistoryFile(), term), null,
super(makeInterpreter(stdin, stderr, stdout, true, term.isAnsiSupported(), getHistoryFile(), term), null,
stdin, stderr, stdout, true, term.isAnsiSupported(), getHistoryFile(), term, null);
}

Expand All @@ -41,10 +41,10 @@ public REPLRunner(ILanguageProtocol language) throws IOException, URISyntaxExcep
}

private static ILanguageProtocol makeInterpreter(InputStream stdin, OutputStream stderr, OutputStream stdout,
boolean prettyPrompt, boolean allowColors, boolean htmlOutput, File persistentHistory, Terminal terminal)
boolean prettyPrompt, boolean allowColors, File persistentHistory, Terminal terminal)
throws IOException, URISyntaxException {
RascalInterpreterREPL repl =
new RascalInterpreterREPL(prettyPrompt, allowColors, htmlOutput, getHistoryFile()) {
new RascalInterpreterREPL(prettyPrompt, allowColors, getHistoryFile()) {
@Override
protected Evaluator constructEvaluator(InputStream input, OutputStream stdout, OutputStream stderr) {
return ShellEvaluatorFactory.getDefaultEvaluator(input, stdout, stderr);
Expand Down

0 comments on commit 3c030f1

Please sign in to comment.