Class: TTY::Table::Operation::Truncation

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

Overview

A class responsible for shortening text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widths) ⇒ Truncation

Initialize a Truncation



15
16
17
# File 'lib/tty/table/operation/truncation.rb', line 15

def initialize(widths)
  @widths = widths
end

Instance Attribute Details

#widthsObject (readonly)

Returns the value of attribute widths.



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

def widths
  @widths
end

Instance Method Details

#call(field, row, col) ⇒ TTY::Table::Field

Apply truncation to a field

Parameters:

  • field (TTY::Table::Field)

    the table field

  • row (Integer)

    the field row index

  • col (Integer)

    the field column index

Returns:



33
34
35
36
# File 'lib/tty/table/operation/truncation.rb', line 33

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

#truncate(string, width) ⇒ String

Shorten given string with traling character.

Parameters:

  • string (String)

    the string to truncate

  • width (Integer)

    the maximum width

Returns:

  • (String)


48
49
50
# File 'lib/tty/table/operation/truncation.rb', line 48

def truncate(string, width)
  TTY::Text.truncate(string, width)
end