Class: CommandT::ProgressReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/command-t/progress_reporter.rb

Overview

Simple class for displaying scan progress to the user.

The active scanner calls the ‘#update` method with a `count` to inform it of progress, the reporter updates the UI and then returns a suggested count at which to invoke `#update` again in the future (the suggested count is based on a heuristic that seeks to update the UI about 5 times per second).

Constant Summary collapse

SPINNER =
%w[^ > v <]

Instance Method Summary collapse

Constructor Details

#initializeProgressReporter

Returns a new instance of ProgressReporter.



14
15
16
# File 'lib/command-t/progress_reporter.rb', line 14

def initialize
  @spinner ||= SPINNER.first
end

Instance Method Details

#update(count) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/command-t/progress_reporter.rb', line 18

def update(count)
  @spinner = SPINNER[(SPINNER.index(@spinner) + 1) % SPINNER.length]

  ::VIM::command "echon '#{@spinner}  #{count}'"
  ::VIM::command 'redraw'

  # Aim for 5 updates per second.
  now = Time.now.to_f
  if @last_time
    time_diff = now - @last_time
    count_diff = count - @last_count
    next_count = count + ((0.2 / time_diff) * count_diff).to_i
  else
    next_count = count + 100
  end
  @last_time = now
  @last_count = count
  next_count
end