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

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

Overview

A class responsible for wrapping text.

Instance Method Summary collapse

Methods included from Unicode

#as_unicode, #clean_utf8, #utf8?

Instance Method Details

#wrap(string, width) ⇒ Object

Wrap a long string according to the width.

Parameters:

  • string (String)

    the string to wrap

  • width (Integer)

    the maximum width

  • addnewline (Boolean)

    add new line add the end



21
22
23
24
25
26
27
28
# File 'lib/tty/table/operation/wrapped.rb', line 21

def wrap(string, width)
  as_unicode do
    chars = string.chars.to_a
    return string if chars.length <= width
    idx = width
    return chars[0, idx].join + "\n" + wrap(chars[idx..-1].join, width)
  end
end