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

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class responsible for shortening text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widths) ⇒ Truncation

Initialize a Truncation



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

def initialize(widths)
  @widths = widths
end

Instance Attribute Details

#widthsObject (readonly)

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.



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

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:



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

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)


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

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