Sha256: 14d3a1170bb04d1dffe13c502c23f9072347c787f292a7964c1dc3557a429f4b

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'daemons'

module TomatoHarvest
  class Timer
    PID_DIR = '~'
    PID_NAME = '.toma'

    def self.start(task_id, options = {})
      new(task_id, options).start
    end

    def self.stop
      if monitor = Daemons::Monitor.find(File.expand_path(DIR), APP_NAME)
        monitor.stop
        true
      end
    end

    def initialize(task_id, options = {})
      @minutes    = options[:minutes]
      @time_entry = options[:time_entry]
      @notifier   = Notifier.new
      @list       = List.new
      @task       = @list.find(task_id)
      @timer      = 0
      @tmux       = Tmux.new
    end

    def start
      if Daemons.daemonize(app_name: PID_NAME, dir: PID_DIR, dir_mode: :normal)
        at_exit { save_and_log }
        run_timer
      else
        run_timer
        save_and_log
      end
    end

    private

    def run_timer
      @notifier.notify "Pomodoro started for #{@minutes} minutes", :subtitle => @task.name

      (@minutes * 60).times do |i|
        sleep 1
        @timer += 1
        @tmux.update(@timer)
      end
    end

    def save_and_log
      @task.log_pomodoro(@timer)
      @list.save
      @time_entry.log(@timer) if @time_entry
      @notifier.notify "Pomodoro finished", :subtitle => "Pomodoro finished!"
      @tmux.update(0)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tomatoharvest-0.0.1 lib/tomatoharvest/timer.rb