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



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

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.



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

def indent
  @indent
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#paddingObject (readonly)

Returns the value of attribute padding.



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

def padding
  @padding
end

#textObject (readonly)

Returns the value of attribute text.



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

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


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

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