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

const_missing, disable, disabled?, enable, enabled?, extended

Class Method Details

.black(&block) ⇒ Object



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

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

.blue(&block) ⇒ Object



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

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

.color(rgb) ⇒ Object



110
111
112
# File 'lib/rib/more/color.rb', line 110

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

.colorize_backtrace(backtrace) ⇒ Object



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

def colorize_backtrace backtrace
  backtrace.map{ |b|
    path, msgs = b.split(':', 2)
    dir, sep, file = path.rpartition('/')
    msgs ||= (line - 1).to_s # msgs would be nil when input is next/break
    msg = msgs.sub(/(\d+)(:?)/) do
      m = Regexp.last_match
      "#{red{m[1]}}#{m[2]}"
    end.sub(/(?:`|').+?'/){green{Regexp.last_match[0]}}

    "#{dir+sep}#{yellow{file}}:#{msg}"
  }
end

.cyan(&block) ⇒ Object



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

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

.find_color(colors, value) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/rib/more/color.rb', line 101

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



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

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

.magenta(&block) ⇒ Object



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

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

.red(&block) ⇒ Object



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

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

.reset(&block) ⇒ Object



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

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

.white(&block) ⇒ Object



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

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

.yellow(&block) ⇒ Object



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

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

Instance Method Details

#before_loopObject

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



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

def before_loop
  colors
  super
end

#colorsObject

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



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rib/more/color.rb', line 34

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_backtrace(backtrace) ⇒ Object



80
81
82
# File 'lib/rib/more/color.rb', line 80

def format_backtrace backtrace
  colorize_backtrace(super(backtrace))
end

#format_color(result, display = inspect_result(result)) ⇒ Object



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
73
74
75
76
77
78
# File 'lib/rib/more/color.rb', line 48

def format_color result, display=inspect_result(result)
  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



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

def format_result result
  return super if Color.disabled?
  "#{result_prompt}#{format_color(result)}"
end

#get_error(err) ⇒ Object



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

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

#warn(message) ⇒ Object



27
28
29
30
# File 'lib/rib/more/color.rb', line 27

def warn message
  return super if Color.disabled?
  super(red{message})
end