Class: Clin::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/clin/text.rb,
lib/clin/text/table.rb

Overview

Table text builder

Defined Under Namespace

Classes: Table, TableCell, TableRow, TableSeparatorRow

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent: '', &block) ⇒ Text

Returns a new instance of Text.



43
44
45
46
47
48
49
# File 'lib/clin/text.rb', line 43

def initialize(indent: '', &block)
  @_lines = []
  @inital_indent = compute_indent(indent)
  @global_indent = @inital_indent
  @listeners = []
  block.call(self) if block_given?
end

Instance Attribute Details

#_linesObject

Returns the value of attribute _lines.



35
36
37
# File 'lib/clin/text.rb', line 35

def _lines
  @_lines
end

#global_indentObject

All the lines added to this text will be indented with this.



38
39
40
# File 'lib/clin/text.rb', line 38

def global_indent
  @global_indent
end

#listenersObject

List of block that listen for line added to the next builder.



41
42
43
# File 'lib/clin/text.rb', line 41

def listeners
  @listeners
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



136
137
138
139
140
# File 'lib/clin/text.rb', line 136

def ==(other)
  return to_s == other if other.is_a? String
  return false unless other.is_a? Clin::Text
  @_lines == other._lines && @global_indent == other.global_indent
end

#blank(times = 1) ⇒ Object

Add a blank line n times

Parameters:

  • times (Integer) (defaults to: 1)

    Number of times to add the line.



77
78
79
80
# File 'lib/clin/text.rb', line 77

def blank(times = 1)
  @_lines += [''] * times
  times.times.each { broadcast('') }
end

#broadcast(line) ⇒ Object

Call the the listener with the newly added line



130
131
132
133
134
# File 'lib/clin/text.rb', line 130

def broadcast(line)
  @listeners.each do |block|
    block.call(line)
  end
end

#indent(indent, &block) ⇒ Object

Indent all the content inside this block.

Parameters:

  • indent (String|Integer)

    indent value

  • block (Proc)

    Callback.



103
104
105
106
107
108
# File 'lib/clin/text.rb', line 103

def indent(indent, &block)
  previous_indent = @global_indent
  @global_indent += compute_indent(indent)
  block.call(self)
  @global_indent = previous_indent
end

#line(text, indent: '') ⇒ Object

Add a new line “‘ line(’Some line’) #=> ‘Some line’ line(‘Some line’, indent: 3) #=> ‘ Some line’ line(‘Some line’, indent: ‘- ’) #=> ‘- Some line’ “‘

Parameters:

  • text (String)

    line to add

  • indent (String|Integer) (defaults to: '')

    Indent the line with x spaces or the given text



59
60
61
62
63
64
# File 'lib/clin/text.rb', line 59

def line(text, indent: '')
  l = process_line(text, indent: indent)
  @_lines << l
  broadcast(l)
  l
end

#lines(array = [], indent: '') ⇒ Object

Add a list of string as lines or get the existing lines.

Parameters:

  • array (Array<String>) (defaults to: [])

    List of lines to add.

  • indent (String|Integer) (defaults to: '')

    Indent each line. @see #line

Returns:

  • list of lines



86
87
88
89
90
91
# File 'lib/clin/text.rb', line 86

def lines(array = [], indent: '')
  array.each do |l|
    line(l, indent: indent)
  end
  @_lines
end

#on(&block) ⇒ Object



125
126
127
# File 'lib/clin/text.rb', line 125

def on(&block)
  @listeners << block
end

#prefix(text, indent: '') ⇒ Object

Add a line at the beginning of the text

Parameters:

  • text (String)

    line to add

  • indent (String|Integer) (defaults to: '')

    Indent the line with x spaces or the given text



69
70
71
72
73
# File 'lib/clin/text.rb', line 69

def prefix(text, indent: '')
  l = process_line(text, indent: indent)
  @_lines.unshift l
  l
end

#process_line(text, indent: '') ⇒ Object



120
121
122
123
# File 'lib/clin/text.rb', line 120

def process_line(text, indent: '')
  indent = compute_indent(indent)
  "#{global_indent}#{indent}#{text}"
end

#table(indent: '', **options, &block) ⇒ Object



110
111
112
# File 'lib/clin/text.rb', line 110

def table(indent: '', **options, &block)
  text Clin::Text::Table.new(**options, &block).to_text, indent: indent
end

#text(text, indent: '') ⇒ Object

Add the content of another Clin::Text object

Parameters:

  • text (Clin::Text)
  • indent (String|Integer) (defaults to: '')

    Indent the content



96
97
98
# File 'lib/clin/text.rb', line 96

def text(text, indent: '')
  lines(text._lines, indent: indent)
end

#to_sString

Join the lines together to form the output

Returns:

  • (String)


116
117
118
# File 'lib/clin/text.rb', line 116

def to_s
  "#{@_lines.join("\n")}\n"
end