Top Description Fields Constructors Methods
groovy.util

public Class IndentPrinter

extends Object
Class Inheritance
Imports
groovy.lang.GroovyRuntimeException, java.io.IOException, .PrintWriter, .Writer

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

Field Summary

Modifier and TypeField and Description
private final boolean
private boolean
private final String
private int
private final Writer

Constructor Summary

AccessConstructor 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

Method Summary

Modifier and TypeMethod and Description
public void
public void
public boolean
public int
public void
public void
printIndent()

Prints the current indent level.

public void
println(String
String to be written
text
)

Prints a string followed by an end of line character.

public void
println()

Prints an end-of-line character (if enabled via addNewLines property).

public void
setAutoIndent(boolean autoIndent)

public void
setIndentLevel(int indentLevel)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addNewlinesback to summary
private final boolean addNewlines
autoIndentback to summary
private boolean autoIndent
indentback to summary
private final String indent
indentLevelback to summary
private int indentLevel
outback to summary
private final Writer out

Constructor Detail

IndentPrinterback to summary
public IndentPrinter()

Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces.

See Also
IndentPrinter(Writer, String)
IndentPrinterback to summary
public IndentPrinter(Writer out)

Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces.

Parameters
out:Writer

Writer to output to

See Also
IndentPrinter(Writer, String)
IndentPrinterback 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.

Parameters
out:Writer

Writer to output to

indent:String

character(s) used to indent each line

IndentPrinterback 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.

Parameters
out:Writer

Writer to output to

indent:String

character(s) used to indent each line

addNewlines:boolean

set to false to gobble all new lines (default true)

IndentPrinterback to summary
public IndentPrinter(Writer out, String indent, boolean addNewlines, boolean autoIndent)

Create an IndentPrinter to the given PrintWriter

Parameters
out:Writer

Writer to output to

indent:String

character(s) used to indent each line

addNewlines:boolean

set to false to gobble all new lines (default true)

autoIndent:boolean

set to true to make println() prepend the indent automatically (default false)

Method Detail

decrementIndentback to summary
public void decrementIndent()
flushback to summary
public void flush()
getAutoIndentback to summary
public boolean getAutoIndent()
getIndentLevelback to summary
public int getIndentLevel()
incrementIndentback to summary
public void incrementIndent()
printIndentback to summary
public void printIndent()

Prints the current indent level.

printlnback to summary
public void println(String text)

Prints a string followed by an end of line character.

Parameters
text:String

String to be written

printlnback 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.

See Also
IndentPrinter(Writer, String, boolean)
setAutoIndentback to summary
public void setAutoIndent(boolean autoIndent)
setIndentLevelback to summary
public void setIndentLevel(int indentLevel)