Package: groovy.util

## [Java] Class IndentPrinter

- groovy.util.IndentPrinter

-     public class IndentPrinter
      extends 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 ->
           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

- - ### Constructor Summary

    Constructors
    Constructor and description
    `IndentPrinter()` Creates an IndentPrinter backed by a PrintWriter pointing to System.out, with an indent of two spaces.
    `IndentPrinter(Writer out)` Creates an IndentPrinter backed by the supplied Writer, with an indent of two spaces.
    `IndentPrinter(Writer out, String indent)` Creates an IndentPrinter backed by the supplied Writer, with a user-supplied String to be used for indenting.
    `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(Writer out, String indent, boolean addNewlines, boolean autoIndent)` Create an IndentPrinter to the given PrintWriter

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `decrementIndent()` Decreases the indentation level by one.
    `` | `public void` | `flush()` Flushes the underlying writer.
    `` | `public boolean` | `getAutoIndent()` Indicates whether println(String) should emit indentation automatically.
    `` | `public int` | `getIndentLevel()` Returns the current indentation level.
    `` | `public void` | `incrementIndent()` Increases the indentation level by one.
    `` | `public void` | `print(String text)` Prints a string.
    `` | `public void` | `print(char c)` Prints a character.
    `` | `public void` | `printIndent()` Prints the current indent level.
    `` | `public void` | `println(String 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)` Enables or disables automatic indentation for println(String).
    `` | `public void` | `setIndentLevel(int indentLevel)` Sets the indentation level.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public IndentPrinter()

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

      See Also:
        IndentPrinter(Writer, String)

    - #### public IndentPrinter(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)

    - #### 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 to output to
        `indent` - character(s) used to indent each line

    - #### 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 to output to
        `indent` - character(s) used to indent each line
        `addNewlines` - set to false to gobble all new lines (default true)

    - #### public IndentPrinter(Writer out, String indent, boolean addNewlines, boolean autoIndent)

      Create an IndentPrinter to the given PrintWriter

      Parameters:
        `out` - Writer to output to
        `indent` - character(s) used to indent each line
        `addNewlines` - 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

    - #### public void decrementIndent()

      Decreases the indentation level by one.

    - #### public void flush()

      Flushes the underlying writer.

    - #### public boolean getAutoIndent()

      Indicates whether println(String) should emit indentation automatically.

      Returns:
        `true` if automatic indentation is enabled

    - #### public int getIndentLevel()

      Returns the current indentation level.

      Returns:
        the indentation level

    - #### public void incrementIndent()

      Increases the indentation level by one.

    - #### public void print(String text)

      Prints a string.

      Parameters:
        `text` - String to be written

    - #### public void print(char c)

      Prints a character.

      Parameters:
        `c` - char to be written

    - #### public void printIndent()

      Prints the current indent level.

    - #### public void println(String text)

      Prints a string followed by an end of line character.

      Parameters:
        `text` - String to be written

    - #### 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)

    - #### public void setAutoIndent(boolean autoIndent)

      Enables or disables automatic indentation for println(String).

      Parameters:
        `autoIndent` - `true` to enable automatic indentation

    - #### public void setIndentLevel(int indentLevel)

      Sets the indentation level.

      Parameters:
        `indentLevel` - the new indentation level