-
-
Save wycleffsean/dad79dedb5b4ddda1fb731f9001ae9d3 to your computer and use it in GitHub Desktop.
TODO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# vim: set filetype=ruby : | |
require 'date' | |
plan = File.read(File.expand_path('~/.plan')) | |
date = nil | |
indent = 0 | |
def color(string, *nums) | |
return nil if string.nil? | |
codes = nums.map{|num| "\e[#{num}m"}.join | |
"#{codes}#{string}\e[0m" | |
end | |
def emphasize(string) | |
color string, 32, 1 | |
end | |
def alert(string) | |
color string, 31 | |
end | |
def light(string) | |
color string, 36 | |
end | |
def deemphasize(string) | |
color string, '1;30' | |
end | |
class Task < Struct.new(:text, :date, :priority, :lineno) | |
# Order by priority, then natural order | |
def <=>(task) | |
if (res = task.priority <=> priority) == 0 | |
res = task.lineno <=> lineno | |
end | |
res | |
end | |
def to_s | |
"* %s %s %s\n%s" % [preamble, emphasize(title.chomp), suffix, body] | |
end | |
def title | |
text.each_line.first | |
end | |
def body | |
text.each_line.to_a[1..-1].join | |
end | |
def preamble | |
[ | |
priority > 0 ? alert('!' * priority) : nil, | |
].compact.join(' ') | |
end | |
def suffix | |
[ | |
light(date), | |
deemphasize(":#{lineno}"), | |
].compact.join(' ') | |
end | |
end | |
tasks = [] | |
plan.each_line.with_index(1) do |line, index| | |
case line | |
# Open Task | |
# e.g. " [ ] Some thing" | |
when /^(?<indent>\s*)\[\s*\]\s*(?<priority>\!*)\s*(?<task>.+)/ | |
taskname = $~[:task] | |
priority = $~[:priority].length | |
tasks << Task.new(taskname, date, priority, index) | |
indent = $~[:indent].length | |
when /^(?<indent>\s+)(?<text>.+)/ | |
len = $~[:indent].length | |
if len > indent && !tasks.last.nil? | |
tasks.last.text += "\n" + ' '*(len - indent) + $~[:text] | |
end | |
when /\d{4}-\d{1,2}-\d{1,2}/ # match 1999-11-31 style date | |
date = Date.parse($~.to_s) | |
#p date | |
#else | |
end | |
end | |
tasks.sort.each do |task| | |
puts task | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment