Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 18, 2020
1 parent a526e18 commit 9546e89
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 3 deletions.
2 changes: 1 addition & 1 deletion builtins/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2002-2016, the original author or authors.
Copyright (c) 2002-2020, 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.
Expand Down
2 changes: 1 addition & 1 deletion builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, 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.
Expand Down
21 changes: 21 additions & 0 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import java.util.Map;
import java.util.Set;

/**
* Store command information, compile tab completers and execute registered commands.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public interface CommandRegistry {

/**
Expand Down Expand Up @@ -80,10 +85,26 @@ static Completers.SystemCompleter compileCompleters(CommandRegistry ... commandR
*/
Widgets.CmdDesc commandDescription(String command);

/**
* Execute a command entered interactively or via JLine script. Implementation of the method is required when aggregating
* command registries using systemRegistry.
* @param command
* @param args
* @return result
* @throws Exception
*/
default Object execute(String command, String[] args) throws Exception {
throw new IllegalArgumentException("CommandRegistry method execute(String command, String[] args) is not implemented!");
}

/**
* Execute a command inside a code block or script engine script. If command has other than string arguments custom
* implementation is required.
* @param command
* @param args
* @return result
* @throws Exception
*/
default Object invoke(String command, Object... args) throws Exception {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
Expand Down
47 changes: 47 additions & 0 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,69 @@
import org.jline.builtins.CommandRegistry;
import org.jline.reader.ParsedLine;

/**
* Manage console variables, commands and script executions.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public interface ConsoleEngine extends CommandRegistry {

/**
* Sets systemRegistry
* @param systemRegistry
*/
void setSystemRegistry(SystemRegistry systemRegistry);

/**
* Executes parsed line that does not contain known command by the system registry.
* If parsed line is neither JLine or ScriptEngine script it will be evaluated
* as ScriptEngine statement.
* @param parsedLine
* @return result
* @throws Exception
*/
Object execute(ParsedLine parsedLine) throws Exception;

/**
* Executes either JLine or ScriptEngine script.
* @param script
* @return result
* @throws Exception
*/
default Object execute(File script) throws Exception {
return execute(script, "", new String[0]);
}

/**
* Executes either JLine or ScriptEngine script.
* @param script
* @param cmdLine
* @param args
* @return result
* @throws Exception
*/
Object execute(File script, String cmdLine, String[] args) throws Exception;

/**
* Post processes execution result. If result is to be assigned to the console variable
* then method will return null.
* @param line
* @param result
* @return result
*/
Object postProcess(String line, Object result);

/**
* Displays object.
* @param object
*/
void println(Object object);

/**
* Displays object.
* @param options
* @param object
*/
void println(Map<String, Object> options, Object object);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down
28 changes: 28 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,52 @@

import org.jline.reader.ParsedLine;

/**
* Aggregate command registries and dispatch command executions.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public interface SystemRegistry extends CommandRegistry {

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

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

/**
* @return systemRegistry of the current thread
*/
static SystemRegistry get() {
return Registeries.getInstance().getSystemRegistry();
}

/**
* Add systemRegistry to the thread map
* @param systemRegistry
*/
static void add(SystemRegistry systemRegistry) {
Registeries.getInstance().addRegistry(systemRegistry);
}

/**
* Remove systemRegistry from the thread map
*/
static void remove() {
Registeries.getInstance().removeRegistry();
}

/**
* Manage systemRegistry store
*/
public class Registeries {
private static Registeries instance = new Registeries();
private Map<Long, SystemRegistry> systemRegisteries = new HashMap<>();
Expand Down
10 changes: 10 additions & 0 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-2020, 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 Down
2 changes: 2 additions & 0 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

/**
* Implements Groovy ScriptEngine.
* You must be very careful when using GroovyEngine in a multithreaded environment. The Binding instance is not
* thread safe, and it is shared by all scripts.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
Expand Down
67 changes: 67 additions & 0 deletions reader/src/main/java/org/jline/reader/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,103 @@
*/
public interface ScriptEngine {

/**
*
* @return scriptEngine name
*/
String getEngineName();

/**
*
* @return script file name extensions
*/
Collection<String> getExtensions();

/**
* Tests if console variable exists
* @param name
* @return true if variable exists
*/
boolean hasVariable(String name);

/**
* Creates variable
* @param name of the variable
* @param value of the variable
*/
void put(String name, Object value);

/**
* Gets variable value
* @param name of the variable
* @return value of the variable
*/
Object get(String name);

/**
* Gets all variables with values
* @return map of the variables
*/
default Map<String,Object> find() {
return find(null);
}

/**
* Gets all the variables that match the name. Name can contain * wild cards.
* @param name of the variable
* @return map of the variables
*/
Map<String,Object> find(String name);

/**
* Deletes variables. Variable name cab contain * wild cards.
* @param vars
*/
void del(String... vars);

/**
* Prepares formatted string of the object.
* @param options for string formatting
* @param object to display
* @return formatted string
*/
String format(Map<String, Object> options, Object object);

/**
* Prepares highlighted string of the object
* @param options for highlighting
* @param object to highlight
* @return highlighted string
*/
List<AttributedString> highlight(Map<String, Object> options, Object object);

Object expandParameter(String variable);

/**
* Executes scriptEngine statement
* @param statement
* @return result
* @throws Exception
*/
Object execute(String statement) throws Exception;

/**
* Executes scriptEngine script
* @param script
* @return result
* @throws Exception
*/
default Object execute(File script) throws Exception {
return execute(script, null);
}

/**
* Executes scriptEngine script
* @param script
* @param args
* @return
* @throws Exception
*/
Object execute(File script, Object[] args) throws Exception;

static List<Map<String, Object>> listEngines() {
Expand Down

0 comments on commit 9546e89

Please sign in to comment.