Class: Parlour::Debugging::Tree

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/debugging.rb

Overview

A module for generating a globally-consistent, nicely-formatted tree of output using Unicode block characters.

Constant Summary collapse

INDENT_SPACES =

The number of spaces to indent each layer of the tree by. Should be at least 1.

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colour: false) ⇒ Tree

Returns a new instance of Tree.



78
79
80
81
# File 'lib/parlour/debugging.rb', line 78

def initialize(colour: false)
  @colour = colour
  @indent_level = 0
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



75
76
77
# File 'lib/parlour/debugging.rb', line 75

def colour
  @colour
end

Instance Method Details

#begin(message) ⇒ Object



88
89
90
91
92
93
# File 'lib/parlour/debugging.rb', line 88

def begin(message)
  result = line_prefix + '' + text_prefix +
    (colour ? Rainbow(message).green.bright.bold : message)
  indent!(1)
  result
end

#end(message) ⇒ Object



108
109
110
111
112
# File 'lib/parlour/debugging.rb', line 108

def end(message)
  result = line_prefix + '' + text_prefix + message
  indent!(-1)
  result
end

#here(message) ⇒ Object



99
100
101
# File 'lib/parlour/debugging.rb', line 99

def here(message)
  line_prefix + '' + text_prefix + message
end

#indent!(offset) ⇒ Object

Modifies the current indent level by the given offset.



130
131
132
# File 'lib/parlour/debugging.rb', line 130

def indent!(offset)
  @indent_level = [0, @indent_level + offset].max
end

#line_prefixString

The prefix which should be printed before anything else on this line of the tree, based on the current indent level.

Returns:

  • (String)


117
118
119
# File 'lib/parlour/debugging.rb', line 117

def line_prefix
  @indent_level.times.map { '' + ' ' * INDENT_SPACES }.join
end

#text_prefixString

The horizontal lines which should be printed between the beginning of the current element and its text, based on the specified number of spaces to use for indents.

Returns:

  • (String)


125
126
127
# File 'lib/parlour/debugging.rb', line 125

def text_prefix
  '' * (INDENT_SPACES - 1) + " "
end