Coder Social home page Coder Social logo

pydawan / java-console-table-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thousandlemons/java-console-table-builder

0.0 1.0 0.0 47 KB

An extendable library to build stylish Excel-like tables, which can be converted into a single string with all formats preserved.

License: Other

Java 100.00%

java-console-table-builder's Introduction

Java Console Table Builder: Quickly Build a Stylish Excel-like Table

maven central license

This library makes it easy to build stylish Excel-like tables, which can be printed into a single string with all formats preserved.

The core features are:

  • Quickly build a table with uniform format for each column.
  • Build tables that have a different format for each column.
  • Built-in formatters including numbers, percentages, currency, date, time, etc.
  • Built-in alignment formats for each column as LEFT, CENTER or RIGHT.
  • Built-in precision formats for numbers

Latest Release

The most recent release is Console Tree Builder 1.2, released 18 Feb 2016.

Version 1.2 API Docs: Package io.bretty.console.table

To add a dependency using Maven, use the following:

<dependency>
  <groupId>io.bretty</groupId>
  <artifactId>console-table-builder</artifactId>
  <version>1.2</version>
</dependency>

To add a dependency using Gradle:

compile 'io.bretty:console-table-builder:1.2'

Quick Start

Alignment

There are three alignment types availabe in the enum Alignment,

  • LEFT
  • CENTER
  • RIGHT

Convert Object[][] to Stylish Table

If you already have a Object[][] data, you can print it into the console as a formatted table instantly with a specifed Alignment and a width for ALL columns in this table:

Table table = Table.of(data, Alignment.LEFT, 10); // 10-character wide for each column
System.out.println(table); // NOTICE: table.toString() is called implicitly

More on Table Formats

Precision

The enum Precision comprises decimal number precision formats from ZERO to NINE.

Each Number object processed by Precision will be rounded up to the specified precision. For example, applying Precision.TWO to the constant Math.PI will give us 3.14 as a result.

ColumnFormatter

ColumnFormatter<T> specifies how all the cells in a certain column should be formatted. It is an abstract class that is designed to be subclassed.

A concrete implementation of a subclass has at least two fields, alignment and width. A subclass also must implement a String format(T t) method, which can convert an object of type T to a string, which will be later used as a cell in the column.

There are several existing implementation of this class. For example, to get a ColumnFormatter<Number> object to format numbers as

  • with "USD" as prefix
  • aligned to the right within each cell.
  • each column is 8-character wide
  • each number has 2-digit precision

simply write:

ColumnFormatter<Number> usdollar = ColumnFormatter.currency(Alignment.RIGHT, 8, Precision.TWO, "USD");

Each element in the column associated with usdollar will be formatted as the example below.

 USD 15.00
USD 457.20
 USD 37.50

Please note that the "USD" in the parameter list can be replaced by any string, which will serve as a prefix in the formatted strings. If this string has only one character, it is most likely to be something like "$". In that case, there will be no space between this prefix string and the number. For example, if we get a ColumnFormatter<Number> using the one-character string "$",

ColumnFormatter<Number> usdollar = ColumnFormatter.currency(Alignment.RIGHT, 8, Precision.TWO, "$");

the same values in the above example will be formatted as:

 $15.00
$457.20
 $37.50

Here is a list of all built-in ColumnFormatter implementations:

Column Type Factory Method
Currency ColumnFormatter.currency(...)
Number ColumnFormatter.number(...)
Percentage ColumnFormatter.percentage(...)
Datetime ColumnFormatter.dateTime(...)
Text ColumnFormatter.text(...)

Build Tables in Fully Customized Format

Convert T[][] to a Stylish Table

The following example shows, with given

  • Double[][] data: table contents, and
  • String[] headers: table headers,

how to print a table with the same format for each column:

  • Aligned to the right
  • 8-char wide
  • 3-digit precision after the decimal point
ColumnFormatter<Number> cf = ColumnFormatter.number(Alignment.RIGHT, 8, Precision.THREE);
Table table = Table.of(headers, data, cf);
System.out.println(table); //NOTICE: table.toString() is called implicitly

In the case that your table doesn't need headers, the headers in the paramaters can be simply omitted like this:

Table table = Table.of(data, cf);

Such a table will not have headers in the first row.

Build a Table Column By Column

If you want each column in your table to be formatted in a different fashion, this is your solution.

The following example shows how to print a stylish table with given

  • String[] names for the first column
  • Integer[] ages for the second,
  • Double[] rates (in percentage format) for the third
// define a formatter for each column
ColumnFormatter<String> nameFormatter = ColumnFormatter.text(Alignment.LEFT, 10);
ColumnFormatter<Number> ageFormatter = ColumnFormatter.number(Alignment.RIGHT, 3, Precision.ZERO);
ColumnFormatter<Number> rateFormatter = ColumnFormatter.percentage(Alignment.RIGHT, 6, Precision.ONE);

// create a builder with the first column
// "Name" serves as the header for this column
Table.Builder builder = new Table.Builder("Name", names, nameFormatter);

// add other columns
builder.addColumn("Age", ages, ageFormatter);
builder.addColumn("Rate", rates, rateFormatter);

// build the table and print it
Table table = builder.build();
System.out.println(table); // NOTICE: table.toString() is called implicitly

java-console-table-builder's People

Contributors

thousandlemons avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.