class Yadriggy::Printer

A helper class for pretty printing.

Public Class Methods

new(indent=2) click to toggle source

@param [Integer] indent the indent size. The default value is 2.

# File lib/yadriggy/printer.rb, line 9
def initialize(indent=2)
  @text = ''
  @level = 0
  @linebreak = false
  @indent = ' ' * indent
end

Public Instance Methods

<<(code) click to toggle source

Prints the text. If `code` is `:nl`, a line break is printed.

@param [String|:nil] code the text.

# File lib/yadriggy/printer.rb, line 46
def << (code)
  add_newline if @linebreak
  if code == :nl
    @linebreak = true
  else
    @text << code.to_s
  end
  self
end
down() click to toggle source

Increase the indentation level.

# File lib/yadriggy/printer.rb, line 25
def down
  @level += 1
  add_newline
end
nl() click to toggle source

Starts a new line.

# File lib/yadriggy/printer.rb, line 39
def nl
  @linebreak = true
end
output() click to toggle source

Returns the output stream.

# File lib/yadriggy/printer.rb, line 18
def output()
  add_newline if @linebreak
  @text
end
up() click to toggle source

Decrease the indentation level.

# File lib/yadriggy/printer.rb, line 32
def up
  @level -= 1
  add_newline
end

Private Instance Methods

add_newline() click to toggle source
# File lib/yadriggy/printer.rb, line 58
def add_newline()
  @text << "\n"
  @level.times { @text << @indent }
  @linebreak = false
end