Module: NyanCat::Common
Constant Summary collapse
- ESC =
"\e["
- NND =
"#{ESC}0m"
- PASS =
'='
- PASS_ARY =
['-', '_']
- FAIL =
'*'
- ERROR =
'!'
- PENDING =
'+'
- VT100_CODES =
{ :black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :magenta => 35, :cyan => 36, :white => 37, :bold => 1, }
- VT100_CODE_VALUES =
VT100_CODES.invert
Class Method Summary collapse
Instance Method Summary collapse
-
#ascii_cat(o = '^') ⇒ Array
Ascii version of Nyan cat.
-
#cat_length ⇒ Object
Returns the cat length.
-
#colors ⇒ Array
Calculates the colors of the rainbow.
- #console_code_for(code_or_symbol) ⇒ Object
-
#current_width ⇒ Fixnum
Calculates the current flight length.
-
#dump_progress ⇒ Object
Displays the current progress in all Nyan Cat glory.
-
#eol ⇒ String
Determines how we end the trail line.
-
#example_width(item = 1) ⇒ Object
Times a mark has to be repeated.
-
#failed_or_pending? ⇒ Boolean
Determines if the any specs failed or are in pending state.
- #failure_color(text) ⇒ Object
-
#finished? ⇒ Boolean
Determines if the specs have completed.
-
#format_duration(duration) ⇒ String
Converts a float of seconds into a minutes/seconds string.
-
#highlight(mark = PASS) ⇒ String
Determines how to color the example.
-
#nyan_cat ⇒ String
Determine which Ascii Nyan Cat to display.
-
#nyan_trail ⇒ String
Creates a rainbow trail.
-
#padding_width ⇒ Fixnum
Gets the padding for the current example count.
- #pending_color(text) ⇒ Object
- #progress_lines ⇒ Object
-
#rainbowify(string) ⇒ String
Colorizes the string with raindow colors of the rainbow.
-
#scoreboard ⇒ Array
Creates a data store of pass, failed, and pending example results We have to pad the results here because sprintf can’t properly pad color.
- #success_color(text) ⇒ Object
-
#terminal_width ⇒ Fixnum
A Unix trick using stty to get the console columns.
-
#tick(mark = PASS) ⇒ Object
Increments the example count and displays the current progress.
- #wrap(text, code_or_symbol) ⇒ Object
Class Method Details
.included(base) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/nyan_cat_formatter/common.rb', line 27 def self.included(base) base.class_eval do attr_reader :current, :example_results, :color_index, :pending_count, :failure_count, :example_count end end |
Instance Method Details
#ascii_cat(o = '^') ⇒ Array
Ascii version of Nyan cat. Two cats in the array allow Nyan to animate running.
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/nyan_cat_formatter/common.rb', line 146 def ascii_cat(o = '^') [[ "_,------, ", "_| /\\_/\\ ", "~|_( #{o} .#{o}) ", " \"\" \"\" " ], [ "_,------, ", "_| /\\_/\\", "^|__( #{o} .#{o}) ", " \" \" \" \"" ]] end |
#cat_length ⇒ Object
Returns the cat length
228 229 230 |
# File 'lib/nyan_cat_formatter/common.rb', line 228 def cat_length nyan_cat.split("\n").group_by(&:size).max.first end |
#colors ⇒ Array
Calculates the colors of the rainbow
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/nyan_cat_formatter/common.rb', line 172 def colors @colors ||= (0...(6 * 7)).map do |n| pi_3 = Math::PI / 3 n *= 1.0 / 6 r = (3 * Math.sin(n ) + 3).to_i g = (3 * Math.sin(n + 2 * pi_3) + 3).to_i b = (3 * Math.sin(n + 4 * pi_3) + 3).to_i 36 * r + 6 * g + b + 16 end end |
#console_code_for(code_or_symbol) ⇒ Object
244 245 246 247 248 249 250 251 252 |
# File 'lib/nyan_cat_formatter/common.rb', line 244 def console_code_for(code_or_symbol) if VT100_CODE_VALUES.has_key?(code_or_symbol) code_or_symbol else VT100_CODES.fetch(code_or_symbol) do console_code_for(:white) end end end |
#current_width ⇒ Fixnum
Calculates the current flight length
88 89 90 91 |
# File 'lib/nyan_cat_formatter/common.rb', line 88 def current_width # padding_width + example_width + cat_length padding_width + (@current * example_width) + cat_length end |
#dump_progress ⇒ Object
Displays the current progress in all Nyan Cat glory
63 64 65 |
# File 'lib/nyan_cat_formatter/common.rb', line 63 def dump_progress output.print(progress_lines.join("\n") + eol) end |
#eol ⇒ String
Determines how we end the trail line. If complete, return a newline etc.
79 80 81 82 83 |
# File 'lib/nyan_cat_formatter/common.rb', line 79 def eol return "\n" if @current == @example_count length = progress_lines.length - 1 length > 0 ? format("\e[1A" * length + "\r") : "\r" end |
#example_width(item = 1) ⇒ Object
Times a mark has to be repeated
138 139 140 |
# File 'lib/nyan_cat_formatter/common.rb', line 138 def example_width(item = 1) 1 end |
#failed_or_pending? ⇒ Boolean
Determines if the any specs failed or are in pending state
221 222 223 |
# File 'lib/nyan_cat_formatter/common.rb', line 221 def failed_or_pending? (@failure_count.to_i > 0 || @pending_count.to_i > 0) end |
#failure_color(text) ⇒ Object
240 241 242 |
# File 'lib/nyan_cat_formatter/common.rb', line 240 def failure_color(text) wrap(text, :failure) end |
#finished? ⇒ Boolean
Determines if the specs have completed
214 215 216 |
# File 'lib/nyan_cat_formatter/common.rb', line 214 def finished? (@current == @example_count) end |
#format_duration(duration) ⇒ String
Converts a float of seconds into a minutes/seconds string
200 201 202 203 204 205 206 207 208 |
# File 'lib/nyan_cat_formatter/common.rb', line 200 def format_duration(duration) seconds = ((duration % 60) * 100.0).round / 100.0 # 1.8.7 safe .round(2) seconds = seconds.to_i if seconds.to_i == seconds # drop that zero if it's not needed = "#{seconds} second#{seconds == 1 ? "" : "s"}" = "#{(duration / 60).to_i} minute#{(duration / 60).to_i == 1 ? "" : "s"} and " + if duration >= 60 end |
#highlight(mark = PASS) ⇒ String
Determines how to color the example. If pass, it is rainbowified, otherwise we assign red if failed or yellow if an error occurred.
187 188 189 190 191 192 193 194 195 |
# File 'lib/nyan_cat_formatter/common.rb', line 187 def highlight(mark = PASS) case mark when PASS; rainbowify PASS_ARY[@color_index%2] when FAIL; "\e[31m#{mark}\e[0m" when ERROR; "\e[33m#{mark}\e[0m" when PENDING; "\e[33m#{mark}\e[0m" else mark end end |
#nyan_cat ⇒ String
Determine which Ascii Nyan Cat to display. If tests are complete, Nyan Cat goes to sleep. If there are failing or pending examples, Nyan Cat is concerned.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nyan_cat_formatter/common.rb', line 48 def nyan_cat if self.failed_or_pending? && self.finished? ascii_cat('x')[@color_index%2].join("\n") #'~|_(x.x)' elsif self.failed_or_pending? ascii_cat('o')[@color_index%2].join("\n") #'~|_(o.o)' elsif self.finished? ascii_cat('-')[@color_index%2].join("\n") # '~|_(-.-)' else ascii_cat('^')[@color_index%2].join("\n") # '~|_(^.^)' end end |
#nyan_trail ⇒ String
Creates a rainbow trail
129 130 131 132 133 134 135 |
# File 'lib/nyan_cat_formatter/common.rb', line 129 def nyan_trail marks = @example_results.each_with_index.map{ |mark, i| highlight(mark) * example_width(i) } marks.shift(current_width - terminal_width) if current_width >= terminal_width nyan_cat.split("\n").each_with_index.map do |line, index| format("%s#{line}", marks.join) end.join("\n") end |
#padding_width ⇒ Fixnum
Gets the padding for the current example count
96 97 98 |
# File 'lib/nyan_cat_formatter/common.rb', line 96 def padding_width @example_count.to_s.length * 2 + 6 end |
#pending_color(text) ⇒ Object
236 237 238 |
# File 'lib/nyan_cat_formatter/common.rb', line 236 def pending_color(text) wrap(text, :pending) end |
#progress_lines ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/nyan_cat_formatter/common.rb', line 67 def progress_lines [ nyan_trail.split("\n").each_with_index.inject([]) do |result, (trail, index)| value = "#{scoreboard[index]}/#{@example_count}:" result << format("%s %s", value, trail) end ].flatten end |
#rainbowify(string) ⇒ String
Colorizes the string with raindow colors of the rainbow
163 164 165 166 167 |
# File 'lib/nyan_cat_formatter/common.rb', line 163 def rainbowify(string) c = colors[@color_index % colors.size] @color_index += 1 "#{ESC}38;5;#{c}m#{string}#{NND}" end |
#scoreboard ⇒ Array
Creates a data store of pass, failed, and pending example results We have to pad the results here because sprintf can’t properly pad color
116 117 118 119 120 121 122 123 124 |
# File 'lib/nyan_cat_formatter/common.rb', line 116 def scoreboard @pending_examples ||= [] @failed_examples ||= [] padding = @example_count.to_s.length [ @current.to_s.rjust(padding), success_color((@current - @pending_examples.size - @failed_examples.size).to_s.rjust(padding)), pending_color(@pending_examples.size.to_s.rjust(padding)), failure_color(@failed_examples.size.to_s.rjust(padding)) ] end |
#success_color(text) ⇒ Object
232 233 234 |
# File 'lib/nyan_cat_formatter/common.rb', line 232 def success_color(text) wrap(text, :success) end |
#terminal_width ⇒ Fixnum
A Unix trick using stty to get the console columns
103 104 105 106 107 108 109 110 |
# File 'lib/nyan_cat_formatter/common.rb', line 103 def terminal_width if defined? JRUBY_VERSION default_width = 80 else default_width = `stty size`.split.map { |x| x.to_i }.reverse.first - 1 end @terminal_width ||= default_width end |
#tick(mark = PASS) ⇒ Object
Increments the example count and displays the current progress
37 38 39 40 41 |
# File 'lib/nyan_cat_formatter/common.rb', line 37 def tick(mark = PASS) @example_results << mark @current = (@current > @example_count) ? @example_count : @current + 1 dump_progress end |
#wrap(text, code_or_symbol) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/nyan_cat_formatter/common.rb', line 254 def wrap(text, code_or_symbol) if RSpec.configuration.color_enabled? "\e[#{console_code_for(code_or_symbol)}m#{text}\e[0m" else text end end |