Skip to content

Commit

Permalink
Refactoring...
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 23, 2020
1 parent c77a376 commit d1a1134
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 139 deletions.
30 changes: 0 additions & 30 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public enum Command {NANO
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 LineReader reader;
private Exception exception;

Expand Down Expand Up @@ -103,11 +102,6 @@ public Map<String, String> commandAliases() {
return aliasCommand;
}

public List<String> commandInfo(String command) {
commandInfo.putIfAbsent(command(command), doCommandInfo(command));
return commandInfo.get(command(command));
}

private void doNameCommand() {
nameCommand = commandName.entrySet()
.stream()
Expand Down Expand Up @@ -190,18 +184,6 @@ public void execute(String command, String[] args, InputStream in, PrintStream o
}
}

public CmdDesc commandDescription(String command) {
List<String> args = Arrays.asList("--help");
try {
execute(command, args);
} catch (HelpException e) {
return compileCommandDescription(e.getMessage());
} catch (Exception e) {

}
return null;
}

private List<OptDesc> commandOptions(String command) {
List<String> args = Arrays.asList("--help");
try {
Expand All @@ -214,18 +196,6 @@ private List<OptDesc> commandOptions(String command) {
return null;
}

private List<String> doCommandInfo(String command) {
List<String> args = Arrays.asList("--help");
try {
execute(command, args);
} catch (HelpException e) {
return compileCommandInfo(e.getMessage());
} catch (Exception e) {

}
return new ArrayList<>();
}

private Terminal terminal() {
return reader.getTerminal();
}
Expand Down
27 changes: 24 additions & 3 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import org.jline.builtins.Completers;
import org.jline.builtins.Widgets;
import org.jline.builtins.Options.HelpException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -67,7 +69,16 @@ default String name() {
* Returns a short info about command known by this registry.
* @return a short info about command
*/
List<String> commandInfo(String command);
default List<String> commandInfo(String command) {
try {
invoke(command, new Object[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandInfo(e.getMessage());
} catch (Exception e) {

}
return new ArrayList<>();
}

/**
* Returns whether a command with the specified name is known to this registry.
Expand All @@ -90,7 +101,16 @@ default String name() {
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
Widgets.CmdDesc commandDescription(String command);
default Widgets.CmdDesc commandDescription(String command) {
try {
invoke(command, new Object[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {

}
return null;
}

/**
* Execute a command that have only string parameters and options. Implementation of the method is required
Expand Down Expand Up @@ -122,4 +142,5 @@ default Object invoke(String command, Object... args) throws Exception {
}
return execute(command, _args);
}
}

}
36 changes: 3 additions & 33 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ public void alias(String alias, String command) {
aliasCommand.put(alias, command);
}

@Override
public List<String> commandInfo(String command) {
return doCommandInfo(command);
}

@Override
public Completers.SystemCompleter compileCompleters() {
SystemCompleter out = new SystemCompleter();
Expand All @@ -170,11 +165,6 @@ public Completers.SystemCompleter compileCompleters() {
return out;
}

@Override
public Widgets.CmdDesc commandDescription(String command) {
return doCommandDescription(command);
}

@Override
public Object[] expandParameters(String[] args) throws Exception {
Object[] out = new Object[args.length];
Expand Down Expand Up @@ -551,7 +541,9 @@ public void println(Map<String, Object> options, Object object) {
} else if (!style.isEmpty() && object instanceof String) {
highlight(width, style, (String) object);
} else if (object instanceof Exception) {
if (options.getOrDefault("exception", "stack").equals("stack")) {
if (object instanceof Options.HelpException) {
Options.HelpException.highlight(((Exception)object).getMessage(), Options.HelpException.defaultStyle()).print(terminal);
} else if (options.getOrDefault("exception", "stack").equals("stack")) {
((Exception) object).printStackTrace();
} else {
String message = ((Exception) object).getMessage();
Expand Down Expand Up @@ -713,17 +705,6 @@ private Object slurp(Builtins.CommandInput input) {
return out;
}

public Widgets.CmdDesc doCommandDescription(String command) {
try {
invoke(command, "--help");
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {

}
return null;
}

private List<OptDesc> commandOptions(String command) {
try {
invoke(command, "--help");
Expand All @@ -735,17 +716,6 @@ private List<OptDesc> commandOptions(String command) {
return null;
}

private List<String> doCommandInfo(String command) {
try {
invoke(command, "--help");
} catch (HelpException e) {
return Builtins.compileCommandInfo(e.getMessage());
} catch (Exception e) {

}
return new ArrayList<>();
}

private List<Completer> slurpCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
Expand Down
15 changes: 12 additions & 3 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SystemRegistryImpl implements SystemRegistry {
private final CommandRegistry[] commandRegistries;
private Integer consoleId = null;
private Terminal terminal;
private Map<String, List<String>> commandInfos = new HashMap<>();

public SystemRegistryImpl(CommandRegistry... commandRegistries) {
this.commandRegistries = commandRegistries;
Expand Down Expand Up @@ -83,7 +84,14 @@ public Map<String, String> commandAliases() {
@Override
public List<String> commandInfo(String command) {
int id = registryId(command);
return id > -1 ? commandRegistries[id].commandInfo(command) : new ArrayList<>();
List<String> out = new ArrayList<>();
if (id > -1) {
if (!commandInfos.containsKey(command)) {
commandInfos.put(command, commandRegistries[id].commandInfo(command));
}
out = commandInfos.get(command);
}
return out;
}

@Override
Expand Down Expand Up @@ -217,6 +225,7 @@ private void printCommands(Collection<String> commands, int max) {
if (!done) {
asb.toAttributedString().println(terminal);
}
terminal.flush();
}

private String doCommandInfo(List<String> info) {
Expand All @@ -231,7 +240,7 @@ private void help() {
for (CommandRegistry r : commandRegistries) {
if (isBuiltinRegistry(r)) {
for (String c: r.commandNames()) {
builtinCommands.put(c, doCommandInfo(r.commandInfo(c)));
builtinCommands.put(c, doCommandInfo(commandInfo(c)));
}
}
}
Expand All @@ -251,7 +260,7 @@ private void help() {
printHeader(r.name());
if (withInfo) {
for (String c : cmds) {
printCommandInfo(c, doCommandInfo(r.commandInfo(c)), max);
printCommandInfo(c, doCommandInfo(commandInfo(c)), max);
}
} else {
printCommands(cmds, max);
Expand Down
51 changes: 14 additions & 37 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Widgets.CmdDesc;
import org.jline.builtins.Widgets.TailTipWidgets;
import org.jline.builtins.Widgets.TailTipWidgets.TipType;
import org.jline.keymap.KeyMap;
Expand Down Expand Up @@ -88,17 +87,6 @@ public Map<String, String> commandAliases() {
return aliasCommand;
}

public List<String> commandInfo(String command) {
try {
execute(command, new String[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandInfo(e.getMessage());
} catch (Exception e) {

}
return new ArrayList<>();
}

public boolean hasCommand(String command) {
if (commandExecute.containsKey(command) || aliasCommand.containsKey(command)) {
return true;
Expand Down Expand Up @@ -133,28 +121,6 @@ public Object execute(String command, String[] args) throws Exception {
return null;
}

public CmdDesc commandDescription(String command) {
try {
execute(command, new String[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {

}
return null;
}

private List<OptDesc> commandOptions(String command) {
try {
execute(command, new String[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandOptions(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private void tput(Builtins.CommandInput input) {
final String[] usage = {
"tput - put terminal capability",
Expand Down Expand Up @@ -230,6 +196,17 @@ private void clear(Builtins.CommandInput input) {
}
}

private List<OptDesc> commandOptions(String command) {
try {
execute(command, new String[] {"--help"});
} catch (HelpException e) {
return Builtins.compileCommandOptions(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private List<Completer> defaultCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
Expand Down Expand Up @@ -308,7 +285,7 @@ public static void main(String[] args) {
consoleEngine.println(terminal.getName()+": "+terminal.getType());
while (true) {
try {
scriptEngine.del("_*"); // delete temporary variables
scriptEngine.del("_*"); // delete temporary variables
String line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
line = line.trim();
if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
Expand All @@ -319,7 +296,7 @@ public static void main(String[] args) {
consoleEngine.println(result);
}
catch (Options.HelpException e) {
Options.HelpException.highlight(e.getMessage(), Options.HelpException.defaultStyle()).print(terminal);
consoleEngine.println(e); // print command help message
}
catch (UserInterruptException e) {
// Ignore
Expand All @@ -329,7 +306,7 @@ public static void main(String[] args) {
}
catch (Exception e) {
consoleEngine.println(e);
scriptEngine.put("exception", e); // save exception to console variable
scriptEngine.put("exception", e); // save exception to console variable
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
-->
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jline</groupId>
<artifactId>jline-parent</artifactId>
<version>3.13.4-SNAPSHOT</version>
</parent>
<artifactId>jline-groovy</artifactId>
<name>JLine Groovy</name>
<url>http://maven.apache.org</url>
<dependencies>
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jline</groupId>
<artifactId>jline-parent</artifactId>
<version>3.13.4-SNAPSHOT</version>
</parent>
<artifactId>jline-groovy</artifactId>
<name>JLine Groovy</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-reader</artifactId>
Expand All @@ -35,13 +35,13 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-json</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -75,7 +75,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit d1a1134

Please sign in to comment.