bin/task-loop
#!/usr/bin/env ruby
# frozen_string_literal: true
# This is WIP.
require 'pomodoro/config'
require 'pomodoro/formats/today'
require 'filewatcher'
require 'rufus-scheduler'
require 'refined-refinements/colours'
require 'refined-refinements/homepath'
using RR::ColouredTerminal
def debug(message)
warn message
end
def set_schedules(today_list_path)
tasks = Pomodoro::Formats::Today.parse(File.new(today_list_path, encoding: 'utf-8'))
scheduler = Rufus::Scheduler.new
scheduled_tasks = tasks.each_task.select do |task|
task.fixed_start_time && task.fixed_start_time >= Hour.now
end
scheduled_tasks.each do |scheduled_task|
debug "<bold>~</bold> Scheduling <green>#{scheduled_task.body}</green> for <yellow>#{scheduled_task.fixed_start_time}</yellow>."
scheduler.at(scheduled_task.fixed_start_time.to_s) do
puts "<bold>~</bold> [<cyan>#{Hour.now}</cyan>] Task <green>#{scheduled_task.body}</green> is due."
system(%(osascript -e 'display notification "#{scheduled_task.body} is due." with title "Now task manager" sound name "Morse"'))
end
end
tasks.with_prev_and_next.each do |prev_time_frame, time_frame, next_time_frame|
prev_time_frame_end_time = prev_time_frame ? prev_time_frame.end_time : Hour.parse('0:00')
next_time_frame_start_time = next_time_frame ? next_time_frame.start_time : Hour.parse('23:59')
begin
start_time = time_frame.start_time || prev_time_frame_end_time
if start_time >= Hour.now
debug "<bold>~</bold> Scheduling <green>#{time_frame.name}</green> <magenta>start</magenta> notification for <yellow>#{start_time}</yellow>."
scheduler.at(start_time.to_s) do
puts "<bold>~</bold> [<cyan>#{Hour.now}</cyan>] Time frame <green>#{time_frame.name}</green> has started."
system(%(osascript -e 'display notification "#{time_frame.name} has started." with title "#{time_frame.header}" sound name "Morse"'))
end
end
rescue StandardError => error
warn "<bold>~</bold> #{error.class} occured on #{time_frame.name}"
end
begin
end_time = time_frame.end_time || next_time_frame_start_time
if end_time >= Hour.now
debug "<bold>~</bold> Scheduling <blue.bold>#{time_frame.name}</blue.bold> <magenta>end</magenta> notification for <yellow>#{end_time}</yellow>."
scheduler.at(end_time.to_s) do
puts "<bold>~</bold> [<cyan>#{Hour.now}</cyan>] Time frame <blue.bold>#{time_frame.name}</blue.bold> has ended."
system(%(osascript -e 'display notification "#{time_frame.name} has ended." with title "Now task manager" sound name "Pop"'))
end
end
rescue StandardError => error
warn "<bold>~</bold> <red>#{error.class}</red> occured on <red>#{time_frame.name}</red>."
end
end
scheduler
end
today_list_path = RR::Homepath.new(Pomodoro::Config.new.today_path)
puts "<bold>~</bold> Watching <bright_black>#{today_list_path}</bright_black>."
set_schedules(today_list_path.expand)
Filewatcher.new([today_list_path.expand]).watch do |filename, event|
puts "\n~ File has changed.\n\n"
$scheduler = set_schedules(today_list_path.expand) # We have to keep the variable, otherwise the scheduler gets lost.
rescue Parslet::ParseFailed => error
warn "<bold>~</bold> <red>Parsing has failed.</red>"
end