Module: Rib::Color

Extended by:
Plugin
Defined in:
lib/rib/more/color.rb

Instance Attribute Summary

Attributes included from Plugin

#disabled

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugin

disable, disabled?, enable, enabled?, extended

Class Method Details

.black(&block) ⇒ Object



101
# File 'lib/rib/more/color.rb', line 101

def   black █ color(30, &block); end

.blue(&block) ⇒ Object



105
# File 'lib/rib/more/color.rb', line 105

def    blue █ color(34, &block); end

.color(rgb) ⇒ Object



97
98
99
# File 'lib/rib/more/color.rb', line 97

def color rgb
  "\e[#{rgb}m" + if block_given? then "#{yield}#{reset}" else '' end
end

.colorize_backtrace(backtrace) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rib/more/color.rb', line 78

def colorize_backtrace backtrace
  backtrace.map{ |b|
    path, msgs = b.split(':', 2)
    dir , file = ::File.split(path)
    msg = msgs.sub(/(\d+):/){red{$1}+':'}.sub(/`.+?'/){green{$&}}

    "#{dir+'/'}#{yellow{file}}:#{msg}"
  }
end

.cyan(&block) ⇒ Object



107
# File 'lib/rib/more/color.rb', line 107

def    cyan █ color(36, &block); end

.find_color(colors, value) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/rib/more/color.rb', line 88

def find_color colors, value
  (colors.sort{ |(k1, _), (k2, _)|
    # Class <=> Class
    if    k1 < k2 then -1
    elsif k1 > k2 then  1
    else                0
    end}.find{ |(klass, _)| value.kind_of?(klass) } || []).last
end

.green(&block) ⇒ Object



103
# File 'lib/rib/more/color.rb', line 103

def   green &block; color(32, &block); end

.magenta(&block) ⇒ Object



106
# File 'lib/rib/more/color.rb', line 106

def magenta &block; color(35, &block); end

.red(&block) ⇒ Object



102
# File 'lib/rib/more/color.rb', line 102

def     red &block; color(31, &block); end

.reset(&block) ⇒ Object



109
# File 'lib/rib/more/color.rb', line 109

def   reset &block; color( 0, &block); end

.white(&block) ⇒ Object



108
# File 'lib/rib/more/color.rb', line 108

def   white &block; color(37, &block); end

.yellow(&block) ⇒ Object



104
# File 'lib/rib/more/color.rb', line 104

def  yellow &block; color(33, &block); end

Instance Method Details

#before_loopObject

————— Rib API —————



10
11
12
13
# File 'lib/rib/more/color.rb', line 10

def before_loop
  colors
  super
end

#colorsObject

————— Plugin API —————



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rib/more/color.rb', line 22

def colors
  config[:color] ||= {
    Numeric    => :red    ,
    String     => :green  ,
    Symbol     => :cyan   ,
    Array      => :blue   ,
    Hash       => :blue   ,
    NilClass   => :magenta,
    TrueClass  => :magenta,
    FalseClass => :magenta,
    Exception  => :magenta,
    Object     => :yellow }
end

#format_color(result, display = result.inspect) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rib/more/color.rb', line 36

def format_color result, display=result.inspect
  case result
    when String ; send(colors[String ]){ display }
    when Numeric; send(colors[Numeric]){ display }
    when Symbol ; send(colors[Symbol ]){ display }

    when Array  ; send(colors[Array  ]){ '['     }   +
                    result.map{ |e   | if e.object_id == result.object_id
                                         send(colors[Array]){'[...]'}
                                       else
                                         format_color(e); end }.
             join(send(colors[Array  ]){ ', '    })  +
                  send(colors[Array  ]){ ']'     }

    when Hash   ; send(colors[Hash   ]){ '{'     }   +
                    result.map{ |k, v| format_color(k) +
                  send(colors[Hash   ]){ '=>'    }   +
                                       if v.object_id == result.object_id
                                         send(colors[Hash]){'{...}'}
                                       else
                                         format_color(v); end }.
             join(send(colors[Hash   ]){ ', '    }) +
                  send(colors[Hash   ]){ '}'     }

    else        ; if color = find_color(colors, result)
                  send(color){ display }
                  else
                  send(colors[Object      ]){ display }
                  end
  end
end

#format_result(result) ⇒ Object



15
16
17
18
# File 'lib/rib/more/color.rb', line 15

def format_result result
  return super if Color.disabled?
  config[:result_prompt] + format_color(result)
end

#get_error(err, backtrace = err.backtrace) ⇒ Object

override StripBacktrace#get_error



69
70
71
72
73
# File 'lib/rib/more/color.rb', line 69

def get_error err, backtrace=err.backtrace
  return super if Color.disabled?
  [format_color(err, "#{err.class.to_s}: #{err.message}"),
   colorize_backtrace(backtrace)]
end