Skip to content

Commit

Permalink
Improved repl printing, step II
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 13, 2020
1 parent 0b7b8e3 commit 5dbec17
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 54 deletions.
12 changes: 7 additions & 5 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
import org.jline.reader.ParsedLine;

public interface ConsoleEngine extends CommandRegistry {

void setSystemRegistry(SystemRegistry systemRegistry);

Object execute(ParsedLine parsedLine) throws Exception;


Object postProcess(String line, Object result);

default void println(Object object) {
println(new HashMap<>(), object);
println(new HashMap<>(), object);
}

void println(Map<String, Object> options, Object object);

}
70 changes: 33 additions & 37 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,22 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jline.builtins.CommandRegistry;
import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.Command;
import org.jline.builtins.Builtins.CommandInput;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.reader.*;
import org.jline.reader.Parser.ParseContext;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;


/**
* Console commands and script execution.
* Manage console variables, commands and script execution.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
Expand All @@ -52,7 +39,6 @@ public enum Command {SHOW
private Map<String,Command> nameCommand = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<Command,CommandMethods> commandExecute = new HashMap<>();
private Map<Command,List<String>> commandInfo = new HashMap<>();
private Exception exception;
private SystemRegistry systemRegistry;
private String scriptExtension = "jline";
Expand All @@ -72,24 +58,24 @@ public ConsoleEngineImpl(ScriptEngine engine, Parser parser, Terminal terminal)
commandExecute.put(Command.DEL, new CommandMethods(this::del, this::defaultCompleter));
commandExecute.put(Command.SHOW, new CommandMethods(this::show, this::defaultCompleter));
}

public void setSystemRegistry(SystemRegistry systemRegistry) {
this.systemRegistry = systemRegistry;
}

public ConsoleEngineImpl scriptExtension(String extension) {
this.scriptExtension = extension;
return this;
}

public Set<String> commandNames() {
return nameCommand.keySet();
}

public Map<String, String> commandAliases() {
return aliasCommand;
}

public boolean hasCommand(String name) {
if (nameCommand.containsKey(name) || aliasCommand.containsKey(name)) {
return true;
Expand Down Expand Up @@ -164,7 +150,7 @@ private Object[] expandVariables(String[] args) throws Exception {
}
return out;
}

private String expandName(String name) {
String regexVar = "[a-zA-Z]{1,}[a-zA-Z0-9_-]*";
String out = name;
Expand All @@ -175,35 +161,35 @@ private String expandName(String name) {
if (matcher.find()) {
out = matcher.group(1) + matcher.group(2);
} else {
throw new IllegalArgumentException();
throw new IllegalArgumentException();
}
}
return out;
}

private boolean isCodeBlock(String line) {
return line.contains("\n") && line.trim().endsWith("}");
}

private boolean isCommandLine(String line) {
String command = Parser.getCommand(line);
return command.startsWith(":") && systemRegistry.hasCommand(command.substring(1));
}

private String quote(String var) {
if ((var.startsWith("\\\"") && var.endsWith("\\\""))
|| (var.startsWith("'") && var.endsWith("'"))) {
return var;
}
if (var.contains("\\\"")) {
return "'" + var + "'";
}
return "'" + var + "'";
}
return "\\\"" + var + "\\\"";
}

public Object execute(ParsedLine pl) throws Exception {
if (pl.line().trim().startsWith("#")) {
return null;
return null;
}
String[] args = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Expand All @@ -214,6 +200,7 @@ public Object execute(ParsedLine pl) throws Exception {
String ext = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
if(engine.getExtensions().contains(ext)) {
out = engine.execute(file, expandVariables(args));
out = postProcess(pl.line(), out);
} else if (scriptExtension.equals(ext)) {
boolean done = false;
String line = "";
Expand All @@ -238,7 +225,7 @@ public Object execute(ParsedLine pl) throws Exception {
}
}
if (!done) {
throw new IllegalArgumentException("Incompleted command: \n" + line);
throw new IllegalArgumentException("Incompleted command: \n" + line);
}
}
} else {
Expand All @@ -260,8 +247,8 @@ public Object execute(ParsedLine pl) throws Exception {
List<String> ws = parser.parse(s, 0, ParseContext.COMPLETE).words();
int idx = ws.get(0).lastIndexOf(":");
if (idx > 0) {
sb.append(ws.get(0).substring(0, idx - 1));
}
sb.append(ws.get(0).substring(0, idx - 1));
}
sb.append(registry + ".invoke('" + ws.get(0).substring(idx + 1) + "'");
for (int i = 1; i < ws.size(); i++) {
sb.append(", ");
Expand All @@ -275,7 +262,7 @@ public Object execute(ParsedLine pl) throws Exception {
}
line = sb.toString();
if (copyRegistry && !engine.hasVariable(registry)) {
engine.put(registry, systemRegistry);
engine.put(registry, systemRegistry);
}
}
if (engine.hasVariable(line)) {
Expand All @@ -286,9 +273,18 @@ public Object execute(ParsedLine pl) throws Exception {
engine.execute(line);
}
}
return out;
return out;
}

public Object postProcess(String line, Object result) {
Object out = result;
if (Parser.getVariable(line) != null) {
engine.put(Parser.getVariable(line), result);
out = null;
}
return out;
}

public Object execute(String command, String[] args) throws Exception {
exception = null;
Object out = commandExecute.get(command(command)).executeFunction().apply(new Builtins.CommandInput(args));
Expand All @@ -297,7 +293,7 @@ public Object execute(String command, String[] args) throws Exception {
}
return out;
}

public void println(Map<String, Object> options, Object object) {
options.putIfAbsent("width", terminal.getSize().getColumns());
for (AttributedString as : engine.format(options, object)) {
Expand All @@ -321,5 +317,5 @@ public Object del(Builtins.CommandInput input) {
private List<Completer> defaultCompleter(String command) {
return Arrays.asList(NullCompleter.INSTANCE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ public Object execute(ParsedLine pl) throws Exception {
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object out = null;
boolean done = false;
if ("help".equals(cmd) || "?".equals(cmd)) {
help();
done = true;
} else {
int id = registryId(cmd);
if (id > -1) {
out = commandRegistries[id].execute(cmd, argv);
if (consoleId != null) {
out = consoleEngine().postProcess(pl.line(), out);
}
} else if (consoleId != null) {
out = consoleEngine().execute(pl);
}
Expand Down
20 changes: 16 additions & 4 deletions groovy/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2002-2016, the original author or authors.
This software is distributable under the BSD license. See the terms of the
BSD license in the documentation provided with this software.
https://opensource.org/licenses/BSD-3-Clause
-->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
Expand All @@ -25,9 +35,11 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
<!--
<type>pom</type>
-->
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-json</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -43,7 +55,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<showWarnings>true</showWarnings>
<showWarnings>false</showWarnings>
<compilerArgs>
<arg>-Xlint:all,-options</arg>
<arg>-profile</arg>
Expand Down
7 changes: 7 additions & 0 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
package org.jline.groovy;

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

public class Utils {

private Utils() {}
Expand All @@ -16,4 +19,8 @@ public class Utils {
object.toString()
}

static Object convert(Object object) {
def slurper = new JsonSlurper()
slurper.parseText(JsonOutput.toJson(object))
}
}
Loading

0 comments on commit 5dbec17

Please sign in to comment.