Package groovy.util
Class IndentPrinter
- java.lang.Object
-
- groovy.util.IndentPrinter
-
public class IndentPrinter extends java.lang.Object
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
The above example prints this to standard output:->
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() }parent1 child 1 child 2 parent2
-
-
Constructor Summary
Constructors Constructor Description IndentPrinter()
Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces.IndentPrinter(java.io.Writer out)
Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces.IndentPrinter(java.io.Writer out, java.lang.String indent)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting.IndentPrinter(java.io.Writer out, java.lang.String indent, boolean addNewlines)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting and the ability to override newline handling.IndentPrinter(java.io.Writer out, java.lang.String indent, boolean addNewlines, boolean autoIndent)
Create an IndentPrinter to the given PrintWriter
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
decrementIndent()
void
flush()
boolean
getAutoIndent()
int
getIndentLevel()
void
incrementIndent()
void
print(char c)
Prints a character.void
print(java.lang.String text)
Prints a string.void
printIndent()
Prints the current indent level.void
println()
Prints an end-of-line character (if enabled via addNewLines property).void
println(java.lang.String text)
Prints a string followed by an end of line character.void
setAutoIndent(boolean autoIndent)
void
setIndentLevel(int indentLevel)
-
-
-
Constructor Detail
-
IndentPrinter
public IndentPrinter()
Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces.- See Also:
IndentPrinter(Writer, String)
-
IndentPrinter
public IndentPrinter(java.io.Writer out)
Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces.- Parameters:
out
- Writer to output to- See Also:
IndentPrinter(Writer, String)
-
IndentPrinter
public IndentPrinter(java.io.Writer out, java.lang.String indent)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting.- Parameters:
out
- Writer to output toindent
- character(s) used to indent each line
-
IndentPrinter
public IndentPrinter(java.io.Writer out, java.lang.String indent, boolean addNewlines)
Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting and the ability to override newline handling.- Parameters:
out
- Writer to output toindent
- character(s) used to indent each lineaddNewlines
- set to false to gobble all new lines (default true)
-
IndentPrinter
public IndentPrinter(java.io.Writer out, java.lang.String indent, boolean addNewlines, boolean autoIndent)
Create an IndentPrinter to the given PrintWriter- Parameters:
out
- Writer to output toindent
- character(s) used to indent each lineaddNewlines
- set to false to gobble all new lines (default true)autoIndent
- set to true to make println() prepend the indent automatically (default false)
-
-
Method Detail
-
println
public void println(java.lang.String text)
Prints a string followed by an end of line character.- Parameters:
text
- String to be written
-
print
public void print(java.lang.String text)
Prints a string.- Parameters:
text
- String to be written
-
print
public void print(char c)
Prints a character.- Parameters:
c
- char to be written
-
printIndent
public void printIndent()
Prints the current indent level.
-
println
public void println()
Prints an end-of-line character (if enabled via addNewLines property). Defaults to outputting a single '\n' character but by using a custom Writer, e.g. PlatformLineWriter, you can get platform-specific end-of-line characters.- See Also:
IndentPrinter(Writer, String, boolean)
-
incrementIndent
public void incrementIndent()
-
decrementIndent
public void decrementIndent()
-
getIndentLevel
public int getIndentLevel()
-
setIndentLevel
public void setIndentLevel(int indentLevel)
-
getAutoIndent
public boolean getAutoIndent()
-
setAutoIndent
public void setAutoIndent(boolean autoIndent)
-
flush
public void flush()
-
-