Skip to content

Commit

Permalink
REPL demo: initialization & added example scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 24, 2020
1 parent fe80245 commit a67db91
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Map;

import org.jline.reader.ParsedLine;
import org.jline.builtins.Widgets;
import org.jline.terminal.Terminal;

/**
* Aggregate command registries and dispatch command executions.
Expand All @@ -22,6 +22,12 @@
*/
public interface SystemRegistry extends CommandRegistry {

/**
* Set terminal
* @param terminal
*/
public void setTerminal(Terminal terminal);

/**
* Initialize consoleEngine environment by executing console script
* @param script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public SystemRegistryImpl(CommandRegistry... commandRegistries) {
SystemRegistry.add(this);
}

@Override
public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}
Expand Down
59 changes: 59 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,65 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-scripts</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/scripts</outputDirectory>
<resources>
<resource>
<directory>src/main/scripts</directory>
<includes>
<include>hello*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-nanorc</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/nanorc</outputDirectory>
<resources>
<resource>
<directory>src/main/scripts</directory>
<includes>
<include>*.nanorc</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-root</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/scripts</directory>
<includes>
<include>init.jline</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
27 changes: 22 additions & 5 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.demo;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -21,16 +22,18 @@
import org.jline.builtins.ConsoleEngine;
import org.jline.builtins.ConsoleEngineImpl;
import org.jline.builtins.Options;
import org.jline.builtins.SystemRegistryImpl;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.SystemRegistry;
import org.jline.builtins.SystemRegistryImpl;
import org.jline.builtins.Widgets.TailTipWidgets;
import org.jline.builtins.Widgets.TailTipWidgets.TipType;
import org.jline.keymap.KeyMap;
import org.jline.reader.Binding;
import org.jline.reader.Completer;
import org.jline.reader.ConfigurationPath;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
Expand All @@ -51,6 +54,7 @@
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.InfoCmp;
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.OSUtils;

/**
* Demo how to create REPL app with JLine.
Expand Down Expand Up @@ -243,17 +247,23 @@ public static void main(String[] args) {
//
DefaultParser parser = new DefaultParser();
parser.setEofOnUnclosedBracket(Bracket.CURLY, Bracket.ROUND, Bracket.SQUARE);
parser.setEofOnUnclosedQuote(true);
parser.setEscapeChars(null);
Terminal terminal = TerminalBuilder.builder().build();
//
// 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!
GroovyEngine scriptEngine = new GroovyEngine();
Builtins builtins = new Builtins(Paths.get(""), null, null);
ConsoleEngine consoleEngine = new ConsoleEngineImpl(scriptEngine, parser, terminal, ()->Paths.get(""), null);
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();
SystemRegistryImpl systemRegistry = new SystemRegistryImpl(consoleEngine, builtins, myCommands);
SystemRegistry systemRegistry = new SystemRegistryImpl(consoleEngine, builtins, myCommands);
systemRegistry.setTerminal(terminal);
// systemRegistry.initialize(new File("./example/init.jline"));
systemRegistry.initialize(Paths.get(root, "init.jline").toFile());
//
//
// LineReader
Expand All @@ -264,9 +274,16 @@ public static void main(String[] args) {
.parser(parser)
.variable(LineReader.SECONDARY_PROMPT_PATTERN, "%M%P > ")
.variable(LineReader.INDENTATION, 2)
.variable(LineReader.LIST_MAX, 100)
.variable(LineReader.HISTORY_FILE, Paths.get(root, "history"))
.option(Option.INSERT_BRACKET, true)
.option(Option.EMPTY_WORD_OPTIONS, false)
.option(Option.USE_FORWARD_SLASH, true) // use forward slash in directory separator
.option(Option.DISABLE_EVENT_EXPANSION, true)
.build();
if (OSUtils.IS_WINDOWS) {
reader.setVariable(LineReader.BLINK_MATCHING_PAREN, 0); // if enabled cursor remains in begin parenthesis
}
//
// complete command registeries
//
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ array.each {
:prnt $resp
}

println(${1})
println('hello ' + ${1:-world} + '!')

_return='ok'
12 changes: 12 additions & 0 deletions demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# All imports added here are imported also to your groovy shell
#
import java.nio.file.*;

PATH = [ROOT + 'scripts']

#
# write jnanorc configuration file
#
_jnanorc = Paths.get(ROOT, 'jnanorc').toFile()
_jnanorc << 'include ' + ROOT + 'nanorc/*.nanorc\n'
17 changes: 17 additions & 0 deletions demo/src/main/scripts/java.nanorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Here is an example for Java.
##
syntax "Java" "\.java$"
color green "\<(boolean|byte|char|double|float|int|long|new|short|this|transient|void)\>"
color red "\<(break|case|catch|continue|default|do|else|finally|for|if|return|switch|throw|try|while)\>"
color green,,faint "(([a-z]{2,}[.]{1}){2,10}([a-z]{2,}){0,1})"
color green "\<[A-Z]{0,2}([A-Z]{1}[a-z]+){1,}\>"
color cyan "\<(abstract|class|extends|final|implements|import|instanceof|interface|native|package|private|protected|public|static|strictfp|super|synchronized|throws|volatile)\>"
color red ""[^"]*""
color yellow "\<(true|false|null)\>"
color yellow "\<[A-Z]+([_]{1}[A-Z]+){0,}\>"
icolor yellow "\b(([1-9][0-9]+)|0+)\.[0-9]+\b" "\b[1-9][0-9]*\b" "\b0[0-7]*\b" "\b0x[1-9a-f][0-9a-f]*\b"
color blue "//.*"
color blue start="/\*" end="\*/"
color brightblue start="/\*\*" end="\*/"
color brightwhite,yellow "(FIXME|TODO|XXX)"
color ,green "[[:space:]]+$"
11 changes: 11 additions & 0 deletions demo/src/main/scripts/json.nanorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax "JSON" "\.json$"
header "^\{$"

color brightblue "\<[-]?[0-9]*([Ee][+-]?[0-9]+)?\>" "\<[-]?[0](\.[0-9]+)?\>"
color cyan "\<null\>"
color brightcyan "\<(true|false)\>"
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'"
color brightyellow "\"(\\"|[^"])*\"[[:space:]]*:" "'(\'|[^'])*'[[:space:]]*:"
color magenta "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
color ,green "[[:space:]]+$"
color ,red " + +| + +"
14 changes: 14 additions & 0 deletions demo/src/main/scripts/xml.nanorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Here is an example for xml files.
##

syntax "XML" ".*\.([jrs]?html?|xml|sgml?|rng)$"
color white "^.+$"
color green start="<" end=">"
color cyan "<[^> ]+"
color cyan ">"
color yellow start="<!DOCTYPE" end="[/]?>"
color yellow start="<!--" end="-->"
color red "&[^;]*;"

## Trailing spaces
color ,green "[[:space:]]+$"
1 change: 1 addition & 0 deletions hello.jline
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
println "Hello " + $1 + "!"
10 changes: 8 additions & 2 deletions 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-2018, 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 Expand Up @@ -396,6 +396,12 @@
<version>1.8.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down Expand Up @@ -424,7 +430,7 @@
<version>3.0.1</version>
<configuration>
<archive>
<manifestEntries>
<manifestEntries>
<Bundle-Name>JLine Sources Bundle</Bundle-Name>
<Bundle-SymbolicName>${project.groupId}.source</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
Expand Down

0 comments on commit a67db91

Please sign in to comment.