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



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

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

.blue(&block) ⇒ Object



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

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

.color(rgb) ⇒ Object



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

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

.colorize_backtrace(backtrace) ⇒ Object



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

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



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

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

.find_color(colors, value) ⇒ Object



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

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



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

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

.magenta(&block) ⇒ Object



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

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

.red(&block) ⇒ Object



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

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

.reset(&block) ⇒ Object



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

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

.white(&block) ⇒ Object



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

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

.yellow(&block) ⇒ Object



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

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



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

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



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

def format_backtrace backtrace
  colorize_backtrace(super(backtrace))
end

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



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

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



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

def format_result result
  return super if Color.disabled?
  "#{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), backtrace]
end

#warn(message) ⇒ Object



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

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