Skip to content

Instantly share code, notes, and snippets.

@phsym
Created June 1, 2015 15:03
Show Gist options
  • Save phsym/8940b8bcc503139fa0f8 to your computer and use it in GitHub Desktop.
Save phsym/8940b8bcc503139fa0f8 to your computer and use it in GitHub Desktop.
A simple quick and dirty tab printer written in java
import java.io.IOException;
import java.util.Vector;
/**
* Utility class to print pretty tables in a console
*/
public class TabPrinter {
private int num_col = 0;
private String[] titles = null;
private Vector<String[]> tab = null;
/**
* Constructor
* @param column_titles array of columns names
*/
public TabPrinter(String[] column_titles) {
this.titles = column_titles;
num_col = titles.length;
tab = new Vector<String[]>();
}
/**
* @return The columns number
*/
public int getColumnsNumber() {
return num_col;
}
/**
* @return Rows number
*/
public int getRowsNumber() {
return tab.size();
}
/**
* Add a row to the table
* @param line Row to add
*/
public void addRow(String[] line) {
tab.add(line);
}
/**
* Change an element's value
* @param element The value
* @param column Column of the element
* @param row Row of the element
*/
public void setElement(String element, int column, int row) {
if(column >= num_col)
throw new IndexOutOfBoundsException("Tab has only " + num_col + " columns");
String[] rowLine;
if(row >= tab.size())
{
rowLine = new String[num_col];
for(int i = 0; i < rowLine.length; i++)
rowLine[i] = "";
addRow(rowLine);
}
else
rowLine = tab.get(row);
rowLine[column] = element;
}
/**
* Remove a line
* @param index The row number
*/
public void removeLine(int index) {
tab.remove(index);
}
/**
* Calculate the column width
* @param columnIndex column index
* @return The column width in characters
*/
private int getColumnWidth(int columnIndex) {
if(columnIndex >= num_col)
throw new IndexOutOfBoundsException("Tab has only " + num_col + " columns");
int width = titles[columnIndex].length();
for (String[] r : tab)
{
if(r[columnIndex].length() > width)
width = r[columnIndex].length();
}
return width;
}
/**
* Print a line separation ("----")
* @param columnWidth The array of columns width
* @throws IOException
*/
private void printLineSeparation(int[] columnWidth) throws IOException{
System.out.print("+");
for(int i = 0; i < num_col; i++)
{
for (int j = 0; j < columnWidth[i]+2; j++)
System.out.print("-");
System.out.print("+");
}
System.out.println("");
}
/**
* Print a lines
* @param line Lines to print
* @param columnWidth Array of column width
* @throws IOException
*/
private void printLine(String[] line, int[] columnWidth) throws IOException{
System.out.print("|");
for(int i = 0; i < num_col; i++)
{
System.out.print(" " + line[i] + " ");
for(int j = 0 ; j < (columnWidth[i] - line[i].length()); j++)
System.out.print(" ");
System.out.print("|");
}
System.out.println("");
}
/**
* Print the table
* @throws IOException
*/
public void print() throws IOException {
int[] col_width = new int[num_col];
for (int i = 0; i < num_col; i++)
{
col_width[i] = getColumnWidth(i);
}
printLineSeparation(col_width);
printLine(titles, col_width);
printLineSeparation(col_width);
for(String[] r : tab)
{
printLine(r, col_width);
printLineSeparation(col_width);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment