-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganize-list
executable file
·94 lines (88 loc) · 2.21 KB
/
organize-list
1
2
3
4
5
6
7
8
9
10
11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env ruby
# returns contents of the first @due tag found
require 'chronic'
def due?(line)
line =~ /@due/ and !done?(line)
end
def parse_due_date(line)
Chronic.parse(line.match(/@due\((.+)\)/)[1])
end
def done?(line)
# find lines containing a @done tag
(line =~ /@done/) != nil
end
def sort_completed(list_of_todos)
completed = list_of_todos.select { |todo| done?(todo) }
completed.each do |t|
list_of_todos << list_of_todos.delete(t)
end
list_of_todos
end
def sort_due(list_of_todos)
due = list_of_todos.select { |todo| due?(todo) }
header = list_of_todos.first
list_of_todos.delete(header)
sorted_due = due.sort_by do |item|
parse_due_date(item)
end
[header].concat(sorted_due.concat(list_of_todos.reject { |t| sorted_due.include?(t) }))
end
def organize(list_of_todos)
sort_due(sort_completed(list_of_todos))
end
if ARGV.length != 0
# there has to be a much cleaner way to do this
todos = []
ARGV.each do |arg|
File.open(arg) do |f|
while line = f.gets
todos << line
end
end
organized = organize(todos)
File.open(arg, "w") do |f|
organized.each do |t|
f.puts(t)
end
end
end
end
def test_due
list = ["header",
"calc @due(September 19th)",
"more calc @due(September 18th)",
"not calc, just stuff",
"even more calc @due(September 17th)"]
desired_result = [
"header",
"even more calc @due(September 17th)",
"more calc @due(September 18th)",
"calc @due(September 19th)",
"not calc, just stuff"
]
puts list
puts "================"
puts organize(list)
puts "================"
puts organize(list) == desired_result
end
def test
list = ["not done",
"also not done",
"newly completed @done",
"third, not done",
"",
"done after gap @done",
"also done after gap @done"]
desired_result = ["not done",
"also not done",
"third, not done",
"",
"newly completed @done",
"done after gap @done",
"also done after gap @done"]
puts organize(list)
puts desired_result
puts organize(list) == desired_result
end
# test_due