Class: TTY::Table::Operation::Padding
- Inherits:
-
Object
- Object
- TTY::Table::Operation::Padding
- Defined in:
- lib/tty/table/operation/padding.rb
Overview
A class responsible for padding field with whitespace
Instance Attribute Summary collapse
-
#multiline ⇒ Object
readonly
Returns the value of attribute multiline.
-
#padding_bottom ⇒ Object
readonly
Returns the value of attribute padding_bottom.
-
#padding_left ⇒ Object
readonly
Returns the value of attribute padding_left.
-
#padding_right ⇒ Object
readonly
Returns the value of attribute padding_right.
-
#padding_top ⇒ Object
readonly
Returns the value of attribute padding_top.
-
#padding_width ⇒ Object
readonly
Returns the value of attribute padding_width.
Instance Method Summary collapse
-
#call(field, row, col) ⇒ TTY::Table::Field
Apply padding to a field.
-
#initialize(padding, multiline) ⇒ Padding
constructor
Initialize a Padding operation.
-
#pad_around(text) ⇒ String
private
Apply padding to left and right side of string.
-
#pad_multi_line(text) ⇒ String
private
Apply padding to multi line text.
-
#pad_single_line(text) ⇒ String
private
Apply padding to single line text.
Constructor Details
#initialize(padding, multiline) ⇒ Padding
Initialize a Padding operation
27 28 29 30 31 32 33 34 |
# File 'lib/tty/table/operation/padding.rb', line 27 def initialize(padding, multiline) @padding_top = "\n" * padding.top @padding_right = ' ' * padding.right @padding_bottom = "\n" * padding.bottom @padding_left = ' ' * padding.left @padding_width = padding.left + padding.right @multiline = multiline end |
Instance Attribute Details
#multiline ⇒ Object (readonly)
Returns the value of attribute multiline.
20 21 22 |
# File 'lib/tty/table/operation/padding.rb', line 20 def multiline @multiline end |
#padding_bottom ⇒ Object (readonly)
Returns the value of attribute padding_bottom.
14 15 16 |
# File 'lib/tty/table/operation/padding.rb', line 14 def padding_bottom @padding_bottom end |
#padding_left ⇒ Object (readonly)
Returns the value of attribute padding_left.
16 17 18 |
# File 'lib/tty/table/operation/padding.rb', line 16 def padding_left @padding_left end |
#padding_right ⇒ Object (readonly)
Returns the value of attribute padding_right.
12 13 14 |
# File 'lib/tty/table/operation/padding.rb', line 12 def padding_right @padding_right end |
#padding_top ⇒ Object (readonly)
Returns the value of attribute padding_top.
10 11 12 |
# File 'lib/tty/table/operation/padding.rb', line 10 def padding_top @padding_top end |
#padding_width ⇒ Object (readonly)
Returns the value of attribute padding_width.
18 19 20 |
# File 'lib/tty/table/operation/padding.rb', line 18 def padding_width @padding_width end |
Instance Method Details
#call(field, row, col) ⇒ TTY::Table::Field
Apply padding to a field
50 51 52 53 54 55 56 57 |
# File 'lib/tty/table/operation/padding.rb', line 50 def call(field, row, col) text = field.value.to_s text = multiline ? pad_multi_line(text) : pad_single_line(text) text.insert(0, padding_top).insert(-1, padding_bottom) field.value = text end |
#pad_around(text) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply padding to left and right side of string
88 89 90 |
# File 'lib/tty/table/operation/padding.rb', line 88 def pad_around(text) text.insert(0, padding_left).insert(-1, padding_right) end |
#pad_multi_line(text) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply padding to multi line text
66 67 68 |
# File 'lib/tty/table/operation/padding.rb', line 66 def pad_multi_line(text) text.split("\n", -1).map { |part| pad_around(part) }.join("\n") end |
#pad_single_line(text) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply padding to single line text
77 78 79 |
# File 'lib/tty/table/operation/padding.rb', line 77 def pad_single_line(text) pad_around(text.strip) end |