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 Method Summary collapse

Instance Method Details

#call(row, options = {}) ⇒ Array[String]

Apply truncation to a row

Parameters:

  • row (Array)

    the table row

Returns:

  • (Array[String])


18
19
20
21
22
23
24
25
# File 'lib/tty/table/operation/truncation.rb', line 18

def call(row, options={})
  index = 0
  row.map! do |field|
    width = options.fetch(:column_widths, {})[index] || field.width
    index += 1
    field.value = truncate(field.value, width)
  end
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)


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

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