forked from utPLSQL/utPLSQL-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBlockFormatter.java
More file actions
85 lines (65 loc) · 2.25 KB
/
StringBlockFormatter.java
File metadata and controls
85 lines (65 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.utplsql.cli.log;
public class StringBlockFormatter {
private String headline;
private final StringBuilder content = new StringBuilder();
public StringBlockFormatter() {
}
public StringBlockFormatter(String headline) {
setHeadline(headline);
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getHeadline() {
return headline;
}
public void append(CharSequence seq) {
content.append(seq);
}
public void appendLine(CharSequence seq) {
content.append(seq).append("\n");
}
private int getMaxLength(String[] lines) {
int len = 0;
for (String line : lines) {
if (line.length() > len) {
len = line.length();
}
}
if (headline.length() > (len + 6)) {
len = headline.length();
}
return len;
}
public static String getEncapsulatedLine(String line, int maxLength) {
return String.format("# %-" + maxLength + "s #", line);
}
public static String getEncapsulatedHeadline(String headline, int maxLength) {
String content = new String(new char[maxLength + 8]).replace("\0", "#");
if (headline == null || headline.isEmpty()) {
return content;
}
headline = " " + headline + " ";
int start = (int) Math.floor(
(float) content.length() / 2f
- (float) headline.length() / 2f
);
int end = start + headline.length();
return content.substring(0, start)
+ headline
+ content.substring(end);
}
public String toString() {
String[] lines = content.toString().split("\n");
int maxLen = getMaxLength(lines);
StringBuilder sb = new StringBuilder();
sb.append(getEncapsulatedHeadline(headline, maxLen)).append("\n");
sb.append(getEncapsulatedLine("", maxLen)).append("\n");
for (String line : lines) {
sb.append(getEncapsulatedLine(line, maxLen)).append("\n");
}
sb.append(getEncapsulatedLine("", maxLen)).append("\n");
sb.append(getEncapsulatedHeadline("", maxLen));
return sb.toString();
}
}