Module: Wood::IndentedPrinter

Defined in:
lib/wood/indented_printer.rb

Overview

Mixin used for indented pretty-printing of code. Expects ‘#io` and `#indentation` methods to work.

Instance Method Summary collapse

Instance Method Details

#indentObject

Increases the current indentation by ‘indentation` spaces.



37
38
39
40
# File 'lib/wood/indented_printer.rb', line 37

def indent
  @__indent__ ||= 0
  @__indent__ += indentation
end

#newlineObject

Inserts a line break into ‘io` together with correct reindentation on the next line.



31
32
33
34
# File 'lib/wood/indented_printer.rb', line 31

def newline
  io << "\n"
  io << (" " * @__indent__.to_i)
end

Prints any arguments passed to ‘io`.

Parameters:

  • args (Array)

    List of objects to print to ‘io`.



17
18
19
20
21
# File 'lib/wood/indented_printer.rb', line 17

def print(*args)
  args.each do |a|
    io << a
  end
end

#println(str) ⇒ Object

Prints a String to ‘io` followed by a line break.

Parameters:

  • str (String)

    String to be printed to ‘io` followed by a newline.



25
26
27
# File 'lib/wood/indented_printer.rb', line 25

def println(str)
  print str, "\n"
end

#unindentObject

Decreases the current indentation by ‘indentation` spaces.



43
44
45
46
# File 'lib/wood/indented_printer.rb', line 43

def unindent
  @__indent__ ||= 0
  @__indent__ -= indentation
end

#with_indentationObject

Yields to a given block while indenting the output generated within the block.



7
8
9
10
11
12
13
# File 'lib/wood/indented_printer.rb', line 7

def with_indentation
  indent
  newline
  yield
  unindent
  newline
end