Class: SpinnerFormatter

Inherits:
DottedFormatter show all
Defined in:
lib/mspec/runner/formatters/spinner.rb

Constant Summary collapse

Spins =
%w!| / - \\!
HOUR =
3600
MIN =
60

Instance Attribute Summary collapse

Attributes inherited from DottedFormatter

#exceptions, #tally, #timer

Instance Method Summary collapse

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/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

#lengthObject

Returns the value of attribute length


5
6
7
# File 'lib/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 and progress bar.


99
100
101
# File 'lib/mspec/runner/formatters/spinner.rb', line 99

def after(state)
  print progress_line
end

#clear_progress_lineObject


70
71
72
# File 'lib/mspec/runner/formatters/spinner.rb', line 70

def clear_progress_line
  print "\r#{' '*progress_line.length}"
end

#etrObject


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mspec/runner/formatters/spinner.rb', line 39

def etr
  return "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

  "%02d:%02d:%02d" % [hour, min, sec]
end

#exception(exception) ⇒ Object

Callback for the MSpec :exception event. Changes the color used to display the tally of errors and failures


88
89
90
91
92
93
94
95
# File 'lib/mspec/runner/formatters/spinner.rb', line 88

def exception(exception)
  super
  @fail_color =  "31" if exception.failure?
  @error_color = "33" unless exception.failure?

  clear_progress_line
  print_exception(exception, @count)
end

#finishObject


103
104
105
106
107
# File 'lib/mspec/runner/formatters/spinner.rb', line 103

def finish
  # We already printed the exceptions
  @exceptions = []
  super
end

#loadObject

Callback for the MSpec :load event. Increments the number of files that have been loaded.


82
83
84
# File 'lib/mspec/runner/formatters/spinner.rb', line 82

def load
  @loaded += 1
end

#percentageObject


52
53
54
55
56
57
58
# File 'lib/mspec/runner/formatters/spinner.rb', line 52

def percentage
  @percent = @loaded * 100 / @total
  bar = ("=" * (@percent / @ratio)).ljust @length
  label = "%d%%" % @percent
  bar[@position, label.size] = label
  bar
end

#progress_lineObject


60
61
62
63
64
65
66
67
68
# File 'lib/mspec/runner/formatters/spinner.rb', line 60

def progress_line
  @which = (@which + 1) % Spins.size
  data = [Spins[@which], percentage, 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

#registerObject


26
27
28
29
30
31
# File 'lib/mspec/runner/formatters/spinner.rb', line 26

def register
  super

  MSpec.register :start, self
  MSpec.register :load, self
end

#startObject

Callback for the MSpec :start event. Stores the total number of files that will be processed.


76
77
78
# File 'lib/mspec/runner/formatters/spinner.rb', line 76

def start
  @total = MSpec.retrieve(:files).size
end