Skip to content

Commit

Permalink
Command output redirection to file
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 4, 2020
1 parent cea2ea9 commit abdd432
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
5 changes: 5 additions & 0 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ default Object execute(File script) throws Exception {
*/
Object getVariable(String name);

/**
* Delete temporary console variables
*/
void purge();

/**
* Execute widget function
* @param function to execute
Expand Down
13 changes: 9 additions & 4 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private Parser parser() {
private Terminal terminal() {
return systemRegistry.terminal();
}

public boolean isExecuting() {
return executing;
}
Expand Down Expand Up @@ -585,6 +585,11 @@ public Object execute(ParsedLine pl) throws Exception {
}
return out;
}

@Override
public void purge() {
engine.del("_*");
}

@Override
public Object getVariable(String name) {
Expand Down Expand Up @@ -624,10 +629,10 @@ public Object postProcess(String line, Object result, String output) {
engine.put(Parser.getVariable(line), output);
}
out = null;
}
}
return out;
}

private Object postProcess(String line, Object result) {
Object out = result instanceof String && ((String)result).trim().length() == 0 ? null : result;
if (Parser.getVariable(line) != null) {
Expand Down Expand Up @@ -676,7 +681,7 @@ private Map<String,Object> defaultPrntOptions() {
@Override
public void println(Object object) {
Map<String,Object> options = defaultPrntOptions();
options.putIfAbsent("exception", "stack");
options.putIfAbsent("exception", "message");
println(options, object);
}

Expand Down
4 changes: 4 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public interface SystemRegistry extends CommandRegistry {
*/
Object execute(String line) throws Exception;

/**
* Delete temporary console variables and reset output streams
*/
void cleanUp();
/**
*
* @return terminal
Expand Down
89 changes: 77 additions & 12 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
*/
package org.jline.builtins;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -35,8 +38,6 @@
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.InfoCmp.Capability;

/**
* Aggregate command registeries.
Expand Down Expand Up @@ -312,7 +313,9 @@ private static class CommandOutputStream {
private PrintStream origErr;
private Terminal origTerminal;
private ByteArrayOutputStream byteOutputStream;
private FileOutputStream fileOutputStream;
private PrintStream out;
private InputStream in;
private Terminal terminal;
private String output;
private CommandRegistry.CommandSession commandSession;
Expand All @@ -327,27 +330,64 @@ public CommandOutputStream(Terminal terminal) {
this.commandSession = new CommandRegistry.CommandSession(terminal, terminal.input(), ps, ps);
}

public void redirect(String var) throws IOException {
public void redirect() throws IOException {
byteOutputStream = new ByteArrayOutputStream();
out = new PrintStream(byteOutputStream);
doTerminal(byteOutputStream);
}

public void redirect(File file, boolean append) throws IOException {
if (!file.exists()){
try {
file.createNewFile();
} catch(IOException e){
(new File(file.getParent())).mkdirs();
file.createNewFile();
}
}
fileOutputStream = new FileOutputStream(file, append);
doTerminal(fileOutputStream);
}

void doTerminal(OutputStream outputStream) throws IOException {
out = new PrintStream(outputStream);
System.setOut(out);
System.setErr(out);
terminal = TerminalBuilder.builder().streams(System.in, out).type(Terminal.TYPE_DUMB).build();
in = new ByteArrayInputStream( "".getBytes() );
terminal = TerminalBuilder.builder().streams(in, outputStream).type(Terminal.TYPE_DUMB).build();
this.commandSession = new CommandRegistry.CommandSession(terminal, terminal.input(), out, out);
redirecting = true;
}

public void flush() {
if (out == null) {
return;
}
try {
out.flush();
if (out instanceof PrintStream && byteOutputStream != null) {
if (byteOutputStream != null) {
byteOutputStream.flush();
output = byteOutputStream.toString();
} else if (fileOutputStream != null) {
fileOutputStream.flush();
}
} catch (Exception e) {

}
}

public void close() {
if (out == null) {
return;
}
try {
in.close();
flush();
if (byteOutputStream != null) {
byteOutputStream.close();
byteOutputStream = null;
} else if (fileOutputStream != null) {
fileOutputStream.close();
fileOutputStream = null;
}
out.close();
out = null;
Expand All @@ -368,6 +408,7 @@ public void reset() {
if (redirecting) {
out = null;
byteOutputStream = null;
fileOutputStream = null;
output = null;
System.setOut(origOut);
System.setErr(origErr);
Expand All @@ -391,12 +432,28 @@ public Object execute(String line) throws Exception {
pl = parser.parse(line.replaceFirst(cmd, consoleEngine().getAlias(cmd)), 0, ParseContext.ACCEPT_LINE);
cmd = ConsoleEngine.plainCommand(Parser.getCommand(pl.word()));
}
String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
File toFile = null;
boolean append = false;
List<String> words = pl.words();
int lastArg = words.size();
for (int i = 1; i < words.size() - 1; i++) {
if (words.get(i).equals(">") || words.get(i).equals(">>")) {
lastArg = i;
append = words.get(i).equals(">>");
toFile = new File(words.get(i + 1));
break;
}
}
String[] argv = words.subList(1, lastArg).toArray(new String[0]);
Object out = null;
exception = null;
try {
if (var != null && consoleId != null && !consoleEngine().isExecuting()) {
outputStream.redirect(var);
if ((var != null || toFile != null) && consoleId != null && !consoleEngine().isExecuting()) {
if (toFile != null) {
outputStream.redirect(toFile, append);
} else {
outputStream.redirect();
}
}
if (isLocalCommand(cmd)) {
out = localExecute(cmd, argv);
Expand All @@ -419,11 +476,19 @@ public Object execute(String line) throws Exception {
if (consoleId != null && !consoleEngine().isExecuting()) {
outputStream.flush();
out = consoleEngine().postProcess(pl.line(), out, outputStream.getOutput());
outputStream.reset();
}
}
return out;
}

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

private void println(Exception exception) {
if (consoleId != null) {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public static void main(String[] args) {
consoleEngine.println(terminal.getName()+": "+terminal.getType());
while (true) {
try {
scriptEngine.del("_*"); // delete temporary variables
systemRegistry.cleanUp(); // delete temporary variables and reset output streams
String line = reader.readLine("groovy-repl> ");
Object result = systemRegistry.execute(line);
consoleEngine.println(result);
Expand Down

0 comments on commit abdd432

Please sign in to comment.