File: color-scheme.rb

package info (click to toggle)
ruby-test-unit 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 928 kB
  • ctags: 1,759
  • sloc: ruby: 12,818; makefile: 6
file content (159 lines) | stat: -rw-r--r-- 5,865 bytes parent folder | download | duplicates (2)
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
39
40
41
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'test/unit/color'

module Test
  module Unit
    class ColorScheme
      include Enumerable

      class << self
        def default
          if available_colors == 256
            default_for_256_colors
          else
            default_for_8_colors
          end
        end

        @@default_for_8_colors = nil
        def default_for_8_colors
          @@default_for_8_colors ||=
            new("pass" => Color.new("green", :background => true) +
                          Color.new("white", :bold => true),
                "failure" => Color.new("red", :background => true) +
                             Color.new("white", :bold => true),
                "pending" => Color.new("magenta", :background => true) +
                             Color.new("white", :bold => true),
                "omission" => Color.new("blue", :background => true) +
                             Color.new("white", :bold => true),
                "notification" => Color.new("cyan", :background => true) +
                                  Color.new("white", :bold => true),
                "error" => Color.new("black", :background => true) +
                           Color.new("yellow", :bold => true),
                "case" => Color.new("blue", :background => true) +
                          Color.new("white", :bold => true),
                "suite" => Color.new("green", :background => true) +
                           Color.new("white", :bold => true),
                "diff-inserted-tag" => Color.new("red", :background => true) +
                                       Color.new("black", :bold => true),
                "diff-deleted-tag" => Color.new("green", :background => true) +
                                      Color.new("black", :bold => true),
                "diff-difference-tag" => Color.new("cyan", :background => true) +
                                         Color.new("white", :bold => true),
                "diff-inserted" => Color.new("red", :background => true) +
                                   Color.new("white", :bold => true),
                "diff-deleted" =>  Color.new("green", :background => true) +
                                   Color.new("white", :bold => true))
        end

        @@default_for_256_colors = nil
        def default_for_256_colors
          @@default_for_256_colors ||=
            new("pass" => Color.new("030", :background => true) +
                          Color.new("555", :bold => true),
                "failure" => Color.new("300", :background => true) +
                             Color.new("555", :bold => true),
                "pending" => Color.new("303", :background => true) +
                              Color.new("555", :bold => true),
                "omission" => Color.new("001", :background => true) +
                              Color.new("555", :bold => true),
                "notification" => Color.new("011", :background => true) +
                                  Color.new("555", :bold => true),
                "error" => Color.new("000", :background => true) +
                           Color.new("550", :bold => true),
                "case" => Color.new("220", :background => true) +
                          Color.new("555", :bold => true),
                "suite" => Color.new("110", :background => true) +
                           Color.new("555", :bold => true),
                "diff-inserted-tag" => Color.new("500", :background => true) +
                                       Color.new("000", :bold => true),
                "diff-deleted-tag" => Color.new("050", :background => true) +
                                      Color.new("000", :bold => true),
                "diff-difference-tag" => Color.new("005", :background => true) +
                                         Color.new("555", :bold => true),
                "diff-inserted" => Color.new("300", :background => true) +
                                   Color.new("555", :bold => true),
                "diff-deleted" =>  Color.new("030", :background => true) +
                                   Color.new("555", :bold => true))
        end

        @@schemes = {}
        def all
          @@schemes.merge("default" => default)
        end

        def [](id)
          @@schemes[id.to_s]
        end

        def []=(id, scheme_or_spec)
          if scheme_or_spec.is_a?(self)
            scheme = scheme_or_spec
          else
            scheme = new(scheme_or_spec)
          end
          @@schemes[id.to_s] = scheme
        end

        def available_colors
          case ENV["COLORTERM"]
          when "gnome-terminal"
            256
          else
            case ENV["TERM"]
            when /-256color\z/
              256
            else
              8
            end
          end
        end
      end

      def initialize(scheme_spec)
        @scheme = {}
        scheme_spec.each do |key, color_spec|
          self[key] = color_spec
        end
      end

      def [](name)
        @scheme[name.to_s]
      end

      def []=(name, color_spec)
        @scheme[name.to_s] = make_color(color_spec)
      end

      def each(&block)
        @scheme.each(&block)
      end

      def to_hash
        hash = {}
        @scheme.each do |key, color|
          hash[key] = color
        end
        hash
      end

      private
      def make_color(color_spec)
        if color_spec.is_a?(Color) or color_spec.is_a?(MixColor)
          color_spec
        else
          color_name = nil
          normalized_color_spec = {}
          color_spec.each do |key, value|
            key = key.to_sym
            if key == :name
              color_name = value
            else
              normalized_color_spec[key] = value
            end
          end
          Color.new(color_name, normalized_color_spec)
        end
      end
    end
  end
end