Skip to content

Commit

Permalink
Refactoring...
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 25, 2020
1 parent a67db91 commit 4b040eb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,16 @@ private void internalExecute() throws Exception {
asb.toAttributedString().println(terminal);
terminal.flush();
}
systemRegistry.execute(parser.parse(line, 0, ParseContext.COMPLETE));
systemRegistry.execute(line);
line = "";
} catch (EOFError e) {
done = false;
line += "\n";
} catch (SyntaxError e) {
throw e;
} catch (EndOfFileException e) {
done = true;
break;
} catch (Exception e) {
throw new IllegalArgumentException(line + "\n" + e.getMessage());
}
Expand Down
14 changes: 7 additions & 7 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@
public interface SystemRegistry extends CommandRegistry {

/**
* Set terminal
* @param terminal
* Set command registeries
* @param commandRegistries
*/
public void setTerminal(Terminal terminal);
void setCommandRegistries(CommandRegistry... commandRegistries);

/**
* Initialize consoleEngine environment by executing console script
* @param script
*/
public void initialize(File script);
void initialize(File script);

/**
* Returns a command, method or syntax description for use in the JLine Widgets framework.
* @param command line whose description to return
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
public Widgets.CmdDesc commandDescription(Widgets.CmdLine line);
Widgets.CmdDesc commandDescription(Widgets.CmdLine line);

/**
* Execute a command, script or evaluate scriptEngine statement
* @param parsedLine
* @param line
* @return result
* @throws Exception
*/
Object execute(ParsedLine parsedLine) throws Exception;
Object execute(String line) throws Exception;

/**
* @return systemRegistry of the current thread
Expand Down
50 changes: 32 additions & 18 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import org.jline.builtins.CommandRegistry;
import org.jline.builtins.Widgets;
import org.jline.builtins.Options.HelpException;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.EndOfFileException;
import org.jline.reader.ParsedLine;
import org.jline.reader.Parser;
import org.jline.reader.Parser.ParseContext;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;

Expand All @@ -26,12 +29,21 @@
*/
public class SystemRegistryImpl implements SystemRegistry {
private static final Class<?>[] BUILTIN_REGISTERIES = {Builtins.class, ConsoleEngineImpl.class};
private final CommandRegistry[] commandRegistries;
private CommandRegistry[] commandRegistries;
private Integer consoleId = null;
private Terminal terminal;
private Parser parser;
private ConfigurationPath configPath;
private Map<String, List<String>> commandInfos = new HashMap<>();

public SystemRegistryImpl(Parser parser, Terminal terminal, ConfigurationPath configPath) {
this.parser = parser;
this.terminal = terminal;
this.configPath = configPath;
}

public SystemRegistryImpl(CommandRegistry... commandRegistries) {
@Override
public void setCommandRegistries(CommandRegistry... commandRegistries) {
this.commandRegistries = commandRegistries;
for (int i = 0; i < commandRegistries.length; i++) {
if (commandRegistries[i] instanceof ConsoleEngine) {
Expand All @@ -48,18 +60,13 @@ public SystemRegistryImpl(CommandRegistry... commandRegistries) {
SystemRegistry.add(this);
}

@Override
public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}

@Override
public void initialize(File script) {
if (consoleId != null) {
try {
consoleEngine().execute(script);
} catch (Exception e) {
System.err.println(e.getMessage());
consoleEngine().println(e);
}
}
}
Expand Down Expand Up @@ -143,7 +150,8 @@ public Object invoke(String command, Object... args) throws Exception {
}

@Override
public Object execute(ParsedLine pl) throws Exception {
public Object execute(String line) throws Exception {
ParsedLine pl = parser.parse(line, 0, ParseContext.ACCEPT_LINE);
if (pl.line().isEmpty() || pl.line().startsWith("#")) {
return null;
}
Expand All @@ -153,19 +161,25 @@ public Object execute(ParsedLine pl) throws Exception {
if (cmd.startsWith(":")) {
cmd = cmd.substring(1);
}
if ("help".equals(cmd) || "?".equals(cmd)) {
if ("exit".equals(cmd)) {
throw new EndOfFileException();
} else if ("help".equals(cmd)) {
help();
} else {
int id = registryId(cmd);
if (id > -1) {
if (consoleId != null) {
out = commandRegistries[id].invoke(cmd, consoleEngine().expandParameters(argv));
out = consoleEngine().postProcess(pl.line(), out);
} else {
out = commandRegistries[id].execute(cmd, argv);
try {
if (id > -1) {
if (consoleId != null) {
out = commandRegistries[id].invoke(cmd, consoleEngine().expandParameters(argv));
out = consoleEngine().postProcess(pl.line(), out);
} else {
out = commandRegistries[id].execute(cmd, argv);
}
} else if (consoleId != null) {
out = consoleEngine().execute(pl);
}
} else if (consoleId != null) {
out = consoleEngine().execute(pl);
} catch (HelpException e) {
consoleEngine().println(e);
}
}
return out;
Expand Down
27 changes: 8 additions & 19 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ private List<Completer> tputCompleter(String command) {


public static void main(String[] args) {
String prompt = "groovy-repl> ";
String rightPrompt = null;
try {
//
// Parser & Terminal
Expand All @@ -254,16 +252,15 @@ public static void main(String[] args) {
// ScriptEngine and command registeries
//
File file = new File(Repl.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String root = file.getCanonicalPath().replace("classes", "").replaceAll("\\\\", "/"); // forward slash works better also in windows!
String root = file.getCanonicalPath().replace("classes", "").replaceAll("\\\\", "/"); // forward slashes works better also in windows!
GroovyEngine scriptEngine = new GroovyEngine();
scriptEngine.put("ROOT", root);
ConfigurationPath configPath = new ConfigurationPath(Paths.get(root), Paths.get(root));
Builtins builtins = new Builtins(Paths.get(""), configPath, null);
ConsoleEngine consoleEngine = new ConsoleEngineImpl(scriptEngine, parser, terminal, ()->Paths.get(""), configPath);
MyCommands myCommands = new MyCommands();
SystemRegistry systemRegistry = new SystemRegistryImpl(consoleEngine, builtins, myCommands);
systemRegistry.setTerminal(terminal);
systemRegistry.initialize(Paths.get(root, "init.jline").toFile());
SystemRegistry systemRegistry = new SystemRegistryImpl(parser, terminal, configPath);
systemRegistry.setCommandRegistries(consoleEngine, builtins, myCommands);
//
//
// LineReader
Expand All @@ -290,31 +287,23 @@ public static void main(String[] args) {
builtins.setLineReader(reader);
myCommands.setLineReader(reader);
//
// Tailtip widget
// widgets and console initialization
//
new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
Map<String, KeyMap<Binding>> keyMaps = reader.getKeyMaps();
KeyMap<Binding> keyMap = keyMaps.get("main");
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));
systemRegistry.initialize(Paths.get(root, "init.jline").toFile());
//
// REPL-loop
//
consoleEngine.println(terminal.getName()+": "+terminal.getType());
while (true) {
try {
scriptEngine.del("_*"); // delete temporary variables
String line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
line = line.trim();
if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
break;
}
ParsedLine pl = reader.getParser().parse(line, 0, ParseContext.ACCEPT_LINE);
Object result = systemRegistry.execute(pl);
String line = reader.readLine("groovy-repl> ");
Object result = systemRegistry.execute(line);
consoleEngine.println(result);
}
catch (Options.HelpException e) {
consoleEngine.println(e); // print command help message
}
catch (UserInterruptException e) {
// Ignore
}
Expand Down

0 comments on commit 4b040eb

Please sign in to comment.