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
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private boolean | |
| private final String | |
| private int | |
| private final Writer |
| Access | Constructor and Description |
|---|---|
| public | IndentPrinter()
Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces. |
| public | IndentPrinter(Writer
Writer to output to out)Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces. |
| public | IndentPrinter(Writer
Writer to output to out, String character(s) used to indent each line indent)Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting. |
| public | IndentPrinter(Writer
Writer to output to out, String character(s) used to indent each line indent, boolean set to false to gobble all new lines (default true) 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. |
| public | IndentPrinter(Writer
Writer to output to out, String character(s) used to indent each line indent, boolean set to false to gobble all new lines (default true) addNewlines, boolean set to true to make println() prepend the indent automatically (default false) autoIndent)Create an IndentPrinter to the given PrintWriter |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public boolean | |
| public int | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void |
| addNewlines | back to summary |
|---|---|
| private final boolean addNewlines | |
| autoIndent | back to summary |
|---|---|
| private boolean autoIndent | |
| indent | back to summary |
|---|---|
| private final String indent | |
| indentLevel | back to summary |
|---|---|
| private int indentLevel | |
| out | back to summary |
|---|---|
| private final Writer out | |
| IndentPrinter | back to summary |
|---|---|
| public IndentPrinter() Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces.
| |
| IndentPrinter | back to summary |
|---|---|
| public IndentPrinter(Writer out) Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces.
| |
| IndentPrinter | back to summary |
|---|---|
| public IndentPrinter(Writer out, String indent) Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting. | |
| IndentPrinter | back to summary |
|---|---|
| public IndentPrinter(Writer out, 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 | back to summary |
|---|---|
| public IndentPrinter(Writer out, String indent, boolean addNewlines, boolean autoIndent) Create an IndentPrinter to the given PrintWriter | |
| decrementIndent | back to summary |
|---|---|
| public void decrementIndent() | |
| flush | back to summary |
|---|---|
| public void flush() | |
| getAutoIndent | back to summary |
|---|---|
| public boolean getAutoIndent() | |
| getIndentLevel | back to summary |
|---|---|
| public int getIndentLevel() | |
| incrementIndent | back to summary |
|---|---|
| public void incrementIndent() | |
| back to summary | |
|---|---|
| public void print(String text) Prints a string.
| |
| back to summary | |
|---|---|
| public void print(char c) Prints a character.
| |
| printIndent | back to summary |
|---|---|
| public void printIndent() Prints the current indent level. | |
| println | back to summary |
|---|---|
| public void println(String text) Prints a string followed by an end of line character.
| |
| println | back to summary |
|---|---|
| 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. | |
| setAutoIndent | back to summary |
|---|---|
| public void setAutoIndent(boolean autoIndent) | |
| setIndentLevel | back to summary |
|---|---|
| public void setIndentLevel(int indentLevel) | |