Skip to content

Commit 9e572bd

Browse files
committed
StringBlock Formatter for nicer log output
1 parent 606aacb commit 9e572bd

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.utplsql.cli.log;
2+
3+
public class StringBlockFormatter {
4+
5+
private String headline;
6+
private StringBuilder content = new StringBuilder();
7+
8+
public StringBlockFormatter() {}
9+
10+
public StringBlockFormatter(String headline) {
11+
setHeadline(headline);
12+
}
13+
14+
public void setHeadline(String headline) {
15+
this.headline = headline;
16+
}
17+
18+
public String getHeadline() {
19+
return headline;
20+
}
21+
22+
public void append( CharSequence seq ) {
23+
content.append(seq);
24+
}
25+
26+
private int getMaxLength( String[] lines ) {
27+
int len = 0;
28+
for ( String line : lines ) {
29+
if (line.length() > len)
30+
len = line.length();
31+
}
32+
33+
if ( headline.length() > (len+6))
34+
len = headline.length();
35+
36+
return len;
37+
}
38+
39+
public static String getEncapsulatedLine( String line, int maxLength ) {
40+
return String.format("# %-" + maxLength + "s #", line);
41+
}
42+
43+
public static String getEncapsulatedHeadline( String headline, int maxLength ) {
44+
String content = new String(new char[maxLength+8]).replace("\0", "#");
45+
if ( headline == null || headline.isEmpty() )
46+
return content;
47+
48+
headline = " " + headline + " ";
49+
int start = (int)Math.floor(
50+
(float)content.length()/2f
51+
-(float)headline.length()/2f
52+
);
53+
int end = start + headline.length();
54+
55+
return content.substring(0, start)
56+
+ headline
57+
+ content.substring(end);
58+
}
59+
60+
public String toString() {
61+
62+
String[] lines = content.toString().split("\n");
63+
int maxLen = getMaxLength(lines);
64+
65+
StringBuilder sb = new StringBuilder();
66+
67+
sb.append(getEncapsulatedHeadline(headline, maxLen)).append("\n");
68+
sb.append(getEncapsulatedLine("", maxLen)).append("\n");
69+
for ( String line : lines ) {
70+
sb.append(getEncapsulatedLine(line, maxLen)).append("\n");
71+
}
72+
sb.append(getEncapsulatedLine("", maxLen)).append("\n");
73+
sb.append(getEncapsulatedHeadline("", maxLen));
74+
75+
return sb.toString();
76+
}
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.utplsql.cli;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.utplsql.cli.log.StringBlockFormatter;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class StringBlockFormatterTest {
9+
10+
@Test
11+
void getBlockFormattedString() {
12+
13+
String expected =
14+
"#### Headline ####\n" +
15+
"# #\n" +
16+
"# My value 1 #\n" +
17+
"# #\n" +
18+
"##################";
19+
20+
StringBlockFormatter formatter = new StringBlockFormatter("Headline");
21+
formatter.append("My value 1");
22+
23+
assertEquals( expected, formatter.toString() );
24+
}
25+
26+
@Test
27+
void getEncapsulatedLine() {
28+
29+
String line = StringBlockFormatter.getEncapsulatedLine("val 1", 20);
30+
31+
assertEquals("# val 1 #", line);
32+
}
33+
34+
@Test
35+
void getEncapsulatedHeadline() {
36+
37+
assertEquals("######### headline #########",
38+
StringBlockFormatter.getEncapsulatedHeadline("headline", 20));
39+
assertEquals("######### headline ##########",
40+
StringBlockFormatter.getEncapsulatedHeadline("headline", 21));
41+
assertEquals("######### headline1 #########",
42+
StringBlockFormatter.getEncapsulatedHeadline("headline1", 21));
43+
assertEquals("######## headline1 #########",
44+
StringBlockFormatter.getEncapsulatedHeadline("headline1", 20));
45+
}
46+
47+
@Test
48+
void getEmptyEncapsulatedHeadline() {
49+
50+
assertEquals("##################",
51+
StringBlockFormatter.getEncapsulatedHeadline("", 10));
52+
}
53+
}

0 commit comments

Comments
 (0)