dotfiles/taskwarrior/hooks/on-modify-allow-shorthands.rb
2024-11-07 16:25:41 -07:00

68 lines
1.6 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'json'
require 'chronic'
# First, figure out if we have tasks to deal with.
tasks = $stdin.filter { |line| line.start_with? '{' }
# If this is a creation, we'll only get one event. Count is as both the original and the new.
original_json_task, json_task = case tasks.length
when 2 then tasks
else
[tasks.first, tasks.first]
end
# Finally, actually handle the shorthands.
task = JSON.parse(json_task)
original_task = JSON.parse(original_json_task)
loop do
case task['description']
# Wait
when /w:([^ ]+)/
task['wait'] = Chronic.parse($1)
task['description'].sub!("w:#{$1}", "")
# Project
when /p:([^ ]+)/
task['project'] = $1
task['description'].sub!("p:#{$1}", "")
# Due
when /d:([^ ]+)/
task['due'] = Chronic.parse($1)
task['description'].sub!("d:#{$1}", "")
# Depends
when /dep:([^ ]+)/
task['depends'] = $1
task['description'].sub!("dep:#{$1}", "")
# Every
when /e:([^ ]+)/
task['every'] = $1
task['description'].sub!("e:#{$1}", "")
# RecurAfter
when /a:([^ ]+)/
task['recur_after'] = $1
task['description'].sub!("a:#{$1}", "")
# Scheduled
when /s:([^ ]+)/
task['scheduled'] = Chronic.parse($1)
task['description'].sub!("s:#{$1}", "")
# Once we're out of things to expand, start parsing, and emit.
else
raise StopIteration
end
end
# If we wound up with no description; e.g. because we were just given things to mod, use the original description.
task['description'] = original_task['description'] if task['description'].strip.empty?
print task.to_json