Sha256: 55efb28a1c348015d519f6a9f94a00780bb0efc6faea1c737878dbe08234df80

Contents?: true

Size: 1.3 KB

Versions: 11

Compression:

Stored size: 1.3 KB

Contents

String.class_eval do
  def quote(q="\"")
    "#{q}#{self}#{q}"
  end
  
  def quote_if_spaced(q="\"")
    self =~ /\s/ ? self.quote : self
  end
  
  def close_unmatched_quotes(q="\"")
    (self.scan(/"/).size % 2) != 0 ? self+q : self
  end
  
  BASH_COLOR = {
    :black => 30,
    :red => 31,
    :green => 32,
    :yellow => 33,
    :blue => 34,
    :magenta => 35,
    :cyan => 36,
    :white => 37,
    :default => 39
  }
  BASH_EFFECT = {
    :none => 0,
    :bright => 1,
    :underline => 4,
    :blink => 5,
    :exchange => 7,
    :hidden => 8
  }
  def colorize(options={})
    return '' if self == ''
    options = {:color => options} if options.is_a?(Symbol)
    options[:color] = :default unless options[:color]
    if options[:color] != :default && options[:color].to_s =~ /^(light|bright)/
      options[:color] = options[:color].to_s.sub(/^(light|bright)/, '').to_sym
      options[:effect] = :bright 
    end
    options[:background] = :default unless options[:background]
    options[:effect] = :none unless options[:effect]
    
    effect_code = "#{BASH_EFFECT[options[:effect]]}"
    background_code = "#{BASH_COLOR[options[:background]]+10}m"
    color_code = "#{BASH_COLOR[options[:color]]}"
    reset_code = "\e[0m"
    "\e[#{effect_code};#{color_code};#{background_code}#{self}#{reset_code}"
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
Overbryd-mighty-mite-0.1.0 lib/string_ext.rb
Overbryd-mighty-mite-0.1.1 lib/string_ext.rb
Overbryd-mighty-mite-0.1.2 lib/string_ext.rb
Overbryd-mighty-mite-0.1.3 lib/string_ext.rb
Overbryd-mite.cmd-0.1.3 lib/string_ext.rb
Overbryd-mite.cmd-0.1.4 lib/string_ext.rb
Overbryd-mite.cmd-0.1.5 lib/string_ext.rb
Overbryd-mite.cmd-0.1.7 lib/string_ext.rb
Overbryd-mite.cmd-0.1.9 lib/string_ext.rb
mite.cmd-0.1.12 lib/string_ext.rb
mite.cmd-0.1.10 lib/string_ext.rb