Class: SpinnerFormatter
- Inherits:
-
DottedFormatter
- Object
- DottedFormatter
- SpinnerFormatter
- Defined in:
- lib/extensions/mspec/mspec/runner/formatters/spinner.rb
Direct Known Subclasses
Constant Summary collapse
- Spins =
%w!| / - \\!
- HOUR =
3600- MIN =
60
Instance Attribute Summary collapse
-
#length ⇒ Object
Returns the value of attribute length.
Attributes inherited from DottedFormatter
Instance Method Summary collapse
-
#after(state) ⇒ Object
Callback for the MSpec :after event.
- #clear_progress_line ⇒ Object
- #compute_etr ⇒ Object
- #compute_percentage ⇒ Object
- #compute_progress ⇒ Object
-
#exception(exception) ⇒ Object
Callback for the MSpec :exception event.
- #finish(printed_exceptions = true) ⇒ Object
-
#initialize(out = nil) ⇒ SpinnerFormatter
constructor
A new instance of SpinnerFormatter.
- #progress_line ⇒ Object
- #register ⇒ Object
-
#start ⇒ Object
Callback for the MSpec :start event.
-
#unload ⇒ Object
Callback for the MSpec :unload event.
Methods inherited from DottedFormatter
#abort, #before, #exception?, #failure?, #print, #print_exception
Constructor Details
#initialize(out = nil) ⇒ SpinnerFormatter
Returns a new instance of SpinnerFormatter.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 11 def initialize(out=nil) super(nil) @which = 0 @loaded = 0 self.length = 40 @percent = 0 @start = Time.now term = ENV['TERM'] @color = (term != "dumb") @fail_color = "32" @error_color = "32" end |
Instance Attribute Details
#length ⇒ Object
Returns the value of attribute length.
5 6 7 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 5 def length @length end |
Instance Method Details
#after(state) ⇒ Object
Callback for the MSpec :after event. Updates the spinner.
108 109 110 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 108 def after(state) print progress_line end |
#clear_progress_line ⇒ Object
76 77 78 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 76 def clear_progress_line print "\r#{' '*progress_line.length}" end |
#compute_etr ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 40 def compute_etr return @etr = "00:00:00" if @percent == 0 elapsed = Time.now - @start remain = (100 * elapsed / @percent) - elapsed hour = remain >= HOUR ? (remain / HOUR).to_i : 0 remain -= hour * HOUR min = remain >= MIN ? (remain / MIN).to_i : 0 sec = remain - min * MIN @etr = "%02d:%02d:%02d" % [hour, min, sec] end |
#compute_percentage ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 53 def compute_percentage @percent = @loaded * 100 / @total = ("=" * (@percent / @ratio)).ljust @length label = "%d%%" % @percent [@position, label.size] = label @bar = end |
#compute_progress ⇒ Object
61 62 63 64 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 61 def compute_progress compute_percentage compute_etr end |
#exception(exception) ⇒ Object
Callback for the MSpec :exception event. Changes the color used to display the tally of errors and failures
98 99 100 101 102 103 104 105 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 98 def exception(exception) super @fail_color = "31" if exception.failure? @error_color = "33" unless exception.failure? clear_progress_line print_exception(exception, @count) end |
#finish(printed_exceptions = true) ⇒ Object
112 113 114 115 116 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 112 def finish(printed_exceptions = true) # We already printed the exceptions @exceptions = [] if printed_exceptions super() end |
#progress_line ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 66 def progress_line @which = (@which + 1) % Spins.size data = [Spins[@which], @bar, @etr, @counter.failures, @counter.errors] if @color "\r[%s | %s | %s] \e[0;#{@fail_color}m%6dF \e[0;#{@error_color}m%6dE\e[0m " % data else "\r[%s | %s | %s] %6dF %6dE " % data end end |
#register ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 26 def register super MSpec.register :start, self MSpec.register :unload, self MSpec.unregister :before, self end |
#start ⇒ Object
Callback for the MSpec :start event. Stores the total number of files that will be processed.
82 83 84 85 86 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 82 def start @total = MSpec.retrieve(:files).size compute_progress print progress_line end |
#unload ⇒ Object
Callback for the MSpec :unload event. Increments the number of files that have been run.
90 91 92 93 94 |
# File 'lib/extensions/mspec/mspec/runner/formatters/spinner.rb', line 90 def unload @loaded += 1 compute_progress print progress_line end |