Added WrappedPrinter, SmartConfig.load()
* Added `SmartConfig.load()` to allow runtime reloading of config. * Added `WrappedPrinter` which allows you to print horizontally aligned and offset text blocks in a monotype environment.
This commit is contained in:
parent
2950bd67db
commit
08810e88ed
@ -1,6 +1,6 @@
|
|||||||
#Tue, 25 Jan 2011 09:33:59 -0600
|
#Tue, 25 Jan 2011 09:33:59 -0600
|
||||||
name=jdb-util
|
name=jdb-util
|
||||||
version=1.2
|
version=1.3
|
||||||
lib.local=true
|
lib.local=true
|
||||||
|
|
||||||
build.number=3
|
build.number=3
|
||||||
|
@ -13,9 +13,7 @@ public class SmartConfig {
|
|||||||
this.@file = file
|
this.@file = file
|
||||||
this.@props = new Properties()
|
this.@props = new Properties()
|
||||||
|
|
||||||
try { file.withInputStream { is -> this.@props.load(is) } }
|
this.load()
|
||||||
catch (FileNotFoundException fnfe) {}
|
|
||||||
catch (Exception e) { log.warn("Cannot open config file.", e) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SmartConfig(String filename) {
|
public SmartConfig(String filename) {
|
||||||
@ -24,7 +22,16 @@ public class SmartConfig {
|
|||||||
log.trace("Loading configuration from {}",
|
log.trace("Loading configuration from {}",
|
||||||
new File(filename).canonicalPath)
|
new File(filename).canonicalPath)
|
||||||
}
|
}
|
||||||
public save() {
|
|
||||||
|
public synchronized load() {
|
||||||
|
log.trace("Loading configuration from {}", file.canonicalPath)
|
||||||
|
|
||||||
|
try { this.@file.withInputStream { is -> this.@props.load(is) } }
|
||||||
|
catch (FileNotFoundException fnfe) {}
|
||||||
|
catch (Exception e) { log.warn("Cannot open config file.", e) }
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized save() {
|
||||||
log.trace("Saving changes.")
|
log.trace("Saving changes.")
|
||||||
try {file.withOutputStream { os -> this.@props.store(os, "") } }
|
try {file.withOutputStream { os -> this.@props.store(os, "") } }
|
||||||
catch (Exception e) { log.warn("Cannot save config file.", e) }
|
catch (Exception e) { log.warn("Cannot save config file.", e) }
|
||||||
|
99
src/main/com/jdbernard/util/WrappedPrinter.java
Normal file
99
src/main/com/jdbernard/util/WrappedPrinter.java
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package com.jdbernard.util;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
public class WrappedPrinter {
|
||||||
|
|
||||||
|
private PrintWriter printer;
|
||||||
|
private int wrapWidth;
|
||||||
|
private int lastPrintedLineLength = 0;
|
||||||
|
|
||||||
|
public WrappedPrinter(OutputStream os, int wrapWidth,
|
||||||
|
boolean autoflush) {
|
||||||
|
this(new PrintWriter(os, autoflush), wrapWidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WrappedPrinter(PrintWriter pw, int wrapWidth) {
|
||||||
|
this.printer = pw;
|
||||||
|
this.wrapWidth = wrapWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(String message) { print(message, ""); }
|
||||||
|
|
||||||
|
public void print(String message, String offset) {
|
||||||
|
int lastSpaceIdx = 0;
|
||||||
|
int curLineLength = 0;
|
||||||
|
int lineStartIdx = 0;
|
||||||
|
int i = 0;
|
||||||
|
int actualWidth = wrapWidth - offset.length();
|
||||||
|
|
||||||
|
//message = message.replaceAll("[\n\r]", " ");
|
||||||
|
|
||||||
|
// print initial offset if this is the beginning of the line
|
||||||
|
if (lastPrintedLineLength == 0) {
|
||||||
|
printer.print(offset);
|
||||||
|
curLineLength = offset.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (i = 0; i < message.length(); ++i) {
|
||||||
|
|
||||||
|
curLineLength++;
|
||||||
|
// see a tab, advance the line length by the appropriate amount
|
||||||
|
if (message.charAt(i) == '\t') curLineLength += 7;
|
||||||
|
|
||||||
|
// see a newline
|
||||||
|
if (message.charAt(i) == '\n') {
|
||||||
|
// mark it as the last seen whitespace
|
||||||
|
lastSpaceIdx = i;
|
||||||
|
|
||||||
|
// push the line length over the limit, forcing a return
|
||||||
|
curLineLength += actualWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// line has overflowed the prescribed width
|
||||||
|
if (curLineLength > actualWidth) {
|
||||||
|
// print up to the last space before the overflow
|
||||||
|
printer.println(message.substring(lineStartIdx, lastSpaceIdx));
|
||||||
|
|
||||||
|
// pick up the next line after said space
|
||||||
|
lineStartIdx = lastSpaceIdx + 1;
|
||||||
|
i = lastSpaceIdx;
|
||||||
|
curLineLength = 0;
|
||||||
|
|
||||||
|
// print initial offset if there is still more to print
|
||||||
|
if (lineStartIdx < message.length()) {
|
||||||
|
printer.print(offset);
|
||||||
|
curLineLength = offset.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// see whitespace, update last space index
|
||||||
|
if (Character.isWhitespace(message.charAt(i))) lastSpaceIdx = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// any left over, it will fit on one line
|
||||||
|
if (i - lineStartIdx > 0) {
|
||||||
|
String lastLine = message.substring(lineStartIdx);
|
||||||
|
printer.print(lastLine);
|
||||||
|
curLineLength += lastLine.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
// save back the new position
|
||||||
|
lastPrintedLineLength = curLineLength;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void println() { println("", ""); }
|
||||||
|
|
||||||
|
public void println(String message) { println(message, ""); }
|
||||||
|
|
||||||
|
public void println(String message, String offset) {
|
||||||
|
|
||||||
|
print(message, offset);
|
||||||
|
printer.println();
|
||||||
|
lastPrintedLineLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user