@@ -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 }
0 commit comments