public
Description: Augment is a system for gathering metadata from code and displaying it.
Homepage: http://augment.rubyforge.org
Clone URL: git://github.com/technomancy/augment.git
augment / lib / frontends / ansi_frontend.rb
100644 38 lines (32 sloc) 0.828 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class String
  COLOR_LOOKUP = {'black' => 30,
      'red' => 31,
      'green' => 32,
      'yellow' => 33,
      'blue' => 34,
      'magenta' => 35,
      'cyan' => 36,
      'white' => 37,
    'default' => 38 }
  
  def colorize(color_code)
    "\e[#{String.lookup_color_code(color_code)}m#{self}\e[0m"
  end
 
  def colorize_range(range, color)
    "#{self[0 ... range.begin]}#{self[range].colorize(color)}#{self[range.end .. -1]}"
  end
  
  def self.lookup_color_code(color, foreground = true)
    return color if color.is_a? Fixnum
    COLOR_LOOKUP[color.downcase]
  end
end
 
class AnsiColorFrontend < Frontend
  class << self
    def run(file)
      puts super(file)
    end
    
    def process_layer(text, layer)
      text.colorize_range(layer.range, layer.color)
    end
  end
  Augment::FRONTENDS['ansi'] = self
end