Class: TTY::Table::Operation::Padding

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/table/operation/padding.rb

Overview

A class responsible for padding field with whitespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(padding, multiline) ⇒ Padding

Initialize a Padding operation

Parameters:



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

#multilineObject (readonly)

Returns the value of attribute multiline.



20
21
22
# File 'lib/tty/table/operation/padding.rb', line 20

def multiline
  @multiline
end

#padding_bottomObject (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_leftObject (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_rightObject (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_topObject (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_widthObject (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

Parameters:

  • field (TTY::Table::Field)

    the table field

  • row (Integer)

    the field row index

  • col (Integer)

    the field column index

Returns:



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

Parameters:

  • text (String)

Returns:

  • (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

Parameters:

  • text (String)

Returns:

  • (String)


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

Parameters:

  • text (String)

Returns:

  • (String)


77
78
79
# File 'lib/tty/table/operation/padding.rb', line 77

def pad_single_line(text)
  pad_around(text.strip)
end