java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
branchv_0
changeset 206 e2f24eea8543
parent 185 087d8ec75109
child 207 2bba68ef47c1
equal deleted inserted replaced
205:d6624c3b146a 206:e2f24eea8543
    20 import info.globalcode.sql.dk.ColorfulPrintWriter;
    20 import info.globalcode.sql.dk.ColorfulPrintWriter;
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    22 import static info.globalcode.sql.dk.Functions.lpad;
    22 import static info.globalcode.sql.dk.Functions.lpad;
    23 import static info.globalcode.sql.dk.Functions.rpad;
    23 import static info.globalcode.sql.dk.Functions.rpad;
    24 import static info.globalcode.sql.dk.Functions.repeat;
    24 import static info.globalcode.sql.dk.Functions.repeat;
       
    25 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
       
    26 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
       
    27 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    25 import java.util.List;
    28 import java.util.List;
    26 import java.util.regex.Matcher;
    29 import java.util.regex.Matcher;
    27 import java.util.regex.Pattern;
    30 import java.util.regex.Pattern;
    28 
    31 
    29 /**
    32 /**
    38  *
    41  *
    39  * @author Ing. František Kučera (frantovo.cz)
    42  * @author Ing. František Kučera (frantovo.cz)
    40  * @see TabularPrefetchingFormatter
    43  * @see TabularPrefetchingFormatter
    41  * @see TabularWrappingFormatter
    44  * @see TabularWrappingFormatter
    42  */
    45  */
       
    46 @PropertyDeclaration(name = COLORFUL, type = Boolean.class, description = COLORFUL_DESCRIPTION)
       
    47 @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
       
    48 @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, type = Boolean.class, description = "whether to trim the values to fit the column width")
    43 public class TabularFormatter extends AbstractFormatter {
    49 public class TabularFormatter extends AbstractFormatter {
    44 
    50 
    45 	public static final String NAME = "tabular"; // bash-completion:formatter
    51 	public static final String NAME = "tabular"; // bash-completion:formatter
    46 	private static final String HEADER_TYPE_PREFIX = " (";
    52 	private static final String HEADER_TYPE_PREFIX = " (";
    47 	private static final String HEADER_TYPE_SUFFIX = ")";
    53 	private static final String HEADER_TYPE_SUFFIX = ")";
    48 	public static final String PROPERTY_ASCII = "ascii";
    54 	public static final String PROPERTY_ASCII = "ascii";
    49 	public static final String PROPERTY_COLORFUL = "color";
       
    50 	public static final String PROPERTY_TRIM = "trim";
    55 	public static final String PROPERTY_TRIM = "trim";
    51 	private static final String NBSP = " ";
    56 	private static final String NBSP = " ";
    52 	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    57 	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    53 	protected ColorfulPrintWriter out;
    58 	protected ColorfulPrintWriter out;
    54 	private boolean firstResult = true;
    59 	private boolean firstResult = true;
    65 	public TabularFormatter(FormatterContext formatterContext) {
    70 	public TabularFormatter(FormatterContext formatterContext) {
    66 		super(formatterContext);
    71 		super(formatterContext);
    67 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    72 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    68 		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
    73 		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
    69 		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
    74 		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
    70 		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
    75 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    71 	}
    76 	}
    72 
    77 
    73 	@Override
    78 	@Override
    74 	public void writeStartResultSet(ColumnsHeader header) {
    79 	public void writeStartResultSet(ColumnsHeader header) {
    75 		super.writeStartResultSet(header);
    80 		super.writeStartResultSet(header);