Flip Tables
Because pretty-printing text tables in Java should be easy.
(โฏยฐโกยฐ๏ผโฏ๏ธต โปโโป
Development on this project has basically stopped. You can find a more comprehensive and actively-developed text table library at https://github.com/JakeWharton/picnic.
Usage
A FlipTable
requires headers and data in string form:
String[] headers = { "Test", "Header" };
String[][] data = {
{ "Foo", "Bar" },
{ "Kit", "Kat" },
};
System.out.println(FlipTable.of(headers, data));
โโโโโโโโคโโโโโโโโโ
โ Test โ Header โ
โ โโโโโโโชโโโโโโโโโฃ
โ Foo โ Bar โ
โโโโโโโโผโโโโโโโโโข
โ Kit โ Kat โ
โโโโโโโโงโโโโโโโโโ
They can be empty:
String[] headers = { "Test", "Header" };
String[][] data = {};
System.out.println(FlipTable.of(headers, data));
โโโโโโโโคโโโโโโโโโ
โ Test โ Header โ
โ โโโโโโโงโโโโโโโโโฃ
โ (empty) โ
โโโโโโโโโโโโโโโโโ
Newlines are supported:
String[] headers = { "One Two\nThree", "Four" };
String[][] data = { { "Five", "Six\nSeven Eight" } };
System.out.println(FlipTable.of(headers, data));
โโโโโโโโโโโคโโโโโโโโโโโโโโ
โ One Two โ Four โ
โ Three โ โ
โ โโโโโโโโโโชโโโโโโโโโโโโโโฃ
โ Five โ Six โ
โ โ Seven Eight โ
โโโโโโโโโโโงโโโโโโโโโโโโโโ
Which means tables can be nested:
String[] innerHeaders = { "One", "Two" };
String[][] innerData = { { "1", "2" } };
String inner = FlipTable.of(innerHeaders, innerData);
String[] headers = { "Left", "Right" };
String[][] data = { { inner, inner } };
System.out.println(FlipTable.of(headers, data));
โโโโโโโโโโโโโโโโโคโโโโโโโโโโโโโโโโ
โ Left โ Right โ
โ โโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโฃ
โ โโโโโโโคโโโโโโ โ โโโโโโโคโโโโโโ โ
โ โ One โ Two โ โ โ One โ Two โ โ
โ โ โโโโโโชโโโโโโฃ โ โ โโโโโโชโโโโโโฃ โ
โ โ 1 โ 2 โ โ โ 1 โ 2 โ โ
โ โโโโโโโงโโโโโโ โ โโโโโโโงโโโโโโ โ
โโโโโโโโโโโโโโโโโงโโโโโโโโโโโโโโโโ
Helper methods convert from types like lists:
List<Person> people = Arrays.asList(new Person("Foo", "Bar"), new Person("Kit", "Kat"));
System.out.println(FlipTableConverters.fromIterable(people, Person.class));
โโโโโโโโโโโโโคโโโโโโโโโโโ
โ FirstName โ LastName โ
โ โโโโโโโโโโโโชโโโโโโโโโโโฃ
โ Foo โ Bar โ
โโโโโโโโโโโโโผโโโโโโโโโโโข
โ Kit โ Kat โ
โโโโโโโโโโโโโงโโโโโโโโโโโ
Or a database result:
ResultSet resultSet = statement.executeQuery("SELECT first_name, last_name FROM users");
System.out.println(FlipTableConverters.fromResultSet(resultSet));
โโโโโโโโโโโโโโคโโโโโโโโโโโโ
โ first_name โ last_name โ
โ โโโโโโโโโโโโโชโโโโโโโโโโโโฃ
โ Jake โ Wharton โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโข
โ Edward โ Snowden โ
โโโโโโโโโโโโโโงโโโโโโโโโโโโ
Arbitrary objects are also supported:
String[] headers = { "First Name", "Last Name", "Age", "Type" };
Object[][] data = {
{ "Big", "Bird", 16, PersonType.COSTUME },
{ "Joe", "Smith", 42, PersonType.HUMAN },
{ "Oscar", "Grouchant", 8, PersonType.PUPPET }
};
System.out.println(FlipTableConverters.fromObjects(headers, data));
โโโโโโโโโโโโโโคโโโโโโโโโโโโคโโโโโโคโโโโโโโโโโ
โ First Name โ Last Name โ Age โ Type โ
โ โโโโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโชโโโโโโโโโโฃ
โ Big โ Bird โ 16 โ COSTUME โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโผโโโโโโโโโโข
โ Joe โ Smith โ 42 โ HUMAN โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโผโโโโโโโโโโข
โ Oscar โ Grouchant โ 8 โ PUPPET โ
โโโโโโโโโโโโโโงโโโโโโโโโโโโงโโโโโโงโโโโโโโโโโ
Download
Download the latest jar or reference on Maven central as
com.jakewharton.fliptables:fliptables:1.1.0
.
Snapshots of the development version are available in Sonatype's snapshots
repository.
License
Copyright 2014 Jake Wharton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.