2016-05-28 11:30:46 +02:00
|
|
|
package com.dorkbox.console;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2016-05-31 01:41:17 +02:00
|
|
|
import java.lang.management.ManagementFactory;
|
|
|
|
import java.lang.management.RuntimeMXBean;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
2016-05-28 11:30:46 +02:00
|
|
|
|
2016-05-29 11:44:30 +02:00
|
|
|
import dorkbox.console.Console;
|
2016-05-28 11:30:46 +02:00
|
|
|
import dorkbox.console.output.Ansi;
|
2016-05-29 11:44:30 +02:00
|
|
|
import dorkbox.console.output.Color;
|
|
|
|
import dorkbox.console.output.Erase;
|
2016-05-28 11:30:46 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-29 11:44:30 +02:00
|
|
|
* System output for visual confirmation of ANSI codes.
|
|
|
|
* <p>
|
|
|
|
* Must enable assertions to verify no errors!! (ie: java -ea -jar blah.jar)
|
2016-05-28 11:30:46 +02:00
|
|
|
*/
|
2016-05-29 11:44:30 +02:00
|
|
|
public
|
|
|
|
class AnsiConsoleExample {
|
|
|
|
public static
|
|
|
|
void main(String[] args) throws IOException {
|
2016-05-28 11:30:46 +02:00
|
|
|
|
2016-05-29 11:44:30 +02:00
|
|
|
Console.ENABLE_ANSI = true;
|
2016-05-30 11:22:04 +02:00
|
|
|
Console.ENABLE_ECHO = true;
|
2016-05-28 11:30:46 +02:00
|
|
|
|
2017-11-28 20:25:34 +01:00
|
|
|
// needed to hook the output streams, so "normal" System.out/err work (rather than having to use Console.err/out
|
|
|
|
Console.hookSystemOutputStreams();
|
2016-05-31 01:41:17 +02:00
|
|
|
|
|
|
|
System.err.println("System Properties");
|
|
|
|
Properties properties = System.getProperties();
|
|
|
|
for (Map.Entry<Object, Object> prop : properties.entrySet()) {
|
|
|
|
System.err.format("\t%s=%s%n", prop.getKey(), prop.getValue());
|
|
|
|
}
|
|
|
|
System.err.println("");
|
|
|
|
System.err.println("");
|
|
|
|
System.err.println("");
|
|
|
|
|
|
|
|
System.err.println("Runtime Arguments");
|
|
|
|
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
|
|
|
|
List<String> arguments = runtimeMxBean.getInputArguments();
|
|
|
|
System.err.println(Arrays.toString(arguments.toArray()));
|
|
|
|
|
|
|
|
System.err.println("");
|
|
|
|
System.err.println("");
|
|
|
|
System.err.println("");
|
|
|
|
|
2016-05-29 11:44:30 +02:00
|
|
|
System.out.println(Ansi.ansi()
|
|
|
|
.fg(Color.BLACK).a("black").bg(Color.BLACK).a("black")
|
|
|
|
.reset()
|
|
|
|
.fg(Color.BRIGHT_BLACK).a("b-black").bg(Color.BRIGHT_BLACK).a("b-black")
|
|
|
|
.reset());
|
2016-05-28 11:30:46 +02:00
|
|
|
|
2016-05-29 11:44:30 +02:00
|
|
|
System.out.println(Ansi.ansi()
|
|
|
|
.fg(Color.BLUE).a("blue").bg(Color.BLUE).a("blue")
|
|
|
|
.reset()
|
|
|
|
.fg(Color.BRIGHT_BLUE).a("b-blue").bg(Color.BRIGHT_BLUE).a("b-blue")
|
|
|
|
.reset());
|
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
|
|
|
.fg(Color.CYAN).a("cyan").bg(Color.CYAN).a("cyan")
|
|
|
|
.reset()
|
|
|
|
.fg(Color.BRIGHT_CYAN).a("b-cyan").bg(Color.BRIGHT_CYAN).a("b-cyan")
|
|
|
|
.reset());
|
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
|
|
|
.fg(Color.GREEN).a("green").bg(Color.GREEN).a("green")
|
|
|
|
.reset()
|
|
|
|
.fg(Color.BRIGHT_GREEN).a("b-green").bg(Color.BRIGHT_GREEN).a("b-green")
|
|
|
|
.reset());
|
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.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());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.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());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.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());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.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());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
Console.reset(); // reset the ansi stream. Can ALSO have ansi().reset(), but that would be redundant
|
2016-05-28 11:30:46 +02:00
|
|
|
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println("The following line should be blank except for the first '>'");
|
|
|
|
System.out.println(Ansi.ansi()
|
|
|
|
.a(">THIS SHOULD BE BLANK")
|
|
|
|
.cursorToColumn(2)
|
|
|
|
.eraseLine());
|
|
|
|
|
|
|
|
System.out.println("The following line should be blank");
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.a(">THIS SHOULD BE BLANK")
|
|
|
|
.eraseLine(Erase.ALL));
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.a(">THIS SHOULD BE BLANK")
|
|
|
|
.eraseLine(Erase.BACKWARD)
|
|
|
|
.a("Everything on this line before this should be blank"));
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.a("Everything on this line after this should be blank")
|
|
|
|
.saveCursorPosition()
|
|
|
|
.a(">THIS SHOULD BE BLANK")
|
|
|
|
.restoreCursorPosition()
|
|
|
|
.eraseLine());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println("00000000000000000000000000");
|
|
|
|
System.out.println("00000000000000000000000000");
|
|
|
|
System.out.println("00000000000000000000000000");
|
|
|
|
System.out.println("00000000000000000000000000");
|
|
|
|
System.out.println("00000000000000000000000000");
|
|
|
|
|
|
|
|
System.out.println(Ansi.ansi()
|
2017-11-28 20:25:34 +01:00
|
|
|
.a("Should have two blank spots in the above 0's")
|
|
|
|
.saveCursorPosition()
|
|
|
|
.cursorUp(4)
|
|
|
|
.cursorLeft(30)
|
|
|
|
.a(" ")
|
|
|
|
.cursorDownLine()
|
|
|
|
.cursorRight(5)
|
|
|
|
.a(" ")
|
|
|
|
.restoreCursorPosition());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
2016-05-31 01:07:02 +02:00
|
|
|
System.out.println("ver : " + Console.getVersion());
|
2016-05-29 11:44:30 +02:00
|
|
|
|
2016-05-30 11:22:04 +02:00
|
|
|
System.out.println();
|
2016-05-29 11:44:30 +02:00
|
|
|
|
|
|
|
System.out.println("Now testing the input console. 'q' to quit");
|
|
|
|
int read;
|
2016-05-31 01:07:02 +02:00
|
|
|
while ((read = Console.in().read()) != 'q') {
|
2016-05-29 11:44:30 +02:00
|
|
|
if (Character.isDigit(read)) {
|
|
|
|
int numericValue = Character.getNumericValue(read);
|
|
|
|
// reverse if pressing 2
|
|
|
|
if (numericValue == 2) {
|
|
|
|
System.out.print(Ansi.ansi().scrollDown(1));
|
|
|
|
System.out.flush(); // flush guarantees the terminal moves the way we want
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
System.out.print(Ansi.ansi().scrollUp(numericValue));
|
|
|
|
System.out.flush(); // flush guarantees the terminal moves the way we want
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 01:07:02 +02:00
|
|
|
System.err.println("char :" + read + " (" + (char) read + ")");
|
2016-05-28 11:30:46 +02:00
|
|
|
}
|
2016-05-29 11:44:30 +02:00
|
|
|
|
2016-05-30 11:22:04 +02:00
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
System.out.println("Now testing the input console LINE input. 'q' to quit");
|
|
|
|
String line;
|
2016-05-31 01:07:02 +02:00
|
|
|
while (!(line = Console.in().readLine()).equals("q")) {
|
2016-05-30 11:22:04 +02:00
|
|
|
System.err.println("line: " + line);
|
|
|
|
}
|
2016-05-29 11:44:30 +02:00
|
|
|
}
|
2016-05-28 11:30:46 +02:00
|
|
|
}
|