Class: Eta::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/eta.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, *arguments) ⇒ Process

Returns a new instance of Process.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/eta.rb', line 42

def initialize name, *arguments
  @name = name
  @process = ChildProcess.build(*arguments)
  @progress_bar = ProgressBar.new
  @estimated_times_json = File.expand_path(File.join(File.dirname(__FILE__), "estimated-times.json"))
  @estimated_times = lambda {
    if File.exist? @estimated_times_json
      JSON.parse(File.read(@estimated_times_json))
    else
      {}
    end
  }.call
end

Instance Method Details

#startObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/eta.rb', line 56

def start
  @start_time = Time.now.to_i
  @process.start

  sleep 0.1 until @process.alive?

  while @process.alive?
    if estimated_time
      @progress_bar.draw elapsed, estimated_time
    else
      @progress_bar.draw_spinner
    end
    sleep 0.1
  end
  @estimated_times[@name] = Time.now.to_i - @start_time
  File.open(@estimated_times_json, "w") { |f| f.write(@estimated_times.to_json) }
end