Skip to content

Commit

Permalink
GroovyCommand: added command console & inspect command option --gui
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 24, 2020
1 parent c166189 commit 43748c7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,8 @@ public void trace(boolean stack, Exception exception) {
String message = exception.getMessage();
AttributedStringBuilder asb = new AttributedStringBuilder();
if (message != null) {
asb.append(message, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
String m = exception.getClass().getSimpleName() + ": " + message;
asb.append(m, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
} else {
asb.append("Caught exception: ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
asb.append(exception.getClass().getCanonicalName(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
Expand Down
5 changes: 5 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<artifactId>groovy-json</artifactId>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-console</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.jline.terminal.Terminal.Signal;
import org.jline.utils.InfoCmp;
import org.jline.utils.InfoCmp.Capability;

import org.jline.utils.OSUtils;

/**
Expand Down Expand Up @@ -383,6 +384,18 @@ public static void main(String[] args) {
}
}
systemRegistry.close(); // persist pipeline completer names etc

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
boolean groovyRunning=false; // check Groovy GUI apps
for (Thread t : threadSet) {
if (t.getName().startsWith("AWT-Shut")) {
groovyRunning = true;
break;
}
}
if (groovyRunning) {
consoleEngine.println("Please, close Groovy Consoles/Object Browsers!");
}
}
catch (Throwable t) {
t.printStackTrace();
Expand Down
5 changes: 5 additions & 0 deletions groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-json</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-console</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
102 changes: 81 additions & 21 deletions groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.utils.AttributedString;

import groovy.console.ui.Console;
import groovy.console.ui.ObjectBrowser;

public class GroovyCommand extends AbstractCommandRegistry implements CommandRegistry {
public enum Command {INSPECT}
public enum Command {INSPECT, CONSOLE}
private GroovyEngine engine;
private Printer printer;
private final Map<Command,CmdDesc> commandDescs = new HashMap<>();
private final Map<Command,List<String>> commandInfos = new HashMap<>();
private boolean consoleUi;

public GroovyCommand(GroovyEngine engine, Printer printer) {
this(null, engine, printer);
Expand All @@ -43,6 +47,11 @@ public GroovyCommand(GroovyEngine engine, Printer printer) {
public GroovyCommand(Set<Command> commands, GroovyEngine engine, Printer printer) {
this.engine = engine;
this.printer = printer;
try {
Class.forName("groovy.console.ui.ObjectBrowser");
consoleUi = true;
} catch (Exception e) {
}
Set<Command> cmds = new HashSet<>();
Map<Command,String> commandName = new HashMap<>();
Map<Command,CommandMethods> commandExecute = new HashMap<>();
Expand All @@ -51,12 +60,17 @@ public GroovyCommand(Set<Command> commands, GroovyEngine engine, Printer printer
} else {
cmds = new HashSet<>(commands);
}
if (!consoleUi) {
cmds.remove(Command.CONSOLE);
}
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
}
commandExecute.put(Command.INSPECT, new CommandMethods(this::inspect, this::inspectCompleter));
commandExecute.put(Command.CONSOLE, new CommandMethods(this::console, this::consoleCompleter));
registerCommands(commandName, commandExecute);
commandDescs.put(Command.INSPECT, inspectCmdDesc());
commandDescs.put(Command.CONSOLE, consoleCmdDesc());
}

@Override
Expand All @@ -71,6 +85,23 @@ public CmdDesc commandDescription(String command) {
return commandDescs.get(cmd);
}

public void console(CommandInput input) {
if (input.args().length > 1) {
throw new IllegalArgumentException("Wrong number of command parameters: " + input.args().length);
}
if (input.args().length == 1) {
String arg = input.args()[0];
if (arg.equals("-?") || arg.equals("--help")) {
printer.println(commandDescs.get(Command.CONSOLE));
return;
} else {
throw new IllegalArgumentException("Unknown command parameter: " + input.args()[0]);
}
}
Console c = new Console();
c.run();
}

public Object inspect(CommandInput input) {
if (input.xargs().length == 0) {
return null;
Expand All @@ -91,33 +122,53 @@ public Object inspect(CommandInput input) {
if (input.args().length < id + 1) {
throw new IllegalArgumentException("Wrong number of command parameters: " + input.args().length);
}
Object obj = input.xargs()[id];
ObjectInspector inspector = new ObjectInspector(obj);
Object out = option;
if (option.equals("-m") || option.equals("--methods")) {
out = inspector.methods();
} else if (option.equals("-n") || option.equals("--metaMethods")) {
out = inspector.metaMethods();
} else if (option.equals("-i") || option.equals("--info")) {
out = inspector.properties();
} else if (option.equals("-g") || option.equals("--gui")) {
// groovy.inspect.swingui.ObjectBrowser.inspect(obj);
} else {
throw new IllegalArgumentException("Unknown option: " + option);
try {
Object obj = input.xargs()[id];
ObjectInspector inspector = new ObjectInspector(obj);
Object out = option;
if (option.equals("-m") || option.equals("--methods")) {
out = inspector.methods();
} else if (option.equals("-n") || option.equals("--metaMethods")) {
out = inspector.metaMethods();
} else if (option.equals("-i") || option.equals("--info")) {
out = inspector.properties();
} else if (consoleUi && (option.equals("-g") || option.equals("--gui"))) {
ObjectBrowser.inspect(obj);
} else {
throw new IllegalArgumentException("Unknown option: " + option);
}
Map<String,Object> options = new HashMap<>();
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
options.put(Printer.COLUMNS, ObjectInspector.METHOD_COLUMNS);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
printer.println(options, out);
} catch (Exception e) {
saveException(e);
}
Map<String,Object> options = new HashMap<>();
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
options.put(Printer.COLUMNS, ObjectInspector.METHOD_COLUMNS);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
printer.println(options, out);
return null;
}

private CmdDesc consoleCmdDesc() {
Map<String,List<AttributedString>> optDescs = new HashMap<>();
optDescs.put("-? --help", doDescription ("Displays command help"));
CmdDesc out = new CmdDesc(new ArrayList<>(), optDescs);
List<AttributedString> mainDesc = new ArrayList<>();
List<String> info = new ArrayList<>();
info.add("Launch Groovy console");
commandInfos.put(Command.CONSOLE, info);
mainDesc.add(new AttributedString("console - " + info.get(0)));
mainDesc.add(new AttributedString("Usage: console"));
out.setMainDesc(mainDesc);
return out;
}

private CmdDesc inspectCmdDesc() {
Map<String,List<AttributedString>> optDescs = new HashMap<>();
optDescs.put("-? --help", doDescription ("Displays command help"));
optDescs.put("-g --gui", doDescription ("Open object browser"));
if (consoleUi) {
optDescs.put("-g --gui", doDescription ("Launch object browser"));
}
optDescs.put("-i --info", doDescription ("Object class info"));
optDescs.put("-m --methods", doDescription ("List object methods"));
optDescs.put("-n --metaMethods", doDescription ("List object metaMethads"));
Expand Down Expand Up @@ -167,4 +218,13 @@ public List<Completer> inspectCompleter(String command) {
out.add(ac);
return out;
}

public List<Completer> consoleCompleter(String command) {
List<Completer> out = new ArrayList<>();
ArgumentCompleter ac = new ArgumentCompleter(NullCompleter.INSTANCE
, new StringsCompleter("--help", "-?")
, NullCompleter.INSTANCE);
out.add(ac);
return out;
}
}
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<gogo.jline.version>1.1.4</gogo.jline.version>
<slf4j.version>1.7.21</slf4j.version>
<findbugs.version>3.0.2</findbugs.version>
<groovy.version>3.0.2</groovy.version>
<groovy.version>3.0.3</groovy.version>

<surefire.argLine />
</properties>
Expand Down Expand Up @@ -239,6 +239,12 @@
<version>${groovy.version}</version>
</dependency>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-console</artifactId>
<version>${groovy.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 43748c7

Please sign in to comment.