module Colored

cute.

>> "this is red".red

>> "this is red with a blue background (read: ugly)".red_on_blue

>> "this is red with an underline".red.underline

>> "this is really bold and really blue".bold.blue

>> Colored.red "This is red" # but this part is mostly untested

Constants

BBS_COLOR_TABLE

BBS-style numeric color codes.

COLORS
EXTRAS
VALID_COLORS

Public Instance Methods

color(color_name) click to toggle source
# File lib/delicious-cli/colored.rb, line 133
def color(color_name)
  background = color_name.to_s =~ /on_/
  color_name = color_name.to_s.sub('on_', '')
  return unless color_name && COLORS[color_name]
  "\e[#{COLORS[color_name] + (background ? 10 : 0)}m" 
end
colorize(string=nil, options = {}) click to toggle source
# File lib/delicious-cli/colored.rb, line 110
def colorize(string=nil, options = {})
  if string == nil
    return tagged_colors(self)
  end
  
  if @@is_tty
    colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * ''
    colored << string
    colored << extra(:clear)
  else
    string
  end
end
colors() click to toggle source
# File lib/delicious-cli/colored.rb, line 124
def colors
  @@colors ||= COLORS.keys.sort
end
disable!() click to toggle source
# File lib/delicious-cli/colored.rb, line 148
def disable!
  @@is_tty = false
end
enable!() click to toggle source
# File lib/delicious-cli/colored.rb, line 142
def enable!
  @@is_tty = true
end
Also aliased as: force!
extra(extra_name) click to toggle source
# File lib/delicious-cli/colored.rb, line 128
def extra(extra_name)
  extra_name = extra_name.to_s
  "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
end
force!()
Alias for: enable!
is_tty?() click to toggle source
# File lib/delicious-cli/colored.rb, line 152
def is_tty?
  @@is_tty
end
tagged_colors(string) click to toggle source

Colorize a string that has “color tags”.

Examples:

Colors as words:

puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize

Numeric ANSI colors (from the BBS days):

puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize
# File lib/delicious-cli/colored.rb, line 178
def tagged_colors(string)
  stack = []

  # split the string into tags and literal strings
  tokens          = string.split(/(<\/?[\w\d_]+>)/)
  tokens.delete_if { |token| token.size == 0 }
  
  result        = ""

  tokens.each do |token|

    # token is an opening tag!
    
    if /<([\w\d_]+)>/ =~ token and valid_tag?($1)
      stack.push $1

    # token is a closing tag!
    
    elsif /<\/([\w\d_]+)>/ =~ token and valid_tag?($1)

      # if this color is on the stack somwehere...
      if pos = stack.rindex($1)
        # close the tag by removing it from the stack
        stack.delete_at pos
      else
        raise "Error: tried to close an unopened color tag -- #{token}"
      end

    # token is a literal string!
    
    else

      color = (stack.last || "white")
      color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
      result << token.send(color)
      
    end
    
  end
  
  result
end
valid_tag?(tag) click to toggle source

Is this string legal?

# File lib/delicious-cli/colored.rb, line 159
def valid_tag?(tag)
  VALID_COLORS.include?(tag) or
  (
    string =~ /^\d+$/ and
    BBS_COLOR_TABLE.include?(tag.to_i)
  )
end