Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
StringBlock Formatter for nicer log output
  • Loading branch information
pesse committed Feb 7, 2019
commit 9e572bda89464c31c913ead77ce05066e8ae29ff
77 changes: 77 additions & 0 deletions src/main/java/org/utplsql/cli/log/StringBlockFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.utplsql.cli.log;

public class StringBlockFormatter {

private String headline;
private 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);
}

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();
}
}
53 changes: 53 additions & 0 deletions src/test/java/org/utplsql/cli/StringBlockFormatterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.utplsql.cli;

import org.junit.jupiter.api.Test;
import org.utplsql.cli.log.StringBlockFormatter;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StringBlockFormatterTest {

@Test
void getBlockFormattedString() {

String expected =
"#### Headline ####\n" +
"# #\n" +
"# My value 1 #\n" +
"# #\n" +
"##################";

StringBlockFormatter formatter = new StringBlockFormatter("Headline");
formatter.append("My value 1");

assertEquals( expected, formatter.toString() );
}

@Test
void getEncapsulatedLine() {

String line = StringBlockFormatter.getEncapsulatedLine("val 1", 20);

assertEquals("# val 1 #", line);
}

@Test
void getEncapsulatedHeadline() {

assertEquals("######### headline #########",
StringBlockFormatter.getEncapsulatedHeadline("headline", 20));
assertEquals("######### headline ##########",
StringBlockFormatter.getEncapsulatedHeadline("headline", 21));
assertEquals("######### headline1 #########",
StringBlockFormatter.getEncapsulatedHeadline("headline1", 21));
assertEquals("######## headline1 #########",
StringBlockFormatter.getEncapsulatedHeadline("headline1", 20));
}

@Test
void getEmptyEncapsulatedHeadline() {

assertEquals("##################",
StringBlockFormatter.getEncapsulatedHeadline("", 10));
}
}