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
|
# File 'lib/do_stuff/runner.rb', line 10
def self.execute(*argv)
dostuffrc = ENV['HOME'] + '/.do_stuffrc'
abort "Error: Couldn't find #{dostuffrc}.\nPlease create it and put " +
"the path to your todo.txt file in it." unless File.exists?(dostuffrc)
todofile = File.expand_path(File.read(dostuffrc).chomp)
FileUtils.mkdir_p(File.dirname(todofile))
FileUtils.touch(todofile)
opts = OptionParser.new do |opts|
opts.on('-e [TASK NUM]') do |task_num|
begin
pre_todolist = Tasklist.new(todofile)
rescue Tasklist::ParseError => e
pre_error = e
end
run_editor(todofile, task_num)
begin
post_todolist = Tasklist.new(todofile)
rescue Tasklist::ParseError => e
post_error = e
end
if post_error
if pre_error
if pre_error.message == post_error.message
puts "Syntax error unchanged by edit.\n#{pre_error.message}"
else
puts "Pre-edit syntax error: #{pre_error.message}"
puts "Post-edit syntax error: #{post_error.message}"
end
else
puts "New syntax error introduced.\n#{post_error.message}"
end
abort
end
if pre_error && !post_error
puts "Syntax error fixed by edit."
exit
end
added_keys = post_todolist.tasks.keys - pre_todolist.tasks.keys
added_keys.each do |task_num|
puts "Added ##{task_num}: #{post_todolist[task_num]}"
end
removed_keys = pre_todolist.tasks.keys - post_todolist.tasks.keys
removed_keys.each do |task_num|
puts "Erased ##{task_num}: #{pre_todolist[task_num]}"
end
old_keys = pre_todolist.tasks.keys & post_todolist.tasks.keys
old_keys.each do |task_num|
if pre_todolist[task_num] != post_todolist[task_num]
puts "Changed ##{task_num}:"
puts "#{RED}-#{pre_todolist[task_num]}#{RESET}"
puts "#{GREEN}+#{post_todolist[task_num]}#{RESET}"
end
end
exit
end
opts.on('--standalone FILE') do |file|
if defined?(::DoStuff::Standalone)
Standalone.save(file)
puts "#{file} generated successfully! Have fun doing stuff."
exit
else
abort "You're already using a standalone do_stuff script."
end
end
opts.on('-h', '--help') do
usage
exit
end
end
begin
opts.parse!(argv)
rescue OptionParser::ParseError => e
abort e.message
end
begin
todolist = Tasklist.new(todofile)
rescue Tasklist::ParseError => e
abort e.message
end
if argv.length == 0
todolist.tasks.sort.each do |num, task|
puts "#{num}. #{task}"
end
elsif argv.length == 1 && argv[0] =~ /^\d+$/
task_num = argv[0].to_i
abort "There is no task ##{task_num}." unless todolist.tasks.key?(task_num)
task = todolist[task_num]
todolist.delete(task_num)
todolist.write!
puts "Erased ##{task_num}: #{task}"
else
task = argv.join(' ')
task_num = todolist.add(task)
todolist.write!
puts "Added ##{task_num}: #{task}"
end
end
|