Bee C0 Coverage Information - RCov

lib/bee_console_style.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
lib/bee_console_style.rb 222 163
100.00%
100.00%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 # Copyright 2006-2012 Michel Casabianca <michel.casabianca@gmail.com>
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 require 'rubygems'
16 require 'bee_util'
17 
18 module Bee
19 
20   module Console
21 
22     # Class to manage a style for console output.
23     class Style
24 
25       include Bee::Util::BuildErrorMixin
26 
27       # style attributes
28       attr_reader :line_character
29       attr_reader :line_length
30       attr_reader :target_style
31       attr_reader :target_foreground
32       attr_reader :target_background
33       attr_reader :task_style
34       attr_reader :task_foreground
35       attr_reader :task_background
36       attr_reader :success_style
37       attr_reader :success_foreground
38       attr_reader :success_background
39       attr_reader :error_style
40       attr_reader :error_foreground
41       attr_reader :error_background
42 
43       # List of style types
44       TYPES = [:target, :task, :success, :error]
45 
46       # List of colors.
47       COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
48 
49       # Foreground color codes.
50       FOREGROUND_COLOR_CODES = {
51         :black   => 30,
52         :red     => 31,
53         :green   => 32,
54         :yellow  => 33,
55         :blue    => 34,
56         :magenta => 35,
57         :cyan    => 36,
58         :white   => 37
59       }
60 
61       # Background color codes.
62       BACKGROUND_COLOR_CODES = {
63         :black   => 40,
64         :red     => 41,
65         :green   => 42,
66         :yellow  => 43,
67         :blue    => 44,
68         :magenta => 45,
69         :cyan    => 46,
70         :white   => 47
71       }
72 
73       # List of styles.
74       STYLES = [:reset, :bright, :dim, :underscore, :blink, :reverse, :hidden]
75 
76       # Style codes.
77       STYLE_CODES = {
78         :reset      => 0,
79         :bright     => 1,
80         :dim        => 2,
81         :underscore => 4,
82         :blink      => 5,
83         :reverse    => 7,
84         :hidden     => 8
85       }
86 
87       # Default style (supposed to work on any configuration).
88       DEFAULT_STYLE = {
89         :line_character => '-'
90       }
91 
92       # Color style (supposed to work on color terminals).
93       COLOR_STYLE = {
94         :line_character     => '-',
95         :target_foreground  => :yellow,
96         :task_foreground    => :blue,
97         :success_style      => :bright,
98         :success_foreground => :green,
99         :error_style        => :bright,
100         :error_foreground   => :red
101       }
102 
103       # Short style keys for command line
104       SHORT_STYLE_KEYS = {
105         'lc' => 'line_character',
106         'll' => 'line_length',
107         'ts' => 'target_style',
108         'tf' => 'target_foreground',
109         'tb' => 'target_background',
110         'ks' => 'task_style',
111         'kf' => 'task_foreground',
112         'kb' => 'task_background',
113         'ss' => 'success_style',
114         'sf' => 'success_foreground',
115         'sb' => 'success_background',
116         'es' => 'error_style',
117         'ef' => 'error_foreground',
118         'eb' => 'error_background'
119       }
120 
121       # Build the style from command line arguments:
122       # - style: the style as a hash or a string (as passed on command line).
123       #   Defaults to nil.
124       # - color: tells if we use color style. Defaults to nil.
125       def initialize(style=nil, color=nil)
126         @line_character = nil
127         @line_length = nil
128         @target_style = nil
129         @target_foreground = nil
130         @target_background = nil
131         @task_style = nil
132         @task_foreground = nil
133         @task_background = nil
134         @success_style = nil
135         @success_foreground = nil
136         @success_background = nil
137         @error_style = nil
138         @error_foreground = nil
139         @error_background = nil
140         apply(color ? COLOR_STYLE : DEFAULT_STYLE)
141         apply(style)
142       end
143 
144       # Apply style to a string:
145       # - string: the string to apply style to.
146       # - type: the type of style to apply (one of :target, :task, :success or
147       #   :error).
148       def style(string, type)
149         raise "Type '#{type}' unknown: must be one of " + TYPES.map{|e| ":#{e}"}.join(', ') if
150           not TYPES.include?(type)
151         style = eval("@#{type}_style")
152         foreground = eval("@#{type}_foreground")
153         background = eval("@#{type}_background")
154         # if no style nor colors, return raw string
155         return string if not foreground and not background and not style
156         # insert style and colors in string
157         colorized = "\e["
158         colorized << "#{STYLE_CODES[style]};" if style
159         colorized << "#{FOREGROUND_COLOR_CODES[foreground]};" if foreground
160         colorized << "#{BACKGROUND_COLOR_CODES[background]};" if background
161         colorized = colorized[0..-2]
162         colorized << "m#{string}\e[#{STYLE_CODES[:reset]}m"
163         return colorized
164       end
165 
166       private
167 
168       # Apply a given style:
169       # - style: the style as a hash or a string.
170       def apply(style)
171         if style.kind_of?(Hash)
172           for key, value in style
173             check_attribute_value(key, value)
174             eval("@#{key} = #{value.inspect}")
175           end
176         elsif style.kind_of?(String)
177           for pair in style.split(',')
178             key, value = pair.split(':')
179             key = SHORT_STYLE_KEYS[key] || key
180             key = key.to_sym
181             if key == :line_length
182               value = value.to_i
183             elsif key == :line_character
184               value = ' ' if not value or value.length == 0
185             else
186               value = value.to_sym if value
187             end
188             check_attribute_value(key, value)
189             eval("@#{key} = #{value.inspect}")
190           end
191         else
192           raise "Style must ne a Hash or a String" if style
193         end
194       end
195 
196       def check_attribute_value(attribute, value)
197         raise "Attribute '#{attribute}' must be a symbol" unless
198           attribute.kind_of?(Symbol)
199         raise "Unknown attribute '#{attribute}'" unless
200           instance_variable_defined?("@#{attribute}".to_sym)
201         if attribute == :line_length
202           raise "'line_length' attribute must be an integer" unless
203             value.kind_of?(Integer)
204         elsif attribute == :line_character
205           raise "'line_character' must be a single character" unless
206             value.kind_of?(String) and value.length == 1
207         else
208           raise "Value '#{value}' should be a symbol" unless
209             value.kind_of?(Symbol)
210           if attribute.to_s[-6..-1] == '_style'
211             raise "Unkown style '#{value}'" if not STYLES.member?(value)
212           else
213             raise "Unkown color '#{value}'" if not COLORS.member?(value)
214           end
215         end
216       end
217 
218     end
219 
220   end
221 
222 end

Generated on Fri Oct 09 02:07:49 +0200 2015 with rcov 1.0.0