Skip to content

Commit

Permalink
* added more configuration options
Browse files Browse the repository at this point in the history
* fixed a bug in linux where class-paths were not split properly
  • Loading branch information
Willie authored and Willie committed Feb 20, 2014
1 parent 20b31de commit 277d1fe
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
120 changes: 90 additions & 30 deletions src/main/java/com/github/epochcoder/prettyconsole/ConsoleBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,44 @@ public final class ConsoleBox {
/**
* the character to use in box corners
*/
private static final String BOX_CHAR = " + ";
private String boxChar = " + ";

/**
* the character to use to pad strings with
*/
private static final char PAD_CHAR = ' ';
private char padChar = ' ';

/**
* the character used for the box's right and left sides
*/
private static final String END_CHAR = " | ";
private String endChar = " | ";

/**
* the character used for the box's top, title and bottom sides
*/
private static final String TB_CHAR = "-";
private String borderChar = "-";

/**
* the character used for representing black in a color
*/
private static final String BLACK_CHAR = " ";
private String blackChar = " ";

/**
* the character used for representing white in a color
*/
private static final String WHITE_CHAR = "#";
private String whiteChar = "#";

/**
* the character used for representing aliasing of fonts.
* actually other colors, but since our image is only black and white
* it will represent a shade, which in this case is aliasing
*/
private static final String ALIAS_CHAR = " ";
private String aliasChar = " ";

/**
* the key value separator for names and values
*/
private static final String KEY_VALUE_SEP = " : ";

private String keyValueSeparator = " : ";

private final List<ConsoleBoxKeyHandler> handlers;
private final StringBuilder builder;
Expand Down Expand Up @@ -98,7 +97,72 @@ public ConsoleBox(int boxWidth, String title) {
public ConsoleBox(int boxWidth) {
this(boxWidth, null);
}

/**
* pads a string from both sides
* @param string the string to pad
* @param pad the padding character
* @param length the length to pad
* @return the padded string
*/
private String padBoth(String string, String pad, int length) {
int right = (length - string.length()) / 2 + string.length();
String result = Strings.padEnd(string, right, pad.toCharArray()[0]);
return Strings.padStart(result, length, pad.toCharArray()[0]);
}

/**
* configures the box characters, note that sensible defaults are already set.
* @param cornerChar the character to use in box corners
* @param padChar the character to use to pad strings with
* @param sideChar the character used for the box's right and left sides
* @param borderChar the character used for the box's top, title and bottom sides
* @return the current instance
*/
public ConsoleBox setBoxCharacters(String cornerChar, char padChar, String sideChar, String borderChar) {
this.boxChar = Preconditions.checkNotNull(cornerChar);
this.padChar = Preconditions.checkNotNull(padChar);
this.endChar = Preconditions.checkNotNull(sideChar);
this.borderChar = Preconditions.checkNotNull(borderChar);
return this;
}

/**
* configures the box's ASCII characters, note that sensible defaults are already set.
* @param blackChar the character used for representing black in a color
* @param whiteChar the character used for representing white in a color
* @param aliasChar the character used for representing aliasing of fonts.
* actually other colors, but since our image is only black and white
* it will represent a shade, which in this case is aliasing.
* @return the current instance
*/
public ConsoleBox setAsciiCharacters(String blackChar, String whiteChar, String aliasChar) {
this.blackChar = Preconditions.checkNotNull(blackChar);
this.whiteChar = Preconditions.checkNotNull(whiteChar);
this.aliasChar = Preconditions.checkNotNull(aliasChar);

return this;
}

/**
* configures the box's key-value separator.
* @param keyValueSeperator the character used for representing black in a color
* @param whiteChar the character used for representing white in a color
* @param aliasChar the character used for representing aliasing of fonts.
* actually other colors, but since our image is only black and white
* it will represent a shade, which in this case is aliasing.
* @return the current instance
*/
public ConsoleBox setKeyValueSeparator(String keyValueSeparator) {
this.keyValueSeparator = Preconditions.checkNotNull(keyValueSeparator);
return this;
}

/**
* adds a new key-value handler for this ConsoleBox
* @param handler the handler to use
* @return the current instance
*/
public ConsoleBox handler(ConsoleBoxKeyHandler handler) {
this.handlers.add(handler);
return this;
Expand All @@ -113,20 +177,14 @@ public void build(PrintStream output) {
output.println(this.builder.toString());
}

private String padBoth(String string, String pad, int length) {
int right = (length - string.length()) / 2 + string.length();
String result = Strings.padEnd(string, right, pad.toCharArray()[0]);
return Strings.padStart(result, length, pad.toCharArray()[0]);
}

/**
* adds a title section to the console box
* @param title the title to use
* @return the current box
*/
public ConsoleBox title(String title) {
this.builder.append("\n" + BOX_CHAR).append(padBoth(title,
TB_CHAR, this.width)).append(BOX_CHAR);
this.builder.append("\n").append(this.boxChar).append(padBoth(title,
this.borderChar, this.width)).append(this.boxChar);

return this;
}
Expand All @@ -136,8 +194,8 @@ public ConsoleBox title(String title) {
* @return the current box
*/
public ConsoleBox empty() {
this.builder.append("\n" + BOX_CHAR).append(
padBoth("", " ", this.width)).append(BOX_CHAR);
this.builder.append("\n").append(this.boxChar).append(
padBoth("", " ", this.width)).append(this.boxChar);

return this;
}
Expand Down Expand Up @@ -172,22 +230,22 @@ public ConsoleBox ascii(String text, boolean invert) {
final int iHeight = image.getHeight();
final int iWidth = image.getWidth();

final String bChar = invert ? WHITE_CHAR : BLACK_CHAR;
final String wChar = invert ? BLACK_CHAR : WHITE_CHAR;
final String bChar = invert ? this.whiteChar : this.blackChar;
final String wChar = invert ? this.blackChar : this.whiteChar;

for (int y = 0; y < iHeight; y++) {
final StringBuilder sb = new StringBuilder();
for (int x = 0; x < iWidth; x++) {
final int rgbColor = image.getRGB(x, y);
sb.append(rgbColor == -16777216 ? bChar : rgbColor == -1 ? wChar : ALIAS_CHAR);
sb.append(rgbColor == -16777216 ? bChar : rgbColor == -1 ? wChar : aliasChar);
}

if (sb.toString().trim().isEmpty()) {
continue;
}

this.builder.append("\n" + END_CHAR)
.append(sb).append(END_CHAR);
this.builder.append("\n").append(this.endChar)
.append(sb).append(this.endChar);
}

return this;
Expand All @@ -206,14 +264,16 @@ public ConsoleBox line(String key, String value) {

// get the key length
final int kL = key.length();
final int kSl = this.keyValueSeparator.length();

// calculate remaining box space for the value
final int ths = (this.width - kL - KEY_VALUE_SEP.length());
final int ths = (this.width - kL - kSl);
Preconditions.checkState(ths > -1, "key[" + key + "] is to long "
+ "for box with a " + width + " width!");

// \n | the_key_length_in_spaces
final String joinOn = ("\n" + END_CHAR + Strings.padEnd("",
kL + KEY_VALUE_SEP.length(), PAD_CHAR));
final String joinOn = ("\n" + this.endChar + Strings.padEnd("",
kL + kSl, this.padChar));

// get key handlers and modify if neccessary
for (ConsoleBoxKeyHandler handler : this.handlers) {
Expand All @@ -234,13 +294,13 @@ public ConsoleBox line(String key, String value) {
Iterables.transform(splitted, new Function<String, String>() {
@Override
public String apply(String input) {
return Strings.padEnd(input, ths, ' ') + END_CHAR;
return Strings.padEnd(input, ths, ' ') + endChar;
}
}));

// write completed line to builder
this.builder.append("\n" + END_CHAR).append(key)
.append(KEY_VALUE_SEP).append(formatted);
this.builder.append("\n").append(this.endChar).append(key)
.append(this.keyValueSeparator).append(formatted);

this.content = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/**
* interface for defining handlers for certain console box keys
* @author Willie Scholtz
* @version 1.43
*/
public interface ConsoleBoxKeyHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/**
* ensures no passwords are present in the ConsoleBox
* @author Willie Scholtz
* @version 1.43
*/
public class ConsoleBoxPasswordHandler implements ConsoleBoxKeyHandler {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.github.epochcoder.prettyconsole.handlers;

import com.github.epochcoder.prettyconsole.ConsoleBoxKeyHandler;
Expand All @@ -9,9 +10,11 @@
/**
* ensures that JVM system properties are handled correctly by consolebox
* @author Willie Scholtz
* @version 1.43
*/
public class JvmKeyHandler implements ConsoleBoxKeyHandler {

private static final boolean WINDOWS = System.getProperty("os.name").indexOf("Windows") > -1;

/**
* keys that should be converted to new lines
*/
Expand Down Expand Up @@ -47,7 +50,7 @@ public boolean shouldHandle(String key) {
public String handleValue(String key, String value) {
for (String string : CLASSPATHS) {
if (string.equals(key)) {
value = Joiner.on("\n").join(Iterables.transform(Splitter.on(";").split(value),
value = Joiner.on("\n").join(Iterables.transform(Splitter.on(WINDOWS ? ";" : ":").split(value),
new Function<String, String>() {
int item = 0;
@Override
Expand Down

0 comments on commit 277d1fe

Please sign in to comment.