Search in sources :

Example 1 with ContentRow

use of de.vandermeer.asciitable.v2.row.ContentRow in project walkmod-core by walkmod.

the class AsciiTableWidth method getColumnWidths.

@Override
public int[] getColumnWidths(V2_AsciiTable table) {
    int cols = table.getColumnCount();
    int[] resultWidths = new int[cols];
    // apply min width settings
    System.arraycopy(minWidths, 0, resultWidths, 0, minWidths.length > cols ? cols : minWidths.length);
    // iterate over all rows
    for (V2_Row row : table.getTable()) {
        if (row instanceof ContentRow) {
            ContentRow crow = (ContentRow) row;
            Object[] cells = crow.getColumns();
            // iterate over all cells in the row
            for (int i = 0; i < cells.length; i++) {
                String[] lines = ArrayTransformations.PROCESS_CONTENT(cells[i]);
                if (lines != null) {
                    // measuring the width of each line within a cell
                    for (String line : lines) {
                        int lineWidth = line.length() + 2 * crow.getPadding()[i];
                        if (lineWidth > maxWidth) {
                            lineWidth = maxWidth;
                        }
                        if (lineWidth > resultWidths[i]) {
                            int maxWidth = (maxWidths.length > i) ? maxWidths[i] : 0;
                            if (maxWidth < 1 || lineWidth < maxWidth) {
                                resultWidths[i] = lineWidth;
                            } else {
                                resultWidths[i] = maxWidth;
                            }
                        }
                    }
                }
            }
        }
    }
    return resultWidths;
}
Also used : ContentRow(de.vandermeer.asciitable.v2.row.ContentRow) V2_Row(de.vandermeer.asciitable.v2.row.V2_Row)

Aggregations

ContentRow (de.vandermeer.asciitable.v2.row.ContentRow)1 V2_Row (de.vandermeer.asciitable.v2.row.V2_Row)1