Skip to content

Commit

Permalink
readLine() ignores any text in the buffer when OEF is reached, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 30, 2020
1 parent 333cb28 commit 3f399ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
28 changes: 19 additions & 9 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jline.builtins.*;
import org.jline.builtins.Completers.OptionCompleter;
Expand All @@ -47,6 +46,7 @@
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.script.GroovyCommand;
import org.jline.script.GroovyEngine;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.Terminal.Signal;
Expand All @@ -65,7 +65,7 @@ public class Repl {

protected static class MyCommands extends JlineCommandRegistry implements CommandRegistry {
private LineReader reader;
private Supplier<Path> workDir;
private final Supplier<Path> workDir;

public MyCommands(Supplier<Path> workDir) {
super();
Expand Down Expand Up @@ -177,7 +177,7 @@ private void executeCmnd(List<String> args) throws Exception {
_args.add("sh");
_args.add("-c");
}
_args.add(args.stream().collect(Collectors.joining(" ")));
_args.add(String.join(" ", args));
builder.command(_args);
builder.directory(workDir.get().toFile());
Process process = builder.start();
Expand All @@ -200,8 +200,7 @@ private void shell(CommandInput input) {
saveException(e);
}
} else {
List<String> argv = new ArrayList<>();
argv.addAll(Arrays.asList(input.args()));
List<String> argv = new ArrayList<>(Arrays.asList(input.args()));
if (!argv.isEmpty()) {
try {
executeCmnd(argv);
Expand Down Expand Up @@ -229,8 +228,8 @@ private List<Completer> tputCompleter(String command) {
}

private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
private final InputStream inputStream;
private final Consumer<String> consumer;

public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
Expand Down Expand Up @@ -259,6 +258,9 @@ public static void main(String[] args) {
parser.setEscapeChars(null);
parser.setRegexCommand("[:]{0,1}[a-zA-Z!]{1,}\\S*"); // change default regex to support shell commands
Terminal terminal = TerminalBuilder.builder().build();
if (terminal.getWidth() == 0 || terminal.getHeight() == 0) {
terminal.setSize(new Size(120, 40)); // hard coded terminal size when redirecting
}
Thread executeThread = Thread.currentThread();
terminal.handle(Signal.INT, signal -> executeThread.interrupt());
//
Expand All @@ -273,7 +275,7 @@ public static void main(String[] args) {
ConsoleEngineImpl consoleEngine = new ConsoleEngineImpl(scriptEngine
, printer
, Repl::workDir, configPath);
Builtins builtins = new Builtins(Repl::workDir, configPath, (String fun)-> {return new ConsoleEngine.WidgetCreator(consoleEngine, fun);});
Builtins builtins = new Builtins(Repl::workDir, configPath, (String fun)-> new ConsoleEngine.WidgetCreator(consoleEngine, fun));
MyCommands myCommands = new MyCommands(Repl::workDir);
SystemRegistryImpl systemRegistry = new SystemRegistryImpl(parser, terminal, Repl::workDir, configPath);
systemRegistry.register("groovy", new GroovyCommand(scriptEngine, printer));
Expand Down Expand Up @@ -315,7 +317,7 @@ public static void main(String[] args) {
//
// REPL-loop
//
consoleEngine.println(terminal.getName()+": "+terminal.getType());
System.out.println(terminal.getName() + ": " + terminal.getType());
while (true) {
try {
systemRegistry.cleanUp(); // delete temporary variables and reset output streams
Expand All @@ -328,6 +330,14 @@ public static void main(String[] args) {
// Ignore
}
catch (EndOfFileException e) {
String pl = e.getPartialLine();
if (pl != null) { // execute last line from redirected file (required for Windows)
try {
consoleEngine.println(systemRegistry.execute(pl));
} catch (Exception e2) {
systemRegistry.trace(e2);
}
}
break;
}
catch (Exception e) {
Expand Down
12 changes: 11 additions & 1 deletion reader/src/main/java/org/jline/reader/EndOfFileException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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 All @@ -15,6 +15,7 @@
public class EndOfFileException extends RuntimeException {

private static final long serialVersionUID = 528485360925144689L;
private String partialLine;

public EndOfFileException() {
}
Expand All @@ -34,4 +35,13 @@ public EndOfFileException(Throwable cause) {
public EndOfFileException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public EndOfFileException partialLine(String partialLine) {
this.partialLine = partialLine;
return this;
}

public String getPartialLine() {
return partialLine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ public String readLine(String prompt, String rightPrompt, MaskingCallback maskin
}
Binding o = readBinding(getKeys(), local);
if (o == null) {
throw new EndOfFileException();
throw new EndOfFileException().partialLine(buf.length() > 0 ? buf.toString() : null);
}
Log.trace("Binding: ", o);
if (buf.length() == 0 && getLastBinding().charAt(0) == originalAttributes.getControlChar(ControlChar.VEOF)) {
Expand Down

0 comments on commit 3f399ac

Please sign in to comment.