Class: FTT::Block
- Inherits:
-
Object
- Object
- FTT::Block
- Defined in:
- lib/fancy_terminal_text/block.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
- #border(chars = FTT::Borders::HASH) ⇒ Object
-
#initialize(*lines) ⇒ Block
constructor
Create a block from one or more text objects.
- #pad(top = 1, left = 1, right = 1, bottom = 1) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*lines) ⇒ Block
Create a block from one or more text objects. Each text object will become a line in the block.
9 10 11 |
# File 'lib/fancy_terminal_text/block.rb', line 9 def initialize(*lines) @lines = normalised_lines(lines) end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
3 4 5 |
# File 'lib/fancy_terminal_text/block.rb', line 3 def lines @lines end |
Instance Method Details
#border(chars = FTT::Borders::HASH) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fancy_terminal_text/block.rb', line 30 def border(chars = FTT::Borders::HASH) top_border = '' (lines.first.visual_length + 2).times do top_border << chars.sample end middle = lines.map do |c| FTT::Text.from_parts(FTT::Text.new(chars.sample), c, FTT::Text.new(chars.sample)) end bottom_border = '' (lines.first.visual_length + 2).times do bottom_border << chars.sample end Block.new(FTT::Text.new(top_border), *middle, FTT::Text.new(bottom_border)) end |
#pad(top = 1, left = 1, right = 1, bottom = 1) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/fancy_terminal_text/block.rb', line 17 def pad(top = 1, left = 1, right = 1, bottom = 1) top_padding = Array.new(top) { FTT::Text.new('') } bottom_padding = Array.new(bottom) { FTT::Text.new('') } padded_top_and_bottom = self.class.new(*top_padding, *@lines, *bottom_padding) padded_all = padded_top_and_bottom.lines.map do |line| l = line.left_pad(line.visual_length + left) l.right_pad(l.visual_length + right) end FTT::Block.new(*padded_all) end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/fancy_terminal_text/block.rb', line 13 def to_s @lines.collect(&:to_s).join("\n") end |