Class: Thyme::Timer

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Timer

Returns a new instance of Timer.



3
4
5
6
7
# File 'lib/thyme/timer.rb', line 3

def initialize(config)
  @config = config
  @format = Format.new(config)
  @tmux = Tmux.new(config)
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thyme/timer.rb', line 13

def run
  # pause/unpause timer if it's already running
  send_signal('USR1') and return if File.exists?(Config::PID_FILE)

  begin
    File.open(Config::PID_FILE, "w") { |f| f.print(Process.pid) }
    @tmux.open
    if @config.repeat == 1
      run_single
    else
      while @config.repeat_index <= @config.repeat || @config.repeat == 0
        @config.break = false
        run_single
        if @config.repeat_index < @config.repeat || @config.repeat == 0
          @config.break = true
          run_single
        end
        @config.repeat_index += 1
      end
    end
  rescue Thyme::StopTimer
    # stop signal received
  ensure
    @tmux.close
    File.delete(Config::PID_FILE) if File.exists?(Config::PID_FILE)
  end
end

#stopObject



9
10
11
# File 'lib/thyme/timer.rb', line 9

def stop
  send_signal('TERM')
end