Parent

Class/Module Index [+]

Quicksearch

String

Public Instance Methods

strip_ansi() click to toggle source
# File lib/rhc/core_ext.rb, line 132
def strip_ansi
  gsub(ANSI_ESCAPE_SEQUENCE, '')
end
strip_heredoc() click to toggle source
# File lib/rhc/core_ext.rb, line 57
def strip_heredoc
  indent = scan(/^[ \t]*(?=\S)/).min.size || 0
  gsub(/^[ \t]{#{indent}}/, '').
    gsub(/(\b|\S)[^\S\n]*\n(\S)/, '\1 \2').
    gsub(/\n+\Z/, '').
    gsub(/\n{3,}/, "\n\n")
end
textwrap_ansi(limit, breakword=true) click to toggle source

Split the given string at limit, treating ANSI escape sequences as zero characters in length. Will insert an ANSI reset code (e[0m) at the end of each line containing an ANSI code, assuming that a reset was not in the wrapped segment.

All newlines are preserved.

Lines longer than limit without natural breaks will be forcibly split at the exact limit boundary.

Returns an Array

# File lib/rhc/core_ext.rb, line 82
def textwrap_ansi(limit, breakword=true)
  re = breakword ? /
    ( 
      # Match substrings that end in whitespace shorter than limit
      #{CHAR_SKIP_ANSI}{1,#{limit}} # up to limit
      (?:\s+|$)                     # require the limit to end on whitespace
      |
      # Match substrings equal to the limit
      #{CHAR_SKIP_ANSI}{1,#{limit}} 
    )
    / :
    /
    ( 
      # Match substrings that end in whitespace shorter than limit
      #{CHAR_SKIP_ANSI}{1,#{limit}}
      (?:\s|$)                     # require the limit to end on whitespace
      |
      # Match all continguous whitespace strings
      #{CHAR_SKIP_ANSI}+?
      (?:\s|$)
      (?:\s+|$)?
    )
    /
  escapes = []

  split("\n",-1).inject([]) do |a, line|
    if line.length < limit
      a << line 
    else
      line.scan(re) do |segment, other|
        if escapes.present? 
          a << escapes.map{ |s| "\e[#{s}"}.join
          a[-1] << segment.rstrip   
        else
          a << segment.rstrip
        end

        segment.scan(ANSI_ESCAPE_SEQUENCE).map{ |e| e.first }.each do |e|
          case e
          when '0m' then escapes.clear
          else escapes << e
          end
        end
        a[-1] << "\e[0m" if escapes.present?
      end
    end
    a
  end
end
wrap(wrap_length=80, char="\n") click to toggle source

Wrap string by the given length, and join it with the given character. The method doesn’t distinguish between words, it will only work based on the length.

# File lib/rhc/core_ext.rb, line 53
def wrap(wrap_length=80, char="\n")
  scan(/.{#{wrap_length}}|.+/).join(char)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.