Class: TTY::Table::Operation::Wrapped

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

Overview

A class responsible for wrapping text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widths, padding) ⇒ Wrapped

Initialize a Wrapped



17
18
19
20
# File 'lib/tty/table/operation/wrapped.rb', line 17

def initialize(widths, padding)
  @widths  = widths
  @padding = padding.padding
end

Instance Attribute Details

#paddingObject (readonly)

Returns the value of attribute padding.



12
13
14
# File 'lib/tty/table/operation/wrapped.rb', line 12

def padding
  @padding
end

#widthsObject (readonly)

Returns the value of attribute widths.



10
11
12
# File 'lib/tty/table/operation/wrapped.rb', line 10

def widths
  @widths
end

Instance Method Details

#call(field, row, col) ⇒ Array[String]

Apply wrapping to a field

Parameters:

  • field (TTY::Table::Field)

    the table field

  • row (Integer)

    the field row index

  • col (Integer)

    the field column index

Returns:

  • (Array[String])


36
37
38
39
# File 'lib/tty/table/operation/wrapped.rb', line 36

def call(field, row, col)
  width = widths[col] || field.width
  field.value = wrap(field.value, width)
end

#wrap(string, width) ⇒ String

Wrap a long string according to the width.

Parameters:

  • string (String)

    the string to wrap

  • width (Integer)

    the maximum width

Returns:

  • (String)


51
52
53
# File 'lib/tty/table/operation/wrapped.rb', line 51

def wrap(string, width)
  TTY::Text.wrap(string, width, padding: padding)
end