Class: TTY::Text::Wrapping

Inherits:
Object
  • Object
show all
Includes:
Unicode
Defined in:
lib/tty/text/wrapping.rb

Overview

A class responsible for text wrapping operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Unicode

#as_unicode, #clean_utf8, #utf8?

Constructor Details

#new(text, value) ⇒ Wrapping #new(text, value, options) ⇒ Wrapping

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.

Initialize a Wrapping

Overloads:

  • #new(text, value) ⇒ Wrapping

    wraps text at given value

    Parameters:

    • value (Integer)
  • #new(text, value, options) ⇒ Wrapping

    Parameters:

    • value (Integer)
    • options (Hash)

    Options Hash (options):

    • :indent (Symbol)

      the indentation

    • :length (Symbol)

      the desired length

Parameters:

  • text (String)

    the text to be wrapped



33
34
35
36
37
38
39
# File 'lib/tty/text/wrapping.rb', line 33

def initialize(text, *args)
  options = Utils.extract_options!(args)
  @text   = text
  @length = options.fetch(:length) { DEFAULT_WIDTH }
  @indent = options.fetch(:indent) { 0 }
  @length = args[0] unless args.empty?
end

Instance Attribute Details

#indentObject (readonly)

Returns the value of attribute indent.



14
15
16
# File 'lib/tty/text/wrapping.rb', line 14

def indent
  @indent
end

#lengthObject (readonly)

Returns the value of attribute length.



12
13
14
# File 'lib/tty/text/wrapping.rb', line 12

def length
  @length
end

#textObject (readonly)

Returns the value of attribute text.



10
11
12
# File 'lib/tty/text/wrapping.rb', line 10

def text
  @text
end

Instance Method Details

#wrapObject

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.

Wrap a text into lines no longer than length

See Also:

  • TTY::Text#wrap


46
47
48
49
50
51
52
53
54
55
# File 'lib/tty/text/wrapping.rb', line 46

def wrap
  return text unless length && length > 0

  as_unicode do
    text.split(NEWLINE).map do |line|
      modified_line = wrap_line line
      indent_line modified_line
    end * NEWLINE
  end
end