diff --git a/project.properties b/project.properties index d054050..140132f 100644 --- a/project.properties +++ b/project.properties @@ -1,6 +1,6 @@ #Tue, 25 Jan 2011 09:33:59 -0600 name=jdb-util -version=1.2 +version=1.3 lib.local=true build.number=3 diff --git a/src/main/com/jdbernard/util/SmartConfig.groovy b/src/main/com/jdbernard/util/SmartConfig.groovy index 62181a3..227444f 100644 --- a/src/main/com/jdbernard/util/SmartConfig.groovy +++ b/src/main/com/jdbernard/util/SmartConfig.groovy @@ -13,9 +13,7 @@ public class SmartConfig { this.@file = file this.@props = new Properties() - try { file.withInputStream { is -> this.@props.load(is) } } - catch (FileNotFoundException fnfe) {} - catch (Exception e) { log.warn("Cannot open config file.", e) } + this.load() } public SmartConfig(String filename) { @@ -24,7 +22,16 @@ public class SmartConfig { log.trace("Loading configuration from {}", 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.") try {file.withOutputStream { os -> this.@props.store(os, "") } } catch (Exception e) { log.warn("Cannot save config file.", e) } diff --git a/src/main/com/jdbernard/util/WrappedPrinter.java b/src/main/com/jdbernard/util/WrappedPrinter.java new file mode 100644 index 0000000..dd4e949 --- /dev/null +++ b/src/main/com/jdbernard/util/WrappedPrinter.java @@ -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; + } + +}