do_stuff 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/do_stuff/runner.rb +75 -71
  2. metadata +1 -1
@@ -12,66 +12,15 @@ module DoStuff
12
12
  abort "Error: Couldn't find #{dostuffrc}.\nPlease create it and put " +
13
13
  "the path to your todo.txt file in it." unless File.exists?(dostuffrc)
14
14
 
15
- todofile = File.expand_path(File.read(dostuffrc).chomp)
16
- FileUtils.mkdir_p(File.dirname(todofile))
17
- FileUtils.touch(todofile)
15
+ taskfile = File.expand_path(File.read(dostuffrc).chomp)
16
+ unless File.exists?(taskfile)
17
+ FileUtils.mkdir_p(File.dirname(taskfile))
18
+ FileUtils.touch(taskfile)
19
+ end
18
20
 
19
21
  opts = OptionParser.new do |opts|
20
22
  opts.on('-e [TASK NUM]') do |task_num|
21
- begin
22
- pre_todolist = Tasklist.new(todofile)
23
- rescue Tasklist::ParseError => e
24
- pre_error = e
25
- end
26
-
27
- run_editor(todofile, task_num)
28
-
29
- begin
30
- post_todolist = Tasklist.new(todofile)
31
- rescue Tasklist::ParseError => e
32
- post_error = e
33
- end
34
-
35
- if post_error
36
- if pre_error
37
- if pre_error.message == post_error.message
38
- puts "Syntax error unchanged by edit.\n#{pre_error.message}"
39
- else
40
- puts "Pre-edit syntax error: #{pre_error.message}"
41
- puts "Post-edit syntax error: #{post_error.message}"
42
- end
43
- else
44
- puts "New syntax error introduced.\n#{post_error.message}"
45
- end
46
- abort
47
- end
48
-
49
- if pre_error && !post_error
50
- puts "Syntax error fixed by edit."
51
- exit
52
- end
53
-
54
- # If there were no errors, compare the old todolist with the new
55
- # one, finding what was added, removed, and changed.
56
- added_keys = post_todolist.tasks.keys - pre_todolist.tasks.keys
57
- added_keys.each do |task_num|
58
- puts "Added ##{task_num}: #{post_todolist[task_num]}"
59
- end
60
-
61
- removed_keys = pre_todolist.tasks.keys - post_todolist.tasks.keys
62
- removed_keys.each do |task_num|
63
- puts "Erased ##{task_num}: #{pre_todolist[task_num]}"
64
- end
65
-
66
- old_keys = pre_todolist.tasks.keys & post_todolist.tasks.keys
67
- old_keys.each do |task_num|
68
- if pre_todolist[task_num] != post_todolist[task_num]
69
- puts "Changed ##{task_num}:"
70
- puts "#{RED}-#{pre_todolist[task_num]}#{RESET}"
71
- puts "#{GREEN}+#{post_todolist[task_num]}#{RESET}"
72
- end
73
- end
74
-
23
+ edit(taskfile, task_num)
75
24
  exit
76
25
  end
77
26
 
@@ -98,40 +47,95 @@ module DoStuff
98
47
  end
99
48
 
100
49
  begin
101
- todolist = Tasklist.new(todofile)
50
+ tasklist = Tasklist.new(taskfile)
102
51
  rescue Tasklist::ParseError => e
103
52
  abort e.message
104
53
  end
105
54
 
106
55
  if argv.length == 0
107
- todolist.tasks.sort.each do |num, task|
56
+ tasklist.tasks.sort.each do |num, task|
108
57
  puts "#{num}. #{task}"
109
58
  end
110
59
  elsif argv.length == 1 && argv[0] =~ /^\d+$/
111
60
  task_num = argv[0].to_i
112
- abort "There is no task ##{task_num}." unless todolist.tasks.key?(task_num)
113
- task = todolist[task_num]
114
- todolist.delete(task_num)
115
- todolist.write!
61
+ abort "There is no task ##{task_num}." unless tasklist.tasks.key?(task_num)
62
+ task = tasklist[task_num]
63
+ tasklist.delete(task_num)
64
+ tasklist.write!
116
65
  puts "Erased ##{task_num}: #{task}"
117
66
  else
118
67
  # If nothing else matches, treat the arguments as a task description.
119
68
  task = argv.join(' ')
120
- task_num = todolist.add(task)
121
- todolist.write!
69
+ task_num = tasklist.add(task)
70
+ tasklist.write!
122
71
  puts "Added ##{task_num}: #{task}"
123
72
  end
124
73
  end
125
74
 
126
- def self.run_editor(file, task_num)
127
- target_line = nil
75
+ def self.edit(file, task_num=nil)
76
+ begin
77
+ pre_tasklist = Tasklist.new(file)
78
+ rescue Tasklist::ParseError => e
79
+ pre_error = e
80
+ end
81
+
82
+ run_editor(file, task_num)
83
+
84
+ begin
85
+ post_tasklist = Tasklist.new(file)
86
+ rescue Tasklist::ParseError => e
87
+ post_error = e
88
+ end
89
+
90
+ if post_error
91
+ if pre_error
92
+ if pre_error.message == post_error.message
93
+ puts "Syntax error unchanged by edit.\n#{pre_error.message}"
94
+ else
95
+ puts "Pre-edit syntax error: #{pre_error.message}"
96
+ puts "Post-edit syntax error: #{post_error.message}"
97
+ end
98
+ else
99
+ puts "New syntax error introduced.\n#{post_error.message}"
100
+ end
101
+ return
102
+ end
103
+
104
+ if pre_error && !post_error
105
+ puts "Syntax error fixed by edit."
106
+ return
107
+ end
108
+
109
+ # If there were no errors, compare the old tasklist with the new
110
+ # one, finding what was added, removed, and changed.
111
+ added_keys = post_tasklist.tasks.keys - pre_tasklist.tasks.keys
112
+ added_keys.each do |task_num|
113
+ puts "Added ##{task_num}: #{post_tasklist[task_num]}"
114
+ end
115
+
116
+ removed_keys = pre_tasklist.tasks.keys - post_tasklist.tasks.keys
117
+ removed_keys.each do |task_num|
118
+ puts "Erased ##{task_num}: #{pre_tasklist[task_num]}"
119
+ end
128
120
 
129
- File.readlines(file).each_with_index do |line, line_num|
130
- target_line = line_num + 1 if line.start_with?("#{task_num}. ")
121
+ old_keys = pre_tasklist.tasks.keys & post_tasklist.tasks.keys
122
+ old_keys.each do |task_num|
123
+ if pre_tasklist[task_num] != post_tasklist[task_num]
124
+ puts "Changed ##{task_num}:"
125
+ puts "#{RED}-#{pre_tasklist[task_num]}#{RESET}"
126
+ puts "#{GREEN}+#{post_tasklist[task_num]}#{RESET}"
127
+ end
131
128
  end
129
+ end
130
+
131
+ def self.run_editor(file, task_num=nil)
132
+ if task_num
133
+ target = File.readlines(file).find_index do |line|
134
+ line.start_with?("#{task_num}. ")
135
+ end
132
136
 
133
- if target_line
134
- system(ENV['EDITOR'], file, "+#{target_line}")
137
+ abort "Could not find task ##{task_num}." unless target
138
+ system(ENV['EDITOR'], file, "+#{target + 1}")
135
139
  else
136
140
  system(ENV['EDITOR'], file)
137
141
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: do_stuff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: