Archived
0

Добавил элемент форматирования

This commit is contained in:
2015-12-14 17:43:34 +03:00
parent ceecb83e5a
commit 59f71c64c7
3 changed files with 28 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
package ru.dmitriymx.shell;
/**
* @author DmitriyMX <mail@dmitriymx.ru>
* 2015
*/
public interface Formatter {
String format(String s);
}

View File

@@ -71,6 +71,10 @@ public class Shell {
return run;
}
public void setFormatter(Formatter formatter) {
newErr.setFormatter(formatter);
}
/**
* Подмена стандартных SysErr и SysOut
*/

View File

@@ -16,6 +16,7 @@ import java.io.PrintWriter;
public class ShellPrintStream extends PrintStream {
private ConsoleReader consoleReader;
private PrintWriter writer;
private Formatter formatter;
public ShellPrintStream(OutputStream outputStream) {
super(outputStream, true);
@@ -29,6 +30,10 @@ public class ShellPrintStream extends PrintStream {
}
}
public void setFormatter(Formatter formatter) {
this.formatter = formatter;
}
@Override
public void print(String s) {
println(s);
@@ -38,7 +43,11 @@ public class ShellPrintStream extends PrintStream {
public void println(String s) {
if (consoleReader != null) {
writer.print(ConsoleReader.RESET_LINE);
writer.print(s);
if (formatter != null) {
writer.print(formatter.format(s));
} else {
writer.print(s);
}
cleanTrashLine(s);
writer.println();
try {
@@ -49,7 +58,11 @@ public class ShellPrintStream extends PrintStream {
writer.flush();
} else {
super.print(ConsoleReader.RESET_LINE);
super.print(s);
if (formatter != null) {
super.print(formatter.format(s));
} else {
super.print(s);
}
}
}