EN
Java - print pretty table in console with ASCII table library (practical working code example)
4
points
Library pom dependency:
<!-- https://mvnrepository.com/artifact/de.vandermeer/asciitable -->
<dependency>
<groupId>de.vandermeer</groupId>
<artifactId>asciitable</artifactId>
<version>0.3.2</version>
</dependency>
Link to maven repository:Β Maven Repository: de.vandermeer Β» asciitable
Β
Example 1:
import de.vandermeer.asciitable.AsciiTable;
public class AsciiTableJavaPrintTableInConsole {
public static void main(String[] args) {
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("row 1 col 1", "row 1 col 2");
at.addRule();
at.addRow("row 2 col 1", "row 2 col 2");
at.addRule();
String rend = at.render();
System.out.println(rend);
}
}
Output:
βββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
βrow 1 col 1 βrow 1 col 2 β
βββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ€
βrow 2 col 1 βrow 2 col 2 β
βββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββ
Β
Example 2:
import de.vandermeer.asciitable.AsciiTable;
public class Example2 {
public static void main(String[] args) {
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("row 1 col 1", "row 1 col 2");
at.addRule();
at.addRow("row 2 col 1", "row 2 col 2");
at.addRule();
at.getContext().setWidth(50);
System.out.println(at.render());
at.getContext().setWidth(40);
System.out.println(at.render());
at.getContext().setWidth(30);
System.out.println(at.render());
}
}
Output:
ββββββββββββββββββββββββββ¬ββββββββββββββββββββββββ
βrow 1 col 1 βrow 1 col 2 β
ββββββββββββββββββββββββββΌββββββββββββββββββββββββ€
βrow 2 col 1 βrow 2 col 2 β
ββββββββββββββββββββββββββ΄ββββββββββββββββββββββββ
βββββββββββββββββββββ¬βββββββββββββββββββ
βrow 1 col 1 βrow 1 col 2 β
βββββββββββββββββββββΌβββββββββββββββββββ€
βrow 2 col 1 βrow 2 col 2 β
βββββββββββββββββββββ΄βββββββββββββββββββ
ββββββββββββββββ¬ββββββββββββββ
βrow 1 col 1 βrow 1 col 2 β
ββββββββββββββββΌββββββββββββββ€
βrow 2 col 1 βrow 2 col 2 β
ββββββββββββββββ΄ββββββββββββββ
Β
Example 3:
import de.vandermeer.asciitable.AsciiTable;
public class Example3 {
public static void main(String[] args) {
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("row 1 col 1", "row 1 col 2");
at.addRule();
at.addRow("row 2 col 1", "row 2 col 2");
at.addRule();
at.getContext().setFrameTopChar('v');
at.getContext().setFrameBottomChar('^');
at.getContext().setFrameLeftChar('>');
at.getContext().setFrameRightChar('<');
at.getContext().setFrameTopMargin(1);
at.getContext().setFrameBottomMargin(2);
at.getContext().setFrameLeftMargin(3);
at.getContext().setFrameRightMargin(4);
System.out.println(at.render(39));
}
}
Output:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
>>>βββββββββββββββββ¬ββββββββββββββββ<<<<
>>>βrow 1 col 1 βrow 1 col 2 β<<<<
>>>βββββββββββββββββΌββββββββββββββββ€<<<<
>>>βrow 2 col 1 βrow 2 col 2 β<<<<
>>>βββββββββββββββββ΄ββββββββββββββββ<<<<
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Β
Example 4:
import de.vandermeer.asciitable.AsciiTable;
public class Example4 {
public static void main(String[] args) {
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]",
"scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]");
at.addRule();
at.addRow(null, "scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]");
at.addRule();
at.addRow("abc://username:password@example.com:123/path/data?key=value#fragid1",
"abc://username:password@example.com:123/path/data?key=value#fragid1");
at.addRule();
at.addRow(null, "abc://username:password@example.com:123/path/data?key=value#fragid1");
at.addRule();
at.addRow("urn:example:mammal:monotreme:echidna", "urn:example:mammal:monotreme:echidna");
at.addRule();
at.addRow(null, "urn:example:mammal:monotreme:echidna");
at.addRule();
at.addRow("http://www.example.com/test1/test2", "http://www.example.com/test1/test2");
at.addRule();
at.addRow(null, "http://www.example.com/test1/test2");
at.addRule();
at.addRow("mailto:user1@example.com", "mailto:firstname.lastname@example.com");
at.addRule();
at.addRow(null, "mailto:firstname.lastname@example.com");
at.addRule();
System.out.println(at.render(73));
}
}
Output:
βββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
βscheme:[//[user:password@]host[:porβscheme:[//[user:password@]host[:porβ
βt]][/]path[?query][#fragment] βt]][/]path[?query][#fragment] β
βββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ€
βscheme:[//[user:password@]host[:port]][/]path[?query][#fragment] β
βββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ€
βabc://username:password@example.comβabc://username:password@example.comβ
β:123/path/data?key=value#fragid1 β:123/path/data?key=value#fragid1 β
βββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ€
βabc://username:password@example.com:123/path/data?key=value#fragid1 β
βββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ€
βurn:example:mammal:monotreme:echidnβurn:example:mammal:monotreme:echidnβ
βa βa β
βββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ€
βurn:example:mammal:monotreme:echidna β
βββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ€
βhttp://www.example.com/test1/test2 βhttp://www.example.com/test1/test2 β
βββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ€
βhttp://www.example.com/test1/test2 β
βββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ€
βmailto:user1@example.com βmailto:firstname.lastname@example.cβ
β βom β
βββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββ€
βmailto:firstname.lastname@example.com β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Β
Examples can be found here: