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



35
36
37
38
39
40
41
42
# File 'lib/tty/text/wrapping.rb', line 35

def initialize(text, *args)
  options  = Utils.extract_options!(args)
  @text    = text
  @length  = options.fetch(:length) { DEFAULT_WIDTH }
  @indent  = options.fetch(:indent) { 0 }
  @padding = options.fetch(:padding) { [] }
  @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

#paddingObject (readonly)

Returns the value of attribute padding.



16
17
18
# File 'lib/tty/text/wrapping.rb', line 16

def padding
  @padding
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


49
50
51
52
53
54
55
56
57
# File 'lib/tty/text/wrapping.rb', line 49

def wrap
  return text unless length && length > 0

  as_unicode do
    text.split(NEWLINE, -1).map do |line|
      pad_line(indent_line(wrap_line(line)))
    end * NEWLINE
  end
end