java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 142 da1e38386d84
child 169 4e131a0b521a
permissions -rw-r--r--
more JavaDoc
     1 /**
     2  * SQL-DK
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    20 import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
    21 import java.util.ArrayList;
    22 import java.util.List;
    23 import static info.globalcode.sql.dk.Functions.lpad;
    24 import static info.globalcode.sql.dk.Functions.rpad;
    25 import static info.globalcode.sql.dk.Functions.repeat;
    26 
    27 /**
    28  * Longer values are line-wrapped – the cell then contains multiple lines. Marks are added to
    29  * signalize forced line ends (not present in original data).
    30  *
    31  * @author Ing. František Kučera (frantovo.cz)
    32  */
    33 public class TabularWrappingFormatter extends TabularFormatter {
    34 
    35 	public static final String NAME = "tabular-wrapping"; // bash-completion:formatter
    36 	private List<String[]> currentRow;
    37 
    38 	public TabularWrappingFormatter(FormatterContext formatterContext) {
    39 		super(formatterContext);
    40 	}
    41 
    42 	@Override
    43 	public void writeStartResultSet(ColumnsHeader header) {
    44 		super.writeStartResultSet(header);
    45 		currentRow = new ArrayList<>(header.getColumnCount());
    46 	}
    47 
    48 	@Override
    49 	protected void writeColumnValueInternal(Object value) {
    50 		boolean rightAlign = value instanceof Number || value instanceof Boolean;
    51 		String valueString = String.valueOf(value);
    52 		int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1;  // -1 = space for new line symbol
    53 		currentRow.add(wrapLines(valueString, columnWidth, rightAlign));
    54 	}
    55 
    56 	@Override
    57 	public void writeEndRow() {
    58 		super.writeEndRow();
    59 
    60 		int wrappedLine = 0;
    61 		boolean hasMoreWrappedLines;
    62 
    63 		do {
    64 			hasMoreWrappedLines = false;
    65 			for (int i = 0; i < currentRow.size(); i++) {
    66 				if (i == 0) {
    67 					printTableIndent();
    68 					printTableBorder("│ ");
    69 				} else {
    70 					printTableBorder(" │ ");
    71 				}
    72 				String[] columnArray = currentRow.get(i);
    73 				if (wrappedLine < columnArray.length) {
    74 					printValueWithNewLinesReplaced(columnArray[wrappedLine]);
    75 
    76 					if (wrappedLine < columnArray.length - 1) {
    77 						out.print(TerminalColor.Red, "↩");
    78 						hasMoreWrappedLines = true;
    79 					} else {
    80 						out.print(" ");
    81 					}
    82 
    83 				} else {
    84 					out.print(repeat(' ', getColumnWidth(i + 1)));
    85 				}
    86 
    87 				if (i == (currentRow.size() - 1)) {
    88 					printTableBorder(" │");
    89 				}
    90 			}
    91 			out.println();
    92 			out.flush();
    93 			wrappedLine++;
    94 		} while (hasMoreWrappedLines);
    95 
    96 		currentRow.clear();
    97 	}
    98 
    99 	@Override
   100 	public void writeEndRowInternal() {
   101 		// already done – wrapped row ends
   102 	}
   103 
   104 	private static String[] wrapLines(String s, int width, boolean rightAlign) {
   105 		String[] array = new String[(s.length() - 1) / width + 1];
   106 		for (int i = 0; i < array.length; i++) {
   107 			if (i == array.length - 1) {
   108 				String part = s.substring(i * width, s.length());
   109 				array[i] = rightAlign ? lpad(part, width) : rpad(part, width);
   110 			} else {
   111 				array[i] = s.substring(i * width, (i + 1) * width);
   112 			}
   113 		}
   114 		return array;
   115 	}
   116 }