Module: ORI::Colorize

Defined in:
lib/ori/colorize.rb

Overview

Simplistic ANSI colorizer.

Class Method Summary collapse

Class Method Details

.colorize(*args) ⇒ Object



58
59
60
# File 'lib/ori/colorize.rb', line 58

def self.colorize(*args)
  args.map {|v| v.is_a?(Array) ? seq(*v) : v}.join
end

.seq(*spec) ⇒ Object

Issue an ANSI color sequence.

puts [Colorize.seq(:message, :error), "Error!", Colorize.seq(:reset)].join


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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ori/colorize.rb', line 7

def self.seq(*spec)
  Tools.ansi(*case spec
    when [:choice, :title]
      [:green]
    when [:choice, :index]
      [:yellow, :bold]
    when [:choice, :label]
      [:cyan]
    when [:choice, :prompt]
      [:yellow, :bold]

    # These go in sequence, each knows who's before. Thus we minimize ANSI.
    when [:list_method, :own_marker]
      [:reset, :bold]
    when [:list_method, :not_own_marker]
      [:reset]
    when [:list_method, :obj_module_name]
      [:cyan, :bold]
    when [:list_method, :owner_name]
      [:reset]
    when [:list_method, :access]
      [:reset, :cyan]
    when [:list_method, :name]
      [:reset, :bold]
    when [:list_method, :visibility]
      [:reset, :yellow]

    # These go in sequence.
    when [:mam, :module_name]
      [:cyan, :bold]
    when [:mam, :access]
      [:reset, :cyan]
    when [:mam, :method_name]
      [:reset, :bold]

    when [:message, :action]
      [:green]
    when [:message, :error]
      [:red, :bold]
    when [:message, :info]
      [:green]

    when [:reset]
      [:reset]

    else
      raise ArgumentError, "Unknown spec: #{spec.inspect}"
    end
  ) # Tools.ansi
end