DefaultLineWrapper extends AbstractLineWrapper { @Override public int wrap(Appendable appendable, TextTableInfo tableInfo, TextColumn... columns) { try { Objects.requireNonNull(appendable, "appendable"); Objects.requireNonNull(tableInfo, "tableInfo"); Objects.requireNonNull(columns, "columns"); if (columns.length == 0) { throw new IllegalArgumentException("columns.length==0"); } int newLines = 0; boolean todo = true; ColumnState[] columnStates = new ColumnState[columns.length]; for (int i = 0; i < columns.length; i++) { columnStates[i] = new ColumnState(columns[i].getText(), columns[i].getColumnInfo(), this.hyphenatorBuilder); } autoAdjustWidthOfColumns(columnStates, tableInfo); assert (verifyWithOfColumns(columnStates, tableInfo)); CellBuffer textFragmentBuilder = new CellBuffer(); while (todo) { todo = false; for (int columnIndex = 0; columnIndex < columnStates.length; columnIndex++) { ColumnState state = columnStates[columnIndex]; appendable.append(state.getColumnInfo().getBorderLeft()); append(appendable, state, textFragmentBuilder); if (!state.isComplete()) { todo = true; } appendable.append(state.getColumnInfo().getBorderRight()); } appendable.append(tableInfo.getLineSeparator()); newLines++; } return newLines; } catch (IOException e) { throw new IllegalStateException("Can not write to Appendable", e); } } DefaultLineWrapper(); @Inject void setHyphenatorBuilder(HyphenatorBuilder hyphenatorBuilder); @Override int wrap(Appendable appendable, TextTableInfo tableInfo, TextColumn... columns);  }
@Test public void testSingleColumn() { LineWrapper wrapper = getLineWrapper(); TextColumnInfo columnInfo = new TextColumnInfo(); columnInfo.setLocale(Locale.US); columnInfo.setBorderLeft("left|"); columnInfo.setBorderRight("|right"); String indent = ">"; columnInfo.setIndent(indent); TextTableInfo tableInfo = new TextTableInfo(); int borderWidth = columnInfo.getBorderWidth(); String text = "Hello world! This is wrapped text. It wraps perfectly well!\n" + "Indentation is '" + indent + "' but no indentation after double\n\n" + "newline."; for (int i = 30; i > 0; i--) { tableInfo.setWidth(i + borderWidth); StringBuilder buffer = new StringBuilder(); int lines = wrapper.wrap(buffer, tableInfo, text, columnInfo); if (lines > 1) { double lineRation = (text.length() / i); double ratio = lineRation / lines; assertThat(ratio).as("ratio").isGreaterThanOrEqualTo(0.5d); } checkWrappedColumnText(buffer, tableInfo, new TextColumn(text, columnInfo)); } }
@Test public void testMultiColumn() { LineWrapper wrapper = getLineWrapper(); TextTableInfo tableInfo = new TextTableInfo(); String text; TextColumn[] columns = new TextColumn[4]; text = "Hello world! This is wrapped text. It wraps perfectly well!\n" + "Indentation is not active at all.\n\nText after doulbe newline."; columns[0] = new TextColumn(text, createColumnInfo(0)); text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"; columns[1] = new TextColumn(text, createColumnInfo(1)); text = "This is a very long sentence without any newlines at all and with the " + "extraordinary long, strange and undefined and therefore un-hyphenate-able " + "word hushgigloobulathifagomasiwashutumiginatorgobonodile."; columns[2] = new TextColumn(text, createColumnInfo(2)); text = ""; columns[3] = new TextColumn(text, createColumnInfo(3)); for (int i = 80; i > 60; i--) { tableInfo.setWidth(i); StringBuilder buffer = new StringBuilder(); int lines = wrapper.wrap(buffer, tableInfo, columns); assertThat(lines).as("lines").isEqualTo(10); checkWrappedColumnText(buffer, tableInfo, columns); } }