Changed init() to hookSystemOutputStreams(), so that is more clear what

it does.
master
nathan 2017-11-28 20:25:34 +01:00
parent eab207deda
commit 77b12f38c5
2 changed files with 51 additions and 49 deletions

View File

@ -67,8 +67,6 @@ class Console {
@Property
public final static boolean ENABLE_BACKSPACE = true;
/**
* Used to determine what console to use/hook when AUTO is not correctly working.
* Valid options are:
@ -89,16 +87,6 @@ class Console {
return "3.4";
}
/**
* Initializes output streams, necessary when using ANSI for the first time inside of an output stream (as it initializes after
* assignment). This is not needed for input streams, since they do not hook System.err/out.
*/
public static
void init() {
err();
out();
}
/**
* If the standard in supports single character input, then a terminal will be returned that supports it, otherwise a buffered (aka
* 'normal') input will be returned
@ -121,6 +109,18 @@ class Console {
return Input.wrappedInputStream;
}
/**
* Initializes and hooks output streams, necessary when using ANSI for the first time inside of an output stream (as it initializes
* after assignment).
* <p>
* This is not needed for input streams, since they do not hook System.err/out.
*/
public static
void hookSystemOutputStreams() {
out();
err();
}
/**
* If the standard out natively supports ANSI escape codes, then this just returns System.out (wrapped to reset ANSI stream on close),
* otherwise it will provide an ANSI aware PrintStream which strips out the ANSI escape sequences.
@ -147,10 +147,11 @@ class Console {
* If we are installed to the system (IE: System.err/out, then reset those streams, otherwise there is nothing to do from a static
* perspective (since creating a NEW ANSI stream will automatically reset the output
*/
public static synchronized
public static
void reset() {
try {
Ansi.out.write(AnsiOutputStream.RESET_CODE);
Ansi.err.write(AnsiOutputStream.RESET_CODE);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -26,7 +26,8 @@ class AnsiConsoleExample {
Console.ENABLE_ANSI = true;
Console.ENABLE_ECHO = true;
Console.init();
// needed to hook the output streams, so "normal" System.out/err work (rather than having to use Console.err/out
Console.hookSystemOutputStreams();
System.err.println("System Properties");
Properties properties = System.getProperties();
@ -71,28 +72,28 @@ class AnsiConsoleExample {
.reset());
System.out.println(Ansi.ansi()
.fg(Color.MAGENTA).a("magenta").bg(Color.MAGENTA).a("magenta")
.reset()
.fg(Color.BRIGHT_MAGENTA).a("b-magenta").bg(Color.BRIGHT_MAGENTA).a("b-magenta")
.reset());
.fg(Color.MAGENTA).a("magenta").bg(Color.MAGENTA).a("magenta")
.reset()
.fg(Color.BRIGHT_MAGENTA).a("b-magenta").bg(Color.BRIGHT_MAGENTA).a("b-magenta")
.reset());
System.out.println(Ansi.ansi()
.fg(Color.RED).a("red").bg(Color.RED).a("red")
.reset()
.fg(Color.BRIGHT_RED).a("b-red").bg(Color.BRIGHT_RED).a("b-red")
.reset());
.fg(Color.RED).a("red").bg(Color.RED).a("red")
.reset()
.fg(Color.BRIGHT_RED).a("b-red").bg(Color.BRIGHT_RED).a("b-red")
.reset());
System.out.println(Ansi.ansi()
.fg(Color.YELLOW).a("yellow").bg(Color.YELLOW).a("yellow")
.reset()
.fg(Color.BRIGHT_YELLOW).a("b-yellow").bg(Color.BRIGHT_YELLOW).a("b-yellow")
.reset());
.fg(Color.YELLOW).a("yellow").bg(Color.YELLOW).a("yellow")
.reset()
.fg(Color.BRIGHT_YELLOW).a("b-yellow").bg(Color.BRIGHT_YELLOW).a("b-yellow")
.reset());
System.out.println(Ansi.ansi()
.fg(Color.WHITE).a("white").bg(Color.WHITE).a("white")
.reset()
.fg(Color.BRIGHT_WHITE).a("b-white").bg(Color.BRIGHT_WHITE).a("b-white")
.reset());
.fg(Color.WHITE).a("white").bg(Color.WHITE).a("white")
.reset()
.fg(Color.BRIGHT_WHITE).a("b-white").bg(Color.BRIGHT_WHITE).a("b-white")
.reset());
Console.reset(); // reset the ansi stream. Can ALSO have ansi().reset(), but that would be redundant
@ -106,20 +107,20 @@ class AnsiConsoleExample {
System.out.println("The following line should be blank");
System.out.println(Ansi.ansi()
.a(">THIS SHOULD BE BLANK")
.eraseLine(Erase.ALL));
.a(">THIS SHOULD BE BLANK")
.eraseLine(Erase.ALL));
System.out.println(Ansi.ansi()
.a(">THIS SHOULD BE BLANK")
.eraseLine(Erase.BACKWARD)
.a("Everything on this line before this should be blank"));
.a(">THIS SHOULD BE BLANK")
.eraseLine(Erase.BACKWARD)
.a("Everything on this line before this should be blank"));
System.out.println(Ansi.ansi()
.a("Everything on this line after this should be blank")
.saveCursorPosition()
.a(">THIS SHOULD BE BLANK")
.restoreCursorPosition()
.eraseLine());
.a("Everything on this line after this should be blank")
.saveCursorPosition()
.a(">THIS SHOULD BE BLANK")
.restoreCursorPosition()
.eraseLine());
System.out.println("00000000000000000000000000");
System.out.println("00000000000000000000000000");
@ -128,15 +129,15 @@ class AnsiConsoleExample {
System.out.println("00000000000000000000000000");
System.out.println(Ansi.ansi()
.a("Should have two blank spots in the above 0's")
.saveCursorPosition()
.cursorUp(4)
.cursorLeft(30)
.a(" ")
.cursorDownLine()
.cursorRight(5)
.a(" ")
.restoreCursorPosition());
.a("Should have two blank spots in the above 0's")
.saveCursorPosition()
.cursorUp(4)
.cursorLeft(30)
.a(" ")
.cursorDownLine()
.cursorRight(5)
.a(" ")
.restoreCursorPosition());
System.out.println("ver : " + Console.getVersion());