Skip to content

Commit

Permalink
jline-script
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 21, 2019
1 parent 78368e4 commit 5e0eb5e
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 2 deletions.
6 changes: 5 additions & 1 deletion builtins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.jline</groupId>
<artifactId>jline-style</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
</dependency>

<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
Expand Down Expand Up @@ -66,7 +70,7 @@
<arg>-Xlint:all,-options</arg>
<arg>-Werror</arg>
<arg>-profile</arg>
<arg>compact1</arg>
<arg>compact3</arg>
</compilerArgs>
</configuration>
</execution>
Expand Down
10 changes: 10 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,14 @@ public PrintStream err() {

public static class CommandMethods {
Consumer<CommandInput> execute;
Function<CommandInput, Object> executeFunction;
Function<String, List<Completer>> compileCompleter;

public CommandMethods(Function<CommandInput, Object> execute, Function<String, List<Completer>> compileCompleter) {
this.executeFunction = execute;
this.compileCompleter = compileCompleter;
}

public CommandMethods(Consumer<CommandInput> execute, Function<String, List<Completer>> compileCompleter) {
this.execute = execute;
this.compileCompleter = compileCompleter;
Expand All @@ -542,6 +548,10 @@ public Consumer<CommandInput> execute() {
return execute;
}

public Function<CommandInput, Object> executeFunction() {
return executeFunction;
}

public Function<String, List<Completer>> compileCompleter() {
return compileCompleter;
}
Expand Down
133 changes: 133 additions & 0 deletions builtins/src/main/java/org/jline/builtins/ScriptCommands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.jline.builtins;

import java.nio.file.Path;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.Command;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.reader.Completer;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.LineReader;
import org.jline.reader.Widget;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.script.JLineEngine;

public class ScriptCommands implements CommandRegistry {
public enum Command {SET
, DEL
, ENGINES};
private final JLineEngine engine;
private Map<Command,String> commandName = new HashMap<>();
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;

public ScriptCommands(JLineEngine engine) {
this.engine = engine;
Set<Command> cmds = new HashSet<>(EnumSet.allOf(Command.class));
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
}
doNameCommand();
commandExecute.put(Command.ENGINES, new CommandMethods(this::engines, this::defaultCompleter));
commandExecute.put(Command.SET, new CommandMethods(this::set, this::defaultCompleter));
}

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;
}
return false;
}

private void doNameCommand() {
nameCommand = commandName.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
}

private Command command(String name) {
Command out = null;
if (!hasCommand(name)) {
throw new IllegalArgumentException("Command does not exists!");
}
if (aliasCommand.containsKey(name)) {
name = aliasCommand.get(name);
}
if (nameCommand.containsKey(name)) {
out = nameCommand.get(name);
} else {
throw new IllegalArgumentException("Command does not exists!");
}
return out;
}

public void rename(Command command, String newName) {
if (nameCommand.containsKey(newName)) {
throw new IllegalArgumentException("Duplicate command name!");
} else if (!commandName.containsKey(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
commandName.put(command, newName);
doNameCommand();
}

public void alias(String alias, String command) {
if (!nameCommand.keySet().contains(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
aliasCommand.put(alias, command);
}

public List<String> commandInfo(String command) {
return null;
}

public Completers.SystemCompleter compileCompleters() {
return null;
}

public Widgets.CmdDesc commandDescription(String command) {
return null;
}

public Object execute(String statement) throws Exception {
return engine.execute(statement);
}

public Object execute(String command, String[] args) throws Exception {
exception = null;
Object out = commandExecute.get(command(command)).executeFunction().apply(new Builtins.CommandInput(args));
if (exception != null) {
throw exception;
}
return out;
}

public Object engines(Builtins.CommandInput input) {
return JLineEngine.listEngines();
}

public Object set(Builtins.CommandInput input) {
return engine.get();
}

private List<Completer> defaultCompleter(String command) {
return Arrays.asList(NullCompleter.INSTANCE);
}

}
13 changes: 13 additions & 0 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Completers.TreeCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.ScriptCommands;
import org.jline.builtins.Widgets.ArgDesc;
import org.jline.builtins.Widgets.AutopairWidgets;
import org.jline.builtins.Widgets.AutosuggestionWidgets;
import org.jline.builtins.Widgets.CmdDesc;
import org.jline.builtins.Widgets.CmdLine;
import org.jline.builtins.Widgets.TailTipWidgets;
import org.jline.builtins.Widgets.TailTipWidgets.TipType;
import org.jline.script.impl.Groovy;
import org.jline.keymap.KeyMap;
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
Expand Down Expand Up @@ -641,6 +643,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
builtins.alias("zle", "widget");
builtins.alias("bindkey", "keymap");
ExampleCommands exampleCommands = new ExampleCommands();
ScriptCommands scriptCommands = new ScriptCommands(new Groovy());
MasterRegistry masterRegistry = new MasterRegistry(builtins, exampleCommands);
//
// Command completers
Expand Down Expand Up @@ -747,6 +750,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
ParsedLine pl = reader.getParser().parse(line, 0);
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
String cmd = Parser.getCommand(pl.word());
Object result = null;
if ("help".equals(cmd) || "?".equals(cmd)) {
masterRegistry.help();
}
Expand All @@ -756,6 +760,15 @@ else if (builtins.hasCommand(cmd)) {
else if (exampleCommands.hasCommand(cmd)) {
exampleCommands.execute(cmd, argv);
}
else if (scriptCommands.hasCommand(cmd)) {
result = scriptCommands.execute(cmd, argv);
}
else {
result = scriptCommands.execute(line);
}
if (result != null) {
System.out.println(result);
}
}
catch (HelpException e) {
HelpException.highlight(e.getMessage(), HelpException.defaultStyle()).print(terminal);
Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
Expand Down Expand Up @@ -499,12 +505,13 @@
<module>terminal-jna</module>
<module>terminal-jansi</module>
<module>reader</module>
<module>script</module>
<module>builtins</module>
<module>remote-ssh</module>
<module>remote-telnet</module>
<module>style</module>
<module>jline</module>
<module>demo</module>
</modules>
</modules>

</project>
58 changes: 58 additions & 0 deletions script/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<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.3-SNAPSHOT</version>
</parent>
<groupId>org.jline</groupId>
<artifactId>jline-script</artifactId>
<version>3.13.3-SNAPSHOT</version>
<name>jline-script</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groovy.version>2.5.8</groovy.version>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all,-options</arg>
<!--
<arg>-Werror</arg>
-->
<arg>-profile</arg>
<arg>compact1</arg>
</compilerArgs>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
40 changes: 40 additions & 0 deletions script/src/main/java/org/jline/script/JLineEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jline.script;

import java.io.File;
import java.util.*;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public interface JLineEngine {

public String getEngineName();

public void put(String name, Object value);

public Object get(String name);

public Map<String,Object> get();

public void del(String... vars);

public Object execute(String statement) throws Exception;

public Object execute(File script) throws Exception;

public static List<Map<String, Object>> listEngines() {
List<Map<String, Object>> out = new ArrayList<>();
ScriptEngineManager f = new ScriptEngineManager();
List<ScriptEngineFactory> engines = f.getEngineFactories();
for (ScriptEngineFactory engine : engines) {
Map<String,Object> e = new HashMap<>();
e.put("name", engine.getEngineName());
e.put("version", engine.getEngineVersion());
e.put("language", engine.getLanguageName());
e.put("extensions", engine.getExtensions());
e.put("nick-names", engine.getNames());
out.add(e);
}
return out;
}
}
Loading

0 comments on commit 5e0eb5e

Please sign in to comment.