* improved support for non-standard console environments when using the scripting shell

This commit is contained in:
Reinhard Pointner 2014-12-30 18:13:06 +00:00
parent ec9c07638c
commit 3cddda03c9
2 changed files with 68 additions and 3 deletions

View File

@ -0,0 +1,65 @@
package net.filebot.cli;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.charset.Charset;
/**
* A {@link Console} like class for when {@link System#console()} is not available in the current JVM
*
* @see Console
*/
public class PseudoConsole {
private final BufferedReader reader;
private final PrintWriter writer;
public PseudoConsole(InputStream in, PrintStream out, Charset cs) {
reader = new BufferedReader(new InputStreamReader(in, cs));
writer = new PrintWriter(new OutputStreamWriter(out, cs), true);
}
public PrintWriter writer() {
return writer;
}
public Reader reader() {
return reader;
}
public PrintWriter format(String fmt, Object... args) {
return writer.format(fmt, args);
}
public PrintWriter printf(String format, Object... args) {
return writer.printf(format, args);
}
public String readLine(String fmt, Object... args) {
throw new UnsupportedOperationException();
}
public String readLine() throws IOException {
return reader.readLine();
}
public char[] readPassword(String fmt, Object... args) {
throw new UnsupportedOperationException();
}
public char[] readPassword() {
throw new UnsupportedOperationException();
}
public void flush() {
writer.flush();
}
}

View File

@ -9,7 +9,6 @@ import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import groovy.xml.MarkupBuilder;
import java.io.Console;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
@ -17,6 +16,7 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumMap;
@ -189,8 +189,8 @@ public abstract class ScriptShellBaseClass extends Script {
}
// define global variable: console
public Console getConsole() {
return System.console();
public Object getConsole() {
return System.console() != null ? System.console() : new PseudoConsole(System.in, System.out, StandardCharsets.UTF_8);
}
public Date getNow() {