Skip to content

Commit 277d1fe

Browse files
WillieWillie
Willie
authored and
Willie
committed
* added more configuration options
* fixed a bug in linux where class-paths were not split properly
1 parent 20b31de commit 277d1fe

File tree

5 files changed

+96
-34
lines changed

5 files changed

+96
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

src/main/java/com/github/epochcoder/prettyconsole/ConsoleBox.java

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,44 @@ public final class ConsoleBox {
2828
/**
2929
* the character to use in box corners
3030
*/
31-
private static final String BOX_CHAR = " + ";
31+
private String boxChar = " + ";
3232

3333
/**
3434
* the character to use to pad strings with
3535
*/
36-
private static final char PAD_CHAR = ' ';
36+
private char padChar = ' ';
3737

3838
/**
3939
* the character used for the box's right and left sides
4040
*/
41-
private static final String END_CHAR = " | ";
41+
private String endChar = " | ";
4242

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

4848
/**
4949
* the character used for representing black in a color
5050
*/
51-
private static final String BLACK_CHAR = " ";
51+
private String blackChar = " ";
5252

5353
/**
5454
* the character used for representing white in a color
5555
*/
56-
private static final String WHITE_CHAR = "#";
56+
private String whiteChar = "#";
5757

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

6565
/**
6666
* the key value separator for names and values
6767
*/
68-
private static final String KEY_VALUE_SEP = " : ";
69-
68+
private String keyValueSeparator = " : ";
7069

7170
private final List<ConsoleBoxKeyHandler> handlers;
7271
private final StringBuilder builder;
@@ -98,7 +97,72 @@ public ConsoleBox(int boxWidth, String title) {
9897
public ConsoleBox(int boxWidth) {
9998
this(boxWidth, null);
10099
}
100+
101+
/**
102+
* pads a string from both sides
103+
* @param string the string to pad
104+
* @param pad the padding character
105+
* @param length the length to pad
106+
* @return the padded string
107+
*/
108+
private String padBoth(String string, String pad, int length) {
109+
int right = (length - string.length()) / 2 + string.length();
110+
String result = Strings.padEnd(string, right, pad.toCharArray()[0]);
111+
return Strings.padStart(result, length, pad.toCharArray()[0]);
112+
}
113+
114+
/**
115+
* configures the box characters, note that sensible defaults are already set.
116+
* @param cornerChar the character to use in box corners
117+
* @param padChar the character to use to pad strings with
118+
* @param sideChar the character used for the box's right and left sides
119+
* @param borderChar the character used for the box's top, title and bottom sides
120+
* @return the current instance
121+
*/
122+
public ConsoleBox setBoxCharacters(String cornerChar, char padChar, String sideChar, String borderChar) {
123+
this.boxChar = Preconditions.checkNotNull(cornerChar);
124+
this.padChar = Preconditions.checkNotNull(padChar);
125+
this.endChar = Preconditions.checkNotNull(sideChar);
126+
this.borderChar = Preconditions.checkNotNull(borderChar);
127+
return this;
128+
}
129+
130+
/**
131+
* configures the box's ASCII characters, note that sensible defaults are already set.
132+
* @param blackChar the character used for representing black in a color
133+
* @param whiteChar the character used for representing white in a color
134+
* @param aliasChar the character used for representing aliasing of fonts.
135+
* actually other colors, but since our image is only black and white
136+
* it will represent a shade, which in this case is aliasing.
137+
* @return the current instance
138+
*/
139+
public ConsoleBox setAsciiCharacters(String blackChar, String whiteChar, String aliasChar) {
140+
this.blackChar = Preconditions.checkNotNull(blackChar);
141+
this.whiteChar = Preconditions.checkNotNull(whiteChar);
142+
this.aliasChar = Preconditions.checkNotNull(aliasChar);
101143

144+
return this;
145+
}
146+
147+
/**
148+
* configures the box's key-value separator.
149+
* @param keyValueSeperator the character used for representing black in a color
150+
* @param whiteChar the character used for representing white in a color
151+
* @param aliasChar the character used for representing aliasing of fonts.
152+
* actually other colors, but since our image is only black and white
153+
* it will represent a shade, which in this case is aliasing.
154+
* @return the current instance
155+
*/
156+
public ConsoleBox setKeyValueSeparator(String keyValueSeparator) {
157+
this.keyValueSeparator = Preconditions.checkNotNull(keyValueSeparator);
158+
return this;
159+
}
160+
161+
/**
162+
* adds a new key-value handler for this ConsoleBox
163+
* @param handler the handler to use
164+
* @return the current instance
165+
*/
102166
public ConsoleBox handler(ConsoleBoxKeyHandler handler) {
103167
this.handlers.add(handler);
104168
return this;
@@ -113,20 +177,14 @@ public void build(PrintStream output) {
113177
output.println(this.builder.toString());
114178
}
115179

116-
private String padBoth(String string, String pad, int length) {
117-
int right = (length - string.length()) / 2 + string.length();
118-
String result = Strings.padEnd(string, right, pad.toCharArray()[0]);
119-
return Strings.padStart(result, length, pad.toCharArray()[0]);
120-
}
121-
122180
/**
123181
* adds a title section to the console box
124182
* @param title the title to use
125183
* @return the current box
126184
*/
127185
public ConsoleBox title(String title) {
128-
this.builder.append("\n" + BOX_CHAR).append(padBoth(title,
129-
TB_CHAR, this.width)).append(BOX_CHAR);
186+
this.builder.append("\n").append(this.boxChar).append(padBoth(title,
187+
this.borderChar, this.width)).append(this.boxChar);
130188

131189
return this;
132190
}
@@ -136,8 +194,8 @@ public ConsoleBox title(String title) {
136194
* @return the current box
137195
*/
138196
public ConsoleBox empty() {
139-
this.builder.append("\n" + BOX_CHAR).append(
140-
padBoth("", " ", this.width)).append(BOX_CHAR);
197+
this.builder.append("\n").append(this.boxChar).append(
198+
padBoth("", " ", this.width)).append(this.boxChar);
141199

142200
return this;
143201
}
@@ -172,22 +230,22 @@ public ConsoleBox ascii(String text, boolean invert) {
172230
final int iHeight = image.getHeight();
173231
final int iWidth = image.getWidth();
174232

175-
final String bChar = invert ? WHITE_CHAR : BLACK_CHAR;
176-
final String wChar = invert ? BLACK_CHAR : WHITE_CHAR;
233+
final String bChar = invert ? this.whiteChar : this.blackChar;
234+
final String wChar = invert ? this.blackChar : this.whiteChar;
177235

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

185243
if (sb.toString().trim().isEmpty()) {
186244
continue;
187245
}
188246

189-
this.builder.append("\n" + END_CHAR)
190-
.append(sb).append(END_CHAR);
247+
this.builder.append("\n").append(this.endChar)
248+
.append(sb).append(this.endChar);
191249
}
192250

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

207265
// get the key length
208266
final int kL = key.length();
267+
final int kSl = this.keyValueSeparator.length();
268+
209269
// calculate remaining box space for the value
210-
final int ths = (this.width - kL - KEY_VALUE_SEP.length());
270+
final int ths = (this.width - kL - kSl);
211271
Preconditions.checkState(ths > -1, "key[" + key + "] is to long "
212272
+ "for box with a " + width + " width!");
213273

214274
// \n | the_key_length_in_spaces
215-
final String joinOn = ("\n" + END_CHAR + Strings.padEnd("",
216-
kL + KEY_VALUE_SEP.length(), PAD_CHAR));
275+
final String joinOn = ("\n" + this.endChar + Strings.padEnd("",
276+
kL + kSl, this.padChar));
217277

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

241301
// write completed line to builder
242-
this.builder.append("\n" + END_CHAR).append(key)
243-
.append(KEY_VALUE_SEP).append(formatted);
302+
this.builder.append("\n").append(this.endChar).append(key)
303+
.append(this.keyValueSeparator).append(formatted);
244304

245305
this.content = true;
246306
}

src/main/java/com/github/epochcoder/prettyconsole/ConsoleBoxKeyHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/**
44
* interface for defining handlers for certain console box keys
55
* @author Willie Scholtz
6-
* @version 1.43
76
*/
87
public interface ConsoleBoxKeyHandler {
98

src/main/java/com/github/epochcoder/prettyconsole/handlers/ConsoleBoxPasswordHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/**
77
* ensures no passwords are present in the ConsoleBox
88
* @author Willie Scholtz
9-
* @version 1.43
109
*/
1110
public class ConsoleBoxPasswordHandler implements ConsoleBoxKeyHandler {
1211
/**

src/main/java/com/github/epochcoder/prettyconsole/handlers/JvmKeyHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.github.epochcoder.prettyconsole.handlers;
23

34
import com.github.epochcoder.prettyconsole.ConsoleBoxKeyHandler;
@@ -9,9 +10,11 @@
910
/**
1011
* ensures that JVM system properties are handled correctly by consolebox
1112
* @author Willie Scholtz
12-
* @version 1.43
1313
*/
1414
public class JvmKeyHandler implements ConsoleBoxKeyHandler {
15+
16+
private static final boolean WINDOWS = System.getProperty("os.name").indexOf("Windows") > -1;
17+
1518
/**
1619
* keys that should be converted to new lines
1720
*/
@@ -47,7 +50,7 @@ public boolean shouldHandle(String key) {
4750
public String handleValue(String key, String value) {
4851
for (String string : CLASSPATHS) {
4952
if (string.equals(key)) {
50-
value = Joiner.on("\n").join(Iterables.transform(Splitter.on(";").split(value),
53+
value = Joiner.on("\n").join(Iterables.transform(Splitter.on(WINDOWS ? ";" : ":").split(value),
5154
new Function<String, String>() {
5255
int item = 0;
5356
@Override

0 commit comments

Comments
 (0)