Module: Strings::Wrap

Defined in:
lib/strings/wrap.rb

Constant Summary collapse

DEFAULT_WIDTH =
80
NEWLINE =
"\n"
SPACE =
" "
LINE_BREAK =
%r{\r\n|\r|\n}.freeze
LINE_BREAKS =
"\r\n+|\r+|\n+"

Class Method Summary collapse

Class Method Details

.display_width(string) ⇒ Object

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.

Visible width of a string



170
171
172
# File 'lib/strings/wrap.rb', line 170

def display_width(string)
  Unicode::DisplayWidth.of(Strings::ANSI.sanitize(string))
end

.format_line(text_line, wrap_at, ansi_stack) ⇒ Array[String]

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.

Format line to be maximum of wrap_at length

Parameters:

  • text_line (String)

    the line to format

  • wrap_at (Integer)

    the maximum length to wrap the line

Returns:

  • (Array[String])

    the wrapped lines



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/strings/wrap.rb', line 46

def format_line(text_line, wrap_at, ansi_stack)
  lines = []
  line  = []
  word  = []
  ansi  = []
  ansi_matched = false
  word_length = 0
  line_length = 0
  char_length = 0 # visible char length
  text_length = display_width(text_line)
  total_length = 0

  UnicodeUtils.each_grapheme(text_line) do |char|
    # we found ansi let's consume
    if char == Strings::ANSI::CSI || ansi.length > 0
      ansi << char
      if Strings::ANSI.only_ansi?(ansi.join)
        ansi_matched = true
      elsif ansi_matched
        ansi_stack << [ansi[0...-1].join, line_length + word_length]
        ansi_matched = false

        if ansi.last == Strings::ANSI::CSI
          ansi = [ansi.last]
        else
          ansi = []
        end
      end
      next if ansi.length > 0
    end

    char_length = display_width(char)
    total_length += char_length
    if line_length + word_length + char_length <= wrap_at
      if char == SPACE || total_length == text_length
        line << word.join + char
        line_length += word_length + char_length
        word = []
        word_length = 0
      else
        word << char
        word_length += char_length
      end
      next
    end

    if char == SPACE # ends with space
      lines << insert_ansi(line.join, ansi_stack)
      line = []
      line_length = 0
      word << char
      word_length += char_length
    elsif word_length + char_length <= wrap_at
      lines << insert_ansi(line.join, ansi_stack)
      line = [word.join + char]
      line_length = word_length + char_length
      word = []
      word_length = 0
    else # hyphenate word - too long to fit a line
      lines << insert_ansi(word.join, ansi_stack)
      line_length = 0
      word = [char]
      word_length = char_length
    end
  end
  lines << insert_ansi(line.join, ansi_stack) unless line.empty?
  lines << insert_ansi(word.join, ansi_stack) unless word.empty?
  lines
end

.insert_ansi(string, ansi_stack = []) ⇒ String

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.

Insert ANSI code into string

Check if there are any ANSI states, if present insert ANSI codes at given positions unwinding the stack.

Parameters:

  • string (String)

    the string to insert ANSI codes into

  • ansi_stack (Array[Array[String, Integer]]) (defaults to: [])

    the ANSI codes to apply

Returns:

  • (String)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/strings/wrap.rb', line 131

def insert_ansi(string, ansi_stack = [])
  return string if ansi_stack.empty?
  return string if string.empty?

  new_stack = []
  output          = string.dup
  length          = string.size
  matched_reset   = false
  ansi_reset      = Strings::ANSI::RESET

  # Reversed so that string index don't count ansi
  ansi_stack.reverse_each do |ansi|
    if ansi[0] =~ /#{Regexp.quote(ansi_reset)}/
      matched_reset = true
      output.insert(ansi[1], ansi_reset)
      next
    elsif !matched_reset # ansi without reset
      matched_reset = false
      new_stack << ansi # keep the ansi
      next if ansi[1] == length
      if output.end_with?(NEWLINE)
        output.insert(-2, ansi_reset)
      else
        output.insert(-1, ansi_reset) # add reset at the end
      end
    end

    output.insert(ansi[1], ansi[0])
  end

  ansi_stack.replace(new_stack)

  output
end

.wrap(text, wrap_at = DEFAULT_WIDTH, separator: NEWLINE) ⇒ Object

Wrap a text into lines no longer than wrap_at length. Preserves existing lines and existing word boundaries.

Examples:

Strings::Wrap.wrap("Some longish text", 8)
# => "Some \nlongish \ntext"


23
24
25
26
27
28
29
30
31
32
# File 'lib/strings/wrap.rb', line 23

def wrap(text, wrap_at = DEFAULT_WIDTH, separator: NEWLINE)
  if text.scan(/[[:print:]]/).length < wrap_at.to_i || wrap_at.to_i.zero?
    return text
  end

  ansi_stack = []
  text.lines.map do |line|
    format_line(line, wrap_at, ansi_stack).join(separator)
  end.join
end