@@ -28,45 +28,44 @@ public final class ConsoleBox {
28
28
/**
29
29
* the character to use in box corners
30
30
*/
31
- private static final String BOX_CHAR = " + " ;
31
+ private String boxChar = " + " ;
32
32
33
33
/**
34
34
* the character to use to pad strings with
35
35
*/
36
- private static final char PAD_CHAR = ' ' ;
36
+ private char padChar = ' ' ;
37
37
38
38
/**
39
39
* the character used for the box's right and left sides
40
40
*/
41
- private static final String END_CHAR = " | " ;
41
+ private String endChar = " | " ;
42
42
43
43
/**
44
44
* the character used for the box's top, title and bottom sides
45
45
*/
46
- private static final String TB_CHAR = "-" ;
46
+ private String borderChar = "-" ;
47
47
48
48
/**
49
49
* the character used for representing black in a color
50
50
*/
51
- private static final String BLACK_CHAR = " " ;
51
+ private String blackChar = " " ;
52
52
53
53
/**
54
54
* the character used for representing white in a color
55
55
*/
56
- private static final String WHITE_CHAR = "#" ;
56
+ private String whiteChar = "#" ;
57
57
58
58
/**
59
59
* the character used for representing aliasing of fonts.
60
60
* actually other colors, but since our image is only black and white
61
61
* it will represent a shade, which in this case is aliasing
62
62
*/
63
- private static final String ALIAS_CHAR = " " ;
63
+ private String aliasChar = " " ;
64
64
65
65
/**
66
66
* the key value separator for names and values
67
67
*/
68
- private static final String KEY_VALUE_SEP = " : " ;
69
-
68
+ private String keyValueSeparator = " : " ;
70
69
71
70
private final List <ConsoleBoxKeyHandler > handlers ;
72
71
private final StringBuilder builder ;
@@ -98,7 +97,72 @@ public ConsoleBox(int boxWidth, String title) {
98
97
public ConsoleBox (int boxWidth ) {
99
98
this (boxWidth , null );
100
99
}
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 );
101
143
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
+ */
102
166
public ConsoleBox handler (ConsoleBoxKeyHandler handler ) {
103
167
this .handlers .add (handler );
104
168
return this ;
@@ -113,20 +177,14 @@ public void build(PrintStream output) {
113
177
output .println (this .builder .toString ());
114
178
}
115
179
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
-
122
180
/**
123
181
* adds a title section to the console box
124
182
* @param title the title to use
125
183
* @return the current box
126
184
*/
127
185
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 );
130
188
131
189
return this ;
132
190
}
@@ -136,8 +194,8 @@ public ConsoleBox title(String title) {
136
194
* @return the current box
137
195
*/
138
196
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 );
141
199
142
200
return this ;
143
201
}
@@ -172,22 +230,22 @@ public ConsoleBox ascii(String text, boolean invert) {
172
230
final int iHeight = image .getHeight ();
173
231
final int iWidth = image .getWidth ();
174
232
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 ;
177
235
178
236
for (int y = 0 ; y < iHeight ; y ++) {
179
237
final StringBuilder sb = new StringBuilder ();
180
238
for (int x = 0 ; x < iWidth ; x ++) {
181
239
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 );
183
241
}
184
242
185
243
if (sb .toString ().trim ().isEmpty ()) {
186
244
continue ;
187
245
}
188
246
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 );
191
249
}
192
250
193
251
return this ;
@@ -206,14 +264,16 @@ public ConsoleBox line(String key, String value) {
206
264
207
265
// get the key length
208
266
final int kL = key .length ();
267
+ final int kSl = this .keyValueSeparator .length ();
268
+
209
269
// 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 );
211
271
Preconditions .checkState (ths > -1 , "key[" + key + "] is to long "
212
272
+ "for box with a " + width + " width!" );
213
273
214
274
// \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 ));
217
277
218
278
// get key handlers and modify if neccessary
219
279
for (ConsoleBoxKeyHandler handler : this .handlers ) {
@@ -234,13 +294,13 @@ public ConsoleBox line(String key, String value) {
234
294
Iterables .transform (splitted , new Function <String , String >() {
235
295
@ Override
236
296
public String apply (String input ) {
237
- return Strings .padEnd (input , ths , ' ' ) + END_CHAR ;
297
+ return Strings .padEnd (input , ths , ' ' ) + endChar ;
238
298
}
239
299
}));
240
300
241
301
// 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 );
244
304
245
305
this .content = true ;
246
306
}
0 commit comments