/*
* Copyright (C) 2021 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* utility library for nicely formatted ascii tables.
* @author Gabriel Venberg
*/
public class ASCIITable {
/**
* generates an ASCII table based on a 2d data array. the top level array is an array of rows.
* @param data 2d array containing data to put in table
* @param padding how much padding to put on each side of entries
* @param tableHeader string to put in the table header (may cause problems if extremely long)
* @param columnHeaders array of strings to put at the top of each column.
* @return
*/
public static String render(Object data[][], int padding, String tableHeader, String[] columnHeaders) throws IllegalArgumentException {
int cols = calcNoCols(data);
if(cols!=columnHeaders.length){throw new IllegalArgumentException("must have equal number of column headers as columns!");}
int[] colWidths = calcColumnWidth(cols, data, columnHeaders);
//colWidths does not count padding or the | chars betwwen tables.
int width = sumOfArray(colWidths)+padding*cols*2+(cols-1);
String horizontalSpacer = assembleHorizontalSpacers(colWidths, padding, cols);
/*ok, so each cell will have the colwidth for the data, then padding for padding,
* then a | at the end. (plus 1 at the begginning of the table.
there will be 2 rows for each row of data (horizontal sep) plus a horizontal sep
at the end.
*/
String string = horizontalSpacer+'\n';
//print table header
string=string+tableHeader(tableHeader, width)+"\n";
string = string+horizontalSpacer+"\n";
//print coumn headers
string=string+columnHeaderString(colWidths, padding, columnHeaders)+'\n';
//got everything set up, build the table row by row.
for(int i=0; i