Skip to content

Commit

Permalink
.riplrc: quick and dirty color implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
godfat committed Feb 22, 2011
1 parent 6d39199 commit 5439af9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .riplrc
Expand Up @@ -35,6 +35,55 @@ module Ripl::RiplRc
def print_result result
super unless @input.strip == ''
end

def format_result result
colors = {
String => :green ,
Numeric => :red ,
Symbol => :cyan ,
Array => :blue ,
Hash => :blue ,
NilClass => :magenta,
TrueClass => :magenta,
FalseClass => :magenta,
Object => :yellow
}

case result
when String ; send(colors[String ]){ "'#{result}'" }
when Numeric; send(colors[Numeric ]){ result }
when Symbol ; send(colors[Symbol ]){ ":#{result}" }
when Array ; send(colors[Array ]){ '[' } +
result.map{ |e| format_result(e) }.join(
send(colors[Array ]){ ', ' }) +
send(colors[Array ]){ ']' }
when Hash ; send(colors[Hash ]){ '{' } +
result.map{ |k, v| format_result(k) +
send(colors[Hash ]){ '=>' } +
format_result(v) }.join(
send(colors[Hash ]){ ', ' }) +
send(colors[Hash ]){ '}' }
else ; if colors[result.class]
send(colors[result.class]){ result.inspect }
else
send(colors[Object ]){ result.inspect }
end
end
end

def color rgb
"\x1b[#{rgb}m" + (block_given? ? "#{yield}#{reset}" : '')
end

def black █ color(30, &block); end
def red █ color(31, &block); end
def green █ color(32, &block); end
def yellow █ color(33, &block); end
def blue █ color(34, &block); end
def magenta █ color(35, &block); end
def cyan █ color(36, &block); end
def white █ color(37, &block); end
def reset █ color('', &block); end
end

Ripl::Shell.include(Ripl::RiplRc)
Expand Down

0 comments on commit 5439af9

Please sign in to comment.