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



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

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

.blue(&block) ⇒ Object



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

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

.color(rgb) ⇒ Object



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

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

.colorize_backtrace(backtrace) ⇒ Object



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

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



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

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

.find_color(colors, value) ⇒ Object



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

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



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

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

.magenta(&block) ⇒ Object



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

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

.red(&block) ⇒ Object



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

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

.reset(&block) ⇒ Object



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

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

.white(&block) ⇒ Object



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

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

.yellow(&block) ⇒ Object



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

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 —————



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rib/more/color.rb', line 28

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



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
67
68
69
70
71
72
# File 'lib/rib/more/color.rb', line 42

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) ⇒ Object



20
21
22
23
24
# File 'lib/rib/more/color.rb', line 20

def get_error err
  return super if Color.disabled?
  message, backtrace = super
  [format_color(err, message), colorize_backtrace(backtrace)]
end