Добавил элемент форматирования
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;
|
return run;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setFormatter(Formatter formatter) {
|
||||||
|
newErr.setFormatter(formatter);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Подмена стандартных SysErr и SysOut
|
* Подмена стандартных SysErr и SysOut
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import java.io.PrintWriter;
|
|||||||
public class ShellPrintStream extends PrintStream {
|
public class ShellPrintStream extends PrintStream {
|
||||||
private ConsoleReader consoleReader;
|
private ConsoleReader consoleReader;
|
||||||
private PrintWriter writer;
|
private PrintWriter writer;
|
||||||
|
private Formatter formatter;
|
||||||
|
|
||||||
public ShellPrintStream(OutputStream outputStream) {
|
public ShellPrintStream(OutputStream outputStream) {
|
||||||
super(outputStream, true);
|
super(outputStream, true);
|
||||||
@@ -29,6 +30,10 @@ public class ShellPrintStream extends PrintStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setFormatter(Formatter formatter) {
|
||||||
|
this.formatter = formatter;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void print(String s) {
|
public void print(String s) {
|
||||||
println(s);
|
println(s);
|
||||||
@@ -38,7 +43,11 @@ public class ShellPrintStream extends PrintStream {
|
|||||||
public void println(String s) {
|
public void println(String s) {
|
||||||
if (consoleReader != null) {
|
if (consoleReader != null) {
|
||||||
writer.print(ConsoleReader.RESET_LINE);
|
writer.print(ConsoleReader.RESET_LINE);
|
||||||
writer.print(s);
|
if (formatter != null) {
|
||||||
|
writer.print(formatter.format(s));
|
||||||
|
} else {
|
||||||
|
writer.print(s);
|
||||||
|
}
|
||||||
cleanTrashLine(s);
|
cleanTrashLine(s);
|
||||||
writer.println();
|
writer.println();
|
||||||
try {
|
try {
|
||||||
@@ -49,7 +58,11 @@ public class ShellPrintStream extends PrintStream {
|
|||||||
writer.flush();
|
writer.flush();
|
||||||
} else {
|
} else {
|
||||||
super.print(ConsoleReader.RESET_LINE);
|
super.print(ConsoleReader.RESET_LINE);
|
||||||
super.print(s);
|
if (formatter != null) {
|
||||||
|
super.print(formatter.format(s));
|
||||||
|
} else {
|
||||||
|
super.print(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user