Class: Yadriggy::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/yadriggy/printer.rb

Overview

A helper class for pretty printing.

Direct Known Subclasses

FilePrinter

Instance Method Summary collapse

Constructor Details

#initialize(indent = 2) ⇒ Printer

Returns a new instance of Printer.

Parameters:

  • indent (Integer) (defaults to: 2)

    the indent size. The default value is 2.



9
10
11
12
13
14
# File 'lib/yadriggy/printer.rb', line 9

def initialize(indent=2)
  @text = ''
  @level = 0
  @linebreak = false
  @indent = ' ' * indent
end

Instance Method Details

#<<(code) ⇒ Object

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

Parameters:

  • code (String|:nil)

    the text.



46
47
48
49
50
51
52
53
54
# 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

#downObject

Increase the indentation level.



25
26
27
28
# File 'lib/yadriggy/printer.rb', line 25

def down
  @level += 1
  add_newline
end

#nlObject

Starts a new line.



39
40
41
# File 'lib/yadriggy/printer.rb', line 39

def nl
  @linebreak = true
end

#outputObject

Returns the output stream.



18
19
20
21
# File 'lib/yadriggy/printer.rb', line 18

def output()
  add_newline if @linebreak
  @text
end

#upObject

Decrease the indentation level.



32
33
34
35
# File 'lib/yadriggy/printer.rb', line 32

def up
  @level -= 1
  add_newline
end