Class: Spoom::Printer

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/spoom/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#set_color

Constructor Details

#initialize(out: $stdout, colors: true, indent_level: 0) ⇒ Printer

: (?out: (IO | StringIO), ?colors: bool, ?indent_level: Integer) -> void



14
15
16
17
18
# File 'lib/spoom/printer.rb', line 14

def initialize(out: $stdout, colors: true, indent_level: 0)
  @out = out
  @colors = colors
  @indent_level = indent_level
end

Instance Attribute Details

#outObject

: (IO | StringIO)



11
12
13
# File 'lib/spoom/printer.rb', line 11

def out
  @out
end

Instance Method Details

#colorize(string, *color) ⇒ Object

Colorize ‘string` with color if `@colors` : (String string, *Spoom::Color color) -> String



75
76
77
78
79
# File 'lib/spoom/printer.rb', line 75

def colorize(string, *color)
  return string unless @colors

  T.unsafe(self).set_color(string, *color)
end

#dedentObject

Decrease indent level : -> void



28
29
30
# File 'lib/spoom/printer.rb', line 28

def dedent
  @indent_level -= 2
end

#indentObject

Increase indent level : -> void



22
23
24
# File 'lib/spoom/printer.rb', line 22

def indent
  @indent_level += 2
end

Print ‘string` into `out` : (String? string) -> void



34
35
36
37
38
# File 'lib/spoom/printer.rb', line 34

def print(string)
  return unless string

  @out.print(string)
end

Print ‘string` colored with `color` into `out`

Does not use colors unless ‘@colors`. : (String? string, *Color color) -> void



44
45
46
47
48
49
# File 'lib/spoom/printer.rb', line 44

def print_colored(string, *color)
  return unless string

  string = T.unsafe(self).colorize(string, *color)
  @out.print(string)
end

#printl(string) ⇒ Object

Print ‘string` with indent and newline : (String? string) -> void



59
60
61
62
63
64
65
# File 'lib/spoom/printer.rb', line 59

def printl(string)
  return unless string

  printt
  print(string)
  printn
end

#printnObject

Print a new line into ‘out` : -> void



53
54
55
# File 'lib/spoom/printer.rb', line 53

def printn
  print("\n")
end

#printtObject

Print an indent space into ‘out` : -> void



69
70
71
# File 'lib/spoom/printer.rb', line 69

def printt
  print(" " * @indent_level)
end