Skip to content

Commit

Permalink
Added Groovy REPL in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 22, 2020
1 parent ce13d2a commit c77a376
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 23 deletions.
4 changes: 4 additions & 0 deletions build.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ function command_rebuild {
function command_demo() {
exec demo/jline-gogo.sh $*
}

function command_repl() {
exec demo/jline-repl.sh $*
}
51 changes: 37 additions & 14 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,31 +533,53 @@ private Map<String,Object> defaultPrntOptions() {

@Override
public void println(Object object) {
println(defaultPrntOptions(), object);
Map<String,Object> options = defaultPrntOptions();
options.put("exception", "message");
println(options, object);
}

@Override
public void println(Map<String, Object> options, Object object) {
if (object == null) {
return;
}
if (object instanceof Exception) {
((Exception) object).printStackTrace();
} else {
options.putIfAbsent("width", terminal.getSize().getColumns());
String style = (String)options.getOrDefault("style", "");
int width = (int)options.get("width");
if (style.equalsIgnoreCase("JSON")) {
highlight(width, style, engine.format(options, object));
} else if (!style.isEmpty() && object instanceof String) {
highlight(width, style, (String)object);
options.putIfAbsent("width", terminal.getSize().getColumns());
String style = (String) options.getOrDefault("style", "");
int width = (int) options.get("width");
if (style.equalsIgnoreCase("JSON")) {
highlight(width, style, engine.format(options, 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")) {
((Exception) object).printStackTrace();
} else {
for (AttributedString as : engine.highlight(options, object)) {
as.println(terminal);
String message = ((Exception) object).getMessage();
AttributedStringBuilder asb = new AttributedStringBuilder();
if (message != null) {
asb.append(message, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
} else {
asb.append("Caught exception: ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
asb.append(object.getClass().getCanonicalName(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
}
asb.toAttributedString().println(terminal);
}
} else if (object instanceof String) {
highlight(AttributedStyle.YELLOW + AttributedStyle.BRIGHT, object);
} else if (object instanceof Number) {
highlight(AttributedStyle.BLUE + AttributedStyle.BRIGHT, object);
} else {
for (AttributedString as : engine.highlight(options, object)) {
as.println(terminal);
}
terminal.flush();
}
terminal.flush();
}

private void highlight(int attrStyle, Object obj) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(obj.toString(), AttributedStyle.DEFAULT.foreground(attrStyle));
asb.toAttributedString().println(terminal);
}

private void highlight(int width, String style, String object) {
Expand Down Expand Up @@ -652,6 +674,7 @@ private Object prnt(Builtins.CommandInput input) {
if (opt.isSet("rownum")) {
options.put("rownum", true);
}
options.put("exception", "stack");
List<Object> args = opt.argObjects();
if (args.size() > 0) {
println(options, args.get(0));
Expand Down
9 changes: 9 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;

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

/**
* Aggregate command registries and dispatch command executions.
Expand All @@ -28,6 +29,14 @@ public interface SystemRegistry extends CommandRegistry {
public void initialize(File script);

/**
* Returns a command, method or syntax description for use in the JLine Widgets framework.
* @param command line whose description to return
* @return command description for JLine TailTipWidgets to be displayed
* in the terminal status bar.
*/
public Widgets.CmdDesc commandDescription(Widgets.CmdLine line);

/**
* Execute a command, script or evaluate scriptEngine statement
* @param parsedLine
* @return result
Expand Down
16 changes: 16 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ public Widgets.CmdDesc commandDescription(String command) {
return id > -1 ? commandRegistries[id].commandDescription(command) : new Widgets.CmdDesc(false);
}

@Override
public Widgets.CmdDesc commandDescription(Widgets.CmdLine line) {
Widgets.CmdDesc out = null;
switch (line.getDescriptionType()) {
case COMMAND:
String cmd = Parser.getCommand(line.getArgs().get(0));
out = commandDescription(cmd);
break;
case METHOD:
case SYNTAX:
// TODO
break;
}
return out;
}

@Override
public Object invoke(String command, Object... args) throws Exception {
Object out = null;
Expand Down
74 changes: 74 additions & 0 deletions demo/jline-repl.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@echo off

set DIRNAME=%~dp0%
set ROOTDIR=%DIRNAME%\..
set TARGETDIR=%DIRNAME%target

rem initialization
if not exist %TARGETDIR%\lib (
echo Build jline with maven before running the demo
goto END
)

goto :SETUP_CLASSPATH

: APPEND_TO_CLASSPATH
set filename=%~1
set cp=%cp%;%TARGETDIR%\lib\%filename%
goto :EOF

:SETUP_CLASSPATH
set cp=%TARGETDIR%\classes
rem JLINE
pushd %TARGETDIR%\lib
for %%G in (jline-*.jar) do call:APPEND_TO_CLASSPATH %%G
rem Groovy
for %%G in (groovy-*.jar) do call:APPEND_TO_CLASSPATH %%G

set "opts=%JLINE_OPTS%"
set "logconf=%DIRNAME%etc\logging.properties"
:RUN_LOOP
if "%1" == "jansi" goto :EXECUTE_JANSI
if "%1" == "jna" goto :EXECUTE_JNA
if "%1" == "debug" goto :EXECUTE_DEBUG
if "%1" == "debugs" goto :EXECUTE_DEBUGS
if "%1" == "verbose" goto :EXECUTE_VERBOSE
if "%1" == "" goto :EXECUTE_MAIN
set "opts=%opts% %~1"
shift
goto :RUN_LOOP

:EXECUTE_JANSI
for %%G in (jansi-*.jar) do call:APPEND_TO_CLASSPATH %%G
shift
goto :RUN_LOOP

:EXECUTE_JNA
for %%G in (jna-*.jar) do call:APPEND_TO_CLASSPATH %%G
shift
goto :RUN_LOOP

:EXECUTE_DEBUG
set "opts=%opts% -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
shift
goto :RUN_LOOP

:EXECUTE_DEBUGS
set "opts=%opts% -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
shift
goto :RUN_LOOP

:EXECUTE_VERBOSE
set "logconf=%DIRNAME%etc\logging-verbose.properties"
shift
goto :RUN_LOOP

:EXECUTE_MAIN
popd

rem Launching Groovy REPL...
echo Launching Groovy REPL...
echo Classpath: %cp%
java -cp %cp% %opts% -Dgosh.home=%DIRNAME% -Djava.util.logging.config.file=%logconf% org.jline.demo.Repl

:END
93 changes: 93 additions & 0 deletions demo/jline-repl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/sh

realpath() {
OURPWD=${PWD}
cd "$(dirname "${1}")"
LINK=$(readlink "$(basename "${1}")")
while [ "${LINK}" ]; do
cd "$(dirname "${LINK}")"
LINK=$(readlink "$(basename "${1}")")
done
REALPATH="${PWD}/$(basename "${1}")"
cd "${OURPWD}"
echo "${REALPATH}"
}

REALNAME=$(realpath "$0")
DIRNAME=$(dirname "${REALNAME}")
PROGNAME=$(basename "${REALNAME}")
ROOTDIR=${DIRNAME}/..
TARGETDIR=${DIRNAME}/target

if [ ! -e ${TARGETDIR}/lib ] ; then
echo "Build jline with maven before running the demo"
exit
fi;

cp=${TARGETDIR}/classes
# JLINE
cp=${cp}$(find ${TARGETDIR}/lib -name "jline-*.jar" -exec printf :{} ';')
cp=${cp}$(find ${TARGETDIR}/lib -name "groovy-*.jar" -exec printf :{} ';')

opts="${JLINE_OPTS}"
logconf="${DIRNAME}/etc/logging.properties"
while [ "${1}" != "" ]; do
case ${1} in
'debug')
opts="${opts} -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
shift
;;
'debugs')
opts="${opts} -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
shift
;;
'jansi')
cp=${cp}$(find ${TARGETDIR}/lib -name "jansi-*.jar" -exec printf :{} ';')
shift
;;
'jna')
cp=${cp}$(find ${TARGETDIR}/lib -name "jna-*.jar" -exec printf :{} ';')
shift
;;
'verbose')
logconf="${DIRNAME}/etc/logging-verbose.properties"
shift
;;
*)
opts="${opts} ${1}"
shift
;;
esac
done

cygwin=false
mingw=false
case "$(uname)" in
CYGWIN*)
cygwin=true
;;
MINGW*)
mingw=true
;;
esac
if ${cygwin}; then
cp=$(cygpath --path --windows "${cp}")
DIRNAME=$(cygpath --path --windows "${DIRNAME}")
fi

nothing() {
# nothing to do here
a=a
}
trap 'nothing' TSTP

# Launch Groovy REPL
echo "Launching Groovy REPL..."
echo "Classpath: $cp"
set mouse=a
java -cp $cp \
$opts \
-Dgosh.home="${DIRNAME}" \
-Djava.util.logging.config.file="${logconf}" \
org.jline.demo.Repl

17 changes: 16 additions & 1 deletion demo/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-2017, 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 All @@ -28,6 +28,11 @@
<artifactId>jline</artifactId>
</dependency>

<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-groovy</artifactId>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.runtime</artifactId>
Expand Down Expand Up @@ -68,6 +73,16 @@
<artifactId>sshd-sftp</artifactId>
</dependency>

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

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

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

0 comments on commit c77a376

Please sign in to comment.