class String

Template coloring

String helpers

Public Instance Methods

good?() click to toggle source

Tests if object is nil or empty

@return [Boolean] true if object is defined and has content

# File lib/doing/good.rb, line 46
def good?
  !strip.empty?
end
highlight_errors() click to toggle source
# File lib/helpers/threaded_tests_string.rb, line 5
def highlight_errors
  cols = `tput cols`.strip.to_i

  string = dup

  errs = string.scan(/(?<==\n)(?:Failure|Error):.*?(?=\n=+)/m)

  errs.map! do |error|
    err = error.dup

    err.gsub!(%r{^(/.*?/)([^/:]+):(\d+):in (.*?)$}) do
      m = Regexp.last_match
      "#{m[1].white}#{m[2].bold.white}:#{m[3].yellow}:in #{m[4].cyan}"
    end
    err.gsub!(/(Failure|Error): (.*?)\((.*?)\):\n  (.*?)(?=\n)/m) do
      m = Regexp.last_match
      [
        m[1].bold.boldbgred.white,
        m[3].bold.boldbgcyan.white,
        m[2].bold.boldbgyellow.black,
        " #{m[4]} ".bold.boldbgwhite.black.reset
      ].join(':'.boldblack.boldbgblack.reset)
    end
    err.gsub!(/(<.*?>) (was expected to) (.*?)\n( *<.*?>)./m) do
      m = Regexp.last_match
      "#{m[1].bold.green} #{m[2].white} #{m[3].boldwhite.boldbgred.reset}\n#{m[4].bold.white}"
    end
    err.gsub!(/(Finished in) ([\d.]+) (seconds)/) do
      m = Regexp.last_match
      "#{m[1].green} #{m[2].bold.white} #{m[3].green}"
    end
    err.gsub!(/(\d+) (failures)/) do
      m = Regexp.last_match
      "#{m[1].bold.red} #{m[2].red}"
    end
    err.gsub!(/100% passed/) do |m|
      m.bold.green
    end

    err
  end

  errs.join("\n#{('=' * cols).blue}\n")
end
last_color_code() click to toggle source

Get the calculated ANSI color at the end of the string

@return ANSI escape sequence to match color

# File lib/doing/colors.rb, line 135
def last_color_code
  m = scan(ESCAPE_REGEX)

  em = ['0']
  fg = nil
  bg = nil
  rgbf = nil
  rgbb = nil

  m.each do |c|
    case c
    when '0'
      em = ['0']
      fg, bg, rgbf, rgbb = nil
    when /^[34]8/
      case c
      when /^3/
        fg = nil
        rgbf = c
      when /^4/
        bg = nil
        rgbb = c
      end
    else
      c.split(/;/).each do |i|
        x = i.to_i
        if x <= 9
          em << x
        elsif x >= 30 && x <= 39
          rgbf = nil
          fg = x
        elsif x >= 40 && x <= 49
          rgbb = nil
          bg = x
        elsif x >= 90 && x <= 97
          rgbf = nil
          fg = x
        elsif x >= 100 && x <= 107
          rgbb = nil
          bg = x
        end
      end
    end
  end

  escape = "\e[#{em.join(';')}m"
  escape += "\e[#{rgbb}m" if rgbb
  escape += "\e[#{rgbf}m" if rgbf
  escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m"
end
normalize_color() click to toggle source

Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg

@return [String] Normalized color name

# File lib/doing/colors.rb, line 126
def normalize_color
  gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg')
end
sanitize() click to toggle source
# File lib/doing/completion/fig_completion.rb, line 6
def sanitize
  gsub(/"/, '\"')
end
utf8() click to toggle source

Force UTF-8 encoding if available

@return [String] UTF-8 encoded string

# File lib/doing/string/string.rb, line 33
def utf8
  if String.method_defined? :force_encoding
    dup.force_encoding('utf-8')
  else
    self
  end
end
valid_id?() click to toggle source

Test if string is a valid 32-character MD5 id

@return [Boolean] string is valid identifier

# File lib/doing/string/string.rb, line 24
def valid_id?
  strip =~ /^[a-z0-9]{32}$/ ? true : false
end
validate_color() click to toggle source

Extract the longest valid %color name from a string.

Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.

@return [String] a valid color name

# File lib/doing/colors.rb, line 108
def validate_color
  valid_color = nil
  compiled = ''
  normalize_color.split('').each do |char|
    compiled += char
    valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i
  end

  valid_color
end