A helper class for printing indented text. This can be used stand-alone or, more commonly, from Builders.
By default, a PrintWriter to System.out is used as the Writer, but it is possible to change the Writer by passing a new one as a constructor argument.
Indention by default is 2 characters but can be changed by passing a different value as a constructor argument.
The following is an example usage. Note that within a "with" block you need to specify a parameter name so that this.println is not called instead of IndentPrinter.println:
new IndentPrinter(new PrintWriter(out)).with { p -> p.printIndent() p.println('parent1') p.incrementIndent() p.printIndent() p.println('child 1') p.printIndent() p.println('child 2') p.decrementIndent() p.printIndent() p.println('parent2') p.flush() }
The above example prints this to standard output:
parent1 child 1 child 2 parent2
@author
James Strachan