Reputation: 35
I'm trying to print/output colorized & formatted text to console, on a machine running Windows 8(64x).
I'm reading from a CSV file, initializing attributes of Product objects with this data, and storing the objects in an ArrayList named productList. After sorting this list with the use of comparators, I am trying to print the results of the sort in a 'neat' formatted way(this is what my code below 'should' do. Additionally, I'm trying to alter the color of the output. This is where I am having trouble.
I've been trying to use Janis for colorizing, but I'm stuck. My main hangup is that the formatted output is displayed via:
console.printf(format, "Name", "Category", "Price");
While colorized output is displayed via something like:
AnsiConsole.out.println(BLUE + blueString + ANSI_NORMAL);
I'm unsure how to use the functionality of both AnsiConsole & Console in the same print().
I have included two pieces of code below. The first is my displaySorted() from my program, designed to format my output in a table fashion. The second piece of code, is something I have been playing with, trying to display formatted, colorized text. I'm including the second piece to show some of my thoughts & provide more information.
I'm also going to include a few links I have used while attempting to work with Janis:
http://grepcode.com/snapshot/repo1.maven.org/maven2/org.fusesource.jansi/jansi/1.9
http://www.doublecloud.org/2013/10/writing-colorful-console-output-in-java/
https://forum.ragezone.com/f428/using-jansi-library-server-console-948744/
This the the first time I have attempted to do something like this, and it might be the case that I'm going about it the wrong way.
If you have a better way to display formatted & colorized output on a Windows machine, I'M ALL EARS.
Please include code with suggestions as this is a new area for me. Thank you.
public static void displaySorted(List<Product> productList) {
// To enable/disable the ANSI capability, you would need to install/uninstall it with a static
// method: AnsiConsole.systemInstall(); AnsiConsole.systemUninstall();
// AnsiConsole.systemInstall();
Console console = null;
Iterator<Product> iterator = WareHouse.productList.iterator();
try {
console = System.console();
if(console != null) {
String format = "%1$4s %2$10s %3$10s%n";
while (iterator.hasNext()) {
Product product = (Product)iterator.next();
console.printf(format, "Name", "Category", "Price");
console.printf(format, "-----", "-----", "-----");
console.printf(format, product.getName(), product.getCategory(), product.getPrice());
}
}
}
/*if error occurs*/
catch(Exception exception) {
exception.printStackTrace();
}
// AnsiConsole.systemUninstall();
}
package app;
import java.io.Console;
import org.fusesource.jansi.*;
public class Main {
public static final String BLACK = "\u001B[0;30m";
public static final String RED = "\u001B[0;31m";
public static final String GREEN = "\u001B[0;32m";
public static final String YELLOW = "\u001B[0;33m";
public static final String BLUE = "\u001B[0;34m";
public static final String MAGENTA = "\u001B[0;35m";
public static final String CYAN = "\u001B[0;36m";
public static final String WHITE = "\u001B[0;37m";
public static final String ANSI_CLS = "\u001b[2J";
public static final String ANSI_HOME = "\u001b[H";
public static final String ANSI_BOLD = "\u001b[1m";
public static final String ANSI_AT55 = "\u001b[10;10H";
public static final String ANSI_REVERSEON = "\u001b[7m";
public static final String ANSI_NORMAL = "\u001b[0m";
public static final String ANSI_WHITEONBLUE = "\u001b[37;44m";
/********************test fields********************/
public static String name;
public static String category;
public static String price;
public static void main(String[] args) {
setName();
setCategory();
setPrice();
display();
}
public static void display() {
//AnsiConsole.systemInstall();
Console console = null;
try {
console = System.console();
if(console != null) {
String format = "%1$4s %2$10s %3$10s%n";
console.printf(format, "Name", "Category", "Price");
console.printf(format, "-----", "-----", "-----");
console.printf(format, getName(), getCategory(), getPrice());
}
// AnsiConsole.systemUninstall();
}
/*if error occurs*/
catch(Exception exception) {
exception.printStackTrace();
}
}
/********************set & get********************/
/**********name**********/
public static void setName() {
name = "Bob";
}
public static String getName() {
return name;
}
/**********category**********/
public static void setCategory() {
category = "Things";
}
public static String getCategory() {
return category;
}
/**********price**********/
public static void setPrice() {
price = "$5.0";
}
public static String getPrice() {
return price;
}
/*********************asci********************/
public static void makeBlue(String blueString) {
AnsiConsole.out.println(BLUE + blueString + ANSI_NORMAL);
}
public static void makeGreen(String greenString) {
AnsiConsole.out.println(GREEN + greenString + ANSI_NORMAL);
}
public static void makeYellow(String yellowString) {
AnsiConsole.out.println(YELLOW + yellowString + ANSI_NORMAL);
}
public static void makeMagenta(String magentaString) {
AnsiConsole.out.println(MAGENTA + magentaString + ANSI_NORMAL);
}
public static void makeBold(String boldString) {
AnsiConsole.out.println(ANSI_BOLD + boldString + ANSI_NORMAL);
}
}
Upvotes: 0
Views: 1557
Reputation: 729
I would think the easiest way to do this is to skip the use of the console for formatting altogether and instead just use String.format(String format, Object... args)
. It has the same parameters, but instead of printing straight to the console it returns the formatted string. You can then send the returned string to the AnsiConsole to color it. So in your second example you could replace
if (console != null) {
String format = "%1$4s %2$10s %3$10s%n";
console.printf(format, "Name", "Category", "Price");
console.printf(format, "-----", "-----", "-----");
console.printf(format, getName(), getCategory(), getPrice());
}
with
if (console != null) {
String format = "%1$4s %2$10s %3$10s%n";
String output;
output = String.format(format, "Name", "Category", "Price");
makeBlue(output);
output = String.format(format, "-----", "-----", "-----");
makeBlue(output);
output = String.format(format, getName(), getCategory(), getPrice());
makeBlue(output);
}
and if you don't want to have a variable to store the formatted outputs you can just pass the output of String.format straight to makeBlue (or whatever color)
if (console != null) {
String format = "%1$4s %2$10s %3$10s%n";
makeBlue(String.format(format, "Name", "Category", "Price"));
makeBlue(String.format(format, "-----", "-----", "-----"));
makeBlue(String.format(format, getName(), getCategory(), getPrice()));
}
Upvotes: 1