Добавил элемент форматирования
This commit is contained in:
9
src/main/java/ru/dmitriymx/shell/Formatter.java
Normal file
9
src/main/java/ru/dmitriymx/shell/Formatter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package ru.dmitriymx.shell;
|
||||
|
||||
/**
|
||||
* @author DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2015
|
||||
*/
|
||||
public interface Formatter {
|
||||
String format(String s);
|
||||
}
|
||||
@@ -71,6 +71,10 @@ public class Shell {
|
||||
return run;
|
||||
}
|
||||
|
||||
public void setFormatter(Formatter formatter) {
|
||||
newErr.setFormatter(formatter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Подмена стандартных SysErr и SysOut
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
if (formatter != null) {
|
||||
writer.print(formatter.format(s));
|
||||
} else {
|
||||
writer.print(s);
|
||||
}
|
||||
cleanTrashLine(s);
|
||||
writer.println();
|
||||
try {
|
||||
@@ -49,9 +58,13 @@ public class ShellPrintStream extends PrintStream {
|
||||
writer.flush();
|
||||
} else {
|
||||
super.print(ConsoleReader.RESET_LINE);
|
||||
if (formatter != null) {
|
||||
super.print(formatter.format(s));
|
||||
} else {
|
||||
super.print(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистка печатной строки от мусора
|
||||
|
||||
Reference in New Issue
Block a user