Archived
0

Добавлен Formatter

This commit is contained in:
2015-12-16 13:42:09 +03:00
parent 7ce1ea74da
commit 07f37b207a
2 changed files with 38 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ public class Shell {
protected ConsoleReader console; protected ConsoleReader console;
private CommandLoop commandLoop; private CommandLoop commandLoop;
private CommandCompleter commandCompleter; private CommandCompleter commandCompleter;
private Formatter formatter;
protected boolean run = false; protected boolean run = false;
public void start() throws IOException, InterruptedException { public void start() throws IOException, InterruptedException {
@@ -70,7 +71,11 @@ public class Shell {
} }
public void setFormatter(Formatter formatter) { public void setFormatter(Formatter formatter) {
if (newErr != null) {
newErr.setFormatter(formatter); newErr.setFormatter(formatter);
} else {
this.formatter = formatter;
}
} }
/** /**
@@ -79,6 +84,7 @@ public class Shell {
public void overrideSysOutErr() { public void overrideSysOutErr() {
sysErr = System.err; sysErr = System.err;
newErr = new ShellPrintStream(sysErr); newErr = new ShellPrintStream(sysErr);
if (formatter != null) newErr.setFormatter(formatter);
System.setErr(newErr); System.setErr(newErr);
System.setOut(newErr); System.setOut(newErr);
} }

View File

@@ -1,12 +1,15 @@
package test; package test;
import org.fusesource.jansi.Ansi;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import ru.dmitriymx.shell.Formatter;
import ru.dmitriymx.shell.Shell; import ru.dmitriymx.shell.Shell;
import java.io.*; import java.io.*;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
@@ -85,4 +88,32 @@ public class TestShell {
assertTrue(resultOut.contains("123")); assertTrue(resultOut.contains("123"));
assertTrue(resultOut.contains("Unknown command")); assertTrue(resultOut.contains("Unknown command"));
} }
@Test
public void testFormatter() throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream shellout = new PrintStream(baos);
System.setOut(shellout);
System.setErr(shellout);
ByteArrayInputStream bais = new ByteArrayInputStream("qwe\nexit\n".getBytes());
System.setIn(bais);
Formatter formatter = s -> "FORMAT: [" + s + "]";
Shell shell = new Shell();
shell.setFormatter(formatter);
shell.overrideSysOutErr();
shell.start();
while (shell.isRunning()) {
Thread.sleep(1);
}
String resultOut = baos.toString();
returnSys();
assertTrue(resultOut.contains("FORMAT: ["));
assertTrue(resultOut.contains("Unknown command"));
}
} }