Class: Clin::Text
- Inherits:
-
Object
- Object
- Clin::Text
- 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
-
#_lines ⇒ Object
Returns the value of attribute _lines.
-
#global_indent ⇒ Object
All the lines added to this text will be indented with this.
-
#listeners ⇒ Object
List of block that listen for line added to the next builder.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#blank(times = 1) ⇒ Object
Add a blank line n times.
-
#broadcast(line) ⇒ Object
Call the the listener with the newly added line.
-
#indent(indent, &block) ⇒ Object
Indent all the content inside this block.
-
#initialize(indent: '', &block) ⇒ Text
constructor
A new instance of Text.
-
#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’ “‘.
-
#lines(array = [], indent: '') ⇒ Object
Add a list of string as lines or get the existing lines.
- #on(&block) ⇒ Object
-
#prefix(text, indent: '') ⇒ Object
Add a line at the beginning of the text.
- #process_line(text, indent: '') ⇒ Object
- #table(indent: '', **options, &block) ⇒ Object
-
#text(text, indent: '') ⇒ Object
Add the content of another Clin::Text object.
-
#to_s ⇒ String
Join the lines together to form the output.
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
#_lines ⇒ Object
Returns the value of attribute _lines.
35 36 37 |
# File 'lib/clin/text.rb', line 35 def _lines @_lines end |
#global_indent ⇒ Object
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 |
#listeners ⇒ Object
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
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.
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’ “‘
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.
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
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: '', **, &block) text Clin::Text::Table.new(**, &block).to_text, indent: indent end |
#text(text, indent: '') ⇒ Object
Add the content of another Clin::Text object
96 97 98 |
# File 'lib/clin/text.rb', line 96 def text(text, indent: '') lines(text._lines, indent: indent) end |
#to_s ⇒ String
Join the lines together to form the output
116 117 118 |
# File 'lib/clin/text.rb', line 116 def to_s "#{@_lines.join("\n")}\n" end |