Добавлен Formatter
This commit is contained in:
@@ -21,6 +21,7 @@ public class Shell {
|
||||
protected ConsoleReader console;
|
||||
private CommandLoop commandLoop;
|
||||
private CommandCompleter commandCompleter;
|
||||
private Formatter formatter;
|
||||
protected boolean run = false;
|
||||
|
||||
public void start() throws IOException, InterruptedException {
|
||||
@@ -70,7 +71,11 @@ public class Shell {
|
||||
}
|
||||
|
||||
public void setFormatter(Formatter formatter) {
|
||||
newErr.setFormatter(formatter);
|
||||
if (newErr != null) {
|
||||
newErr.setFormatter(formatter);
|
||||
} else {
|
||||
this.formatter = formatter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,6 +84,7 @@ public class Shell {
|
||||
public void overrideSysOutErr() {
|
||||
sysErr = System.err;
|
||||
newErr = new ShellPrintStream(sysErr);
|
||||
if (formatter != null) newErr.setFormatter(formatter);
|
||||
System.setErr(newErr);
|
||||
System.setOut(newErr);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package test;
|
||||
|
||||
import org.fusesource.jansi.Ansi;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import ru.dmitriymx.shell.Formatter;
|
||||
import ru.dmitriymx.shell.Shell;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -85,4 +88,32 @@ public class TestShell {
|
||||
assertTrue(resultOut.contains("123"));
|
||||
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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user