public static String formatDisplayCompactListTerminalString ( List < TerminalString > displayList , int termWidth ) { if ( displayList == null || displayList . size ( ) < 1 ) return "" ; // make sure that termWidth is > 0 if ( termWidth < 1 ) termWidth = 80 ; // setting it to default int numRows = 1 ; // increase numRows and check in loop if it's possible to format strings to columns while ( ! canDisplayColumns ( displayList , numRows , termWidth ) && numRows < displayList . size ( ) ) { numRows ++ ; } int numColumns = displayList . size ( ) / numRows ; if ( displayList . size ( ) % numRows > 0 ) { numColumns ++ ; } int [ ] columnsSizes = calculateColumnSizes ( displayList , numColumns , numRows , termWidth ) ; StringBuilder stringOutput = new StringBuilder ( ) ; for ( int i = 0 ; i < numRows ; i ++ ) { for ( int c = 0 ; c < numColumns ; c ++ ) { int fetch = i + ( c * numRows ) ; int nextFetch = i + ( ( c + 1 ) * numRows ) ; if ( fetch < displayList . size ( ) ) { // don't need to format last column of row = nextFetch doesn't exit if ( nextFetch < displayList . size ( ) ) { stringOutput . append ( padRight ( columnsSizes [ c ] + displayList . get ( i + ( c * numRows ) ) . getANSILength ( ) , displayList . get ( i + ( c * numRows ) ) . toString ( ) ) ) ; } else { stringOutput . append ( displayList . get ( i + ( c * numRows ) ) . toString ( ) ) ; } } else { break ; } } stringOutput . append ( Config . getLineSeparator ( ) ) ; } return stringOutput . toString ( ) ; }
Format output to columns with flexible sizes and no redundant space between them