Skip to content

Commit

Permalink
Groovy-REPL: A few small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 7, 2020
1 parent a78b234 commit 9bf0efe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static String plainCommand(String command) {
* Sets file name extension used by console scripts
* @param extension console script file extension
*/
public void setScriptExtension(String extension);
void setScriptExtension(String extension);

/**
* Returns true if alias 'name' exists
Expand Down
11 changes: 5 additions & 6 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public List<String> scripts() {
out.add(name.substring(0, name.lastIndexOf(".")));
}
} catch (Exception e) {
println(e);
systemRegistry.println(e);
}
return out;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ public boolean executeWidget(Object function) {
}
engine.execute("_widgetFunction()");
} catch (Exception e) {
println(e);
systemRegistry.println(e);
return false;
}
return true;
Expand All @@ -628,7 +628,7 @@ private boolean splitCommandOutput() {
out = (boolean) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault("splitOutput", true);
}
} catch (Exception e) {
println(new Exception("Bad CONSOLE_OPTION value: " + e.getMessage()));
systemRegistry.println(new Exception("Bad CONSOLE_OPTION value: " + e.getMessage()));
}
return out;
}
Expand Down Expand Up @@ -700,7 +700,6 @@ private Map<String,Object> defaultPrntOptions() {
@Override
public void println(Object object) {
Map<String,Object> options = defaultPrntOptions();
options.putIfAbsent("exception", "message");
println(options, object);
}

Expand Down Expand Up @@ -746,9 +745,9 @@ private void highlight(int width, String style, String object) {
}
SyntaxHighlighter highlighter = nanorc != null ? SyntaxHighlighter.build(nanorc, style)
: null;
for (String s: object.split("\n")) {
for (String s: object.split("\\r?\\n")) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(s).setLength(width);
asb.append(s).subSequence(0, width); // setLength(width) fill nul-chars at the end of line
if (highlighter != null) {
highlighter.highlight(asb).println(terminal());
} else {
Expand Down
39 changes: 26 additions & 13 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Attributes;
import org.jline.terminal.Attributes.InputFlag;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.OSUtils;

/**
* Aggregate command registeries.
Expand Down Expand Up @@ -355,7 +358,14 @@ public void open() throws IOException {
System.setOut(out);
System.setErr(out);
in = new ByteArrayInputStream( "".getBytes() );
terminal = TerminalBuilder.builder().streams(in, outputStream).type(Terminal.TYPE_DUMB).build();
Attributes attrs = new Attributes();
if (OSUtils.IS_WINDOWS) {
attrs.setInputFlag(InputFlag.IGNCR, true);
}
terminal = TerminalBuilder.builder()
.streams(in, outputStream)
.attributes(attrs)
.type(Terminal.TYPE_DUMB).build();
this.commandSession = new CommandRegistry.CommandSession(terminal, terminal.input(), out, out);
redirecting = true;
}
Expand Down Expand Up @@ -460,10 +470,10 @@ public Object execute(String line) throws Exception {
exception = null;
boolean statement = false;
try {
if ((var != null || toFile != null) && consoleId != null && !consoleEngine().isExecuting()) {
if (var != null || toFile != null) {
if (toFile != null) {
outputStream.redirect(toFile, append);
} else {
} else if (consoleId != null && !consoleEngine().isExecuting()) {
outputStream.redirect();
}
outputStream.open();
Expand Down Expand Up @@ -500,23 +510,26 @@ public Object execute(String line) throws Exception {
}

public void cleanUp() {
if (consoleId == null) {
return;
if (outputStream.isRedirecting()) {
outputStream.close();
outputStream.reset();
}
if (consoleId != null) {
consoleEngine().purge();
}
outputStream.close();
outputStream.reset();
consoleEngine().purge();
}

@Override
public void println(Exception exception) {
if (outputStream.isRedirecting()) {
outputStream.close();
outputStream.reset();
}
if (consoleId != null) {
if (outputStream.isRedirecting()) {
outputStream.close();
outputStream.reset();
}
consoleEngine().putVariable("exception", exception);
consoleEngine().println(exception);
Map<String, Object> options = new HashMap<>();
options.put("exception", "message");
consoleEngine().println(options, exception);
} else {
SystemRegistry.println(false, terminal(), exception);
}
Expand Down
15 changes: 9 additions & 6 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
asb.append("\t");
}
if (asb.columnLength() > width) {
asb.setLength(width);
asb.subSequence(0, width);
}
out.add(asb.toAttributedString());
Integer row = 0;
Expand All @@ -356,7 +356,7 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
asb2.append("\t");
}
if (asb2.columnLength() > width) {
asb2.setLength(width);
asb2.subSequence(0, width);
}
out.add(asb2.toAttributedString());
}
Expand Down Expand Up @@ -391,7 +391,7 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
asb.append("\t");
}
if (asb.columnLength() > width) {
asb.setLength(width);
asb.subSequence(0, width);
}
out.add(asb.toAttributedString());
}
Expand All @@ -407,7 +407,7 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
}
asb.append(Utils.toString(o));
if (asb.columnLength() > width) {
asb.setLength(width);
asb.subSequence(0, width);
}
out.add(asb.toAttributedString());
}
Expand Down Expand Up @@ -435,13 +435,16 @@ private List<AttributedString> highlightMap(Map<String, Object> map, int width)
for (Map.Entry<String, Object> entry : map.entrySet()) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
asb.append(entry.getKey(), AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
for (String v : Utils.toString(entry.getValue()).split("\n")) {
for (String v : Utils.toString(entry.getValue()).split("\\r?\\n")) {
asb.append("\t");
asb.append(v, AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW));
if (asb.columnLength() > width) {
asb.setLength(width);
asb.subSequence(0, width);
}
out.add(asb.toAttributedString());
if (map.size() > 1) {
break;
}
asb = new AttributedStringBuilder().tabs(Arrays.asList(0, max + 1));
}
}
Expand Down

0 comments on commit 9bf0efe

Please sign in to comment.