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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#!/usr/bin/env ruby
# notyet [-ace] [-f FILE] [FILTERS...] - a todo tracker
#
# notyet is in the public domain.
# To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
# has waived all copyright and related or neighboring rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
require 'optionparser'
require 'date'
TODAY = Date.today
class Entry < Struct.new(:depth, :state, :desc, :file, :line, :children)
def reindent(change)
self.depth += change
children.each { |c| c.reindent change }
self
end
def dump
unless $show_all
return if "xX".index(state)
return if count("xX") == count
end
if depth < 0
puts "#{desc.strip} [#{stat}]".lstrip unless $edit
else
stat = children.empty? ? "" : " [#{self.stat}]"
print "#{file}:#{line}\t" if $edit && depth >= 0
print "#{" " * depth}#{state} #{desc}#{stat}\n" if depth >= 0
end
children.each { |c| c.dump }
end
def stat
children.each { |c| c.stat }
@stat ||= "#{count("xX")}/#{count("-xX")}"
end
def count(s=nil)
if children.empty? && (s.nil? || s.index(state))
return 1
else
children.map { |c| c.count(s) }.sum
end
end
def sort
children.each { |c| c.sort }
children.sort_by! { |c| [c.state, c.desc] }
end
def propagate
unless children.empty?
children.each { |c| c.propagate }
if children.all? { |c| "X".index(c.state) }
self.state = "X"
elsif children.all? { |c| "xX".index(c.state) }
self.state = "x"
elsif children.all? { |c| "?xX".index(c.state) }
self.state = "?"
end
force(state) if "?xX".index(state)
if desc =~ /^(?:\(.*?\)\s*)?(\d\d\d\d-\d\d-\d\d)/
diff = "%+d" % (Date.parse($1) - TODAY)
self.desc = "#{$1} {#{diff}}#{$'}"
else
soonest = children.map { |c|
"-?".index(c.state) &&
c.desc[/^(?:\(.*?\)\s*)?(\d\d\d\d-\d\d-\d\d)/, 1]
}.compact.min
if soonest
diff = "%+d" % (Date.parse(soonest) - TODAY)
self.desc = "#{soonest} {#{diff}} #{desc}"
end
end
end
end
def force(state)
self.state = state unless "xX?".index(self.state)
children.each { |c| c.force(state) }
end
# remove two layers of parents
def splat
self.children = children.map { |c|
c.splat
if c.desc == "#splat"
if c.children.empty?
[]
else
sub = c.children.map { |cc| cc.children }.flatten
rel = sub.first.depth - c.depth
sub.map { |cc| cc.reindent(-rel) }
end
else
[c]
end
}.flatten(1)
end
def match1(word)
desc =~ %r{(^|\W)#{Regexp.quote word.to_s}($|\W)}
end
def match(word)
# need to match all non-@ words, and one of the mentioned @-words (if any)
tags, words = word.partition { |w| w =~ /^@/ }
(tags.empty? || tags.any? { |w| match1(w) }) && words.all? { |w| match1(w) }
end
def rmatch(word)
match(word) || children.any? { |c| c.rmatch(word) }
end
def filter(word)
if !match(word)
children.reject! { |c| !c.rmatch(word) }
children.each { |c| c.filter(word) }
end
end
end
def parse(io, filename=nil)
todos = [Entry.new(-1, "/", "", "", 0, [])]
filter = (io.stat.ftype != "file")
while line = io.gets
if filter && line =~ /^(.*):(\d+)\t/
fname, lineno, line = $1, $2.to_i, $'
else
fname, lineno = filename, io.lineno
end
if line =~ /\A(?:\s*(?:#|\/\/)\s)?(\s*)([-xX?])\s+(.*)/
i, state, desc = $1.size, $2, $3
while i <= todos.last.depth
todos.pop
end
children = []
if desc =~ /^#exec\s+(.+)/
Dir.chdir(File.dirname(fname)) {
begin
IO.popen($1) { |f|
sub = parse(f, $1)
sub.reindent(i+2)
children = sub.children
}
rescue SystemCallError => e
children = [Entry.new(i+2, "?", "ERROR: #{e}", fname, lineno, [])]
end
}
next if children.empty?
elsif desc =~ /^#include\s+(\S+)/
n = File.expand_path($1, File.dirname(fname))
begin
File.open(n) { |f|
sub = parse(f, $1)
sub.reindent(i+2)
children = sub.children
}
rescue SystemCallError => e
children = [Entry.new(i+2, "?", "ERROR: #{e}", fname, lineno, [])]
end
next if children.empty?
elsif desc =~ /^#includeall\s+(\S+)/
Dir.glob(File.expand_path($1, File.dirname(fname))) { |file|
File.open(file) { |f|
sub = parse(f, $1)
sub.reindent(i+4)
children << Entry.new(i+2, state, "#include #{file}", fname, lineno, sub.children)
}
}
next if children.empty?
end
e = Entry.new(i, state, desc, fname, lineno, children)
todos.last.children << e
todos << e
end
end
todos.first
end
files = []
filter = []
$show_all = $edit = false
count_only = false
opt_parser = OptionParser.new { |opts|
opts.banner = "Usage: notyet [-ace] [-f FILE] [FILTERS...]"
opts.summary_width = 9
opts.on("-a", "show all tasks (default: open tasks only)") { $show_all = true }
opts.on("-c", "only show total count") { count_only = true }
opts.on("-f FILE", "read tasks from FILE (default ~/.notyet)") { |f| files << f }
opts.on("-e", "edit tasks in vim") { $edit = true }
}.parse! ARGV
files = [File.expand_path("~/.notyet")] if files.empty?
t = nil
if files.size > 1
t = Entry.new(-1, "/", "", "", 0, [])
end
files.each { |file|
file = "/dev/stdin" if file == "-"
File.open(file) { |f|
d = parse(f, file)
if t
d.state = "-"
d.desc = file
d.reindent(2)
d.depth = 0
t.children << d
else
t = d
end
}
}
t.splat
t.propagate
t.sort
t.stat
t.filter(ARGV)
if count_only
puts "#{t.count("xX")}/#{t.count("-xX")}"
exit
end
if t.children.empty? && !ARGV.empty?
exit 1
end
if $edit && STDOUT.tty?
rd, wr = IO.pipe
$stdout = wr
Process.spawn('vim',
'+nmap <buffer><CR> ^gF',
'+nmap <buffer><TAB> <CR>',
'+nnoremap <buffer>o ^gFo- |' \
'nnoremap <buffer>x ^gF^rx | nnoremap <buffer>X ^gF^rX |' \
'nnoremap <buffer>- ^gF^r- | nnoremap <buffer>? ^gF^r?',
'+setl nomod noma nowrap conceallevel=3 concealcursor=nc',
'+syn match Conceal /^.\{-}\t/ conceal',
'+syn match Comment /\t\s*[xX].*/lc=1',
'+hi Comment cterm=NONE ctermfg=darkgrey ctermbg=NONE guifg=#777777 guibg=NONE',
'-',
0 => rd)
t.dump
$stdout.close
Process.wait
exit $?.exitstatus
else
t.dump
end
|