Module: DoStuff::Runner
- Defined in:
- lib/do_stuff/runner.rb
Constant Summary collapse
- RED =
"\033[31;1m"- GREEN =
"\033[32;1m"- RESET =
"\033[m"
Class Method Summary collapse
- .edit(file, task_num = nil) ⇒ Object
- .execute(*argv) ⇒ Object
- .run_editor(file, task_num = nil) ⇒ Object
- .usage ⇒ Object
Class Method Details
.edit(file, task_num = nil) ⇒ Object
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 |
# File 'lib/do_stuff/runner.rb', line 98 def self.edit(file, task_num=nil) begin pre_tasklist = Tasklist.new(file) rescue Tasklist::ParseError => e pre_error = e end run_editor(file, task_num) begin post_tasklist = Tasklist.new(file) rescue Tasklist::ParseError => e post_error = e end if post_error if pre_error if pre_error. == post_error. 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 return end if pre_error && !post_error puts "Syntax error fixed by edit." return end # If there were no errors, compare the old tasklist with the new # one, finding what was added, removed, and changed. added_keys = post_tasklist.tasks.keys - pre_tasklist.tasks.keys added_keys.each do |task_num| puts "Added ##{task_num}: #{post_tasklist[task_num]}" end removed_keys = pre_tasklist.tasks.keys - post_tasklist.tasks.keys removed_keys.each do |task_num| puts "Erased ##{task_num}: #{pre_tasklist[task_num]}" end old_keys = pre_tasklist.tasks.keys & post_tasklist.tasks.keys old_keys.each do |task_num| if pre_tasklist[task_num] != post_tasklist[task_num] puts "Changed ##{task_num}:" puts "#{RED}-#{pre_tasklist[task_num]}#{RESET}" puts "#{GREEN}+#{post_tasklist[task_num]}#{RESET}" end end end |
.execute(*argv) ⇒ Object
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 |
# 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) # This will be set by options if we are to change the text of a task. edit_target = nil taskfile = File.(File.read(dostuffrc).chomp) unless File.exists?(taskfile) FileUtils.mkdir_p(File.dirname(taskfile)) FileUtils.touch(taskfile) end opts = OptionParser.new do |opts| opts.on('-e [TASK NUM]') do |task_num| if argv.empty? edit(taskfile, task_num) exit else edit_target = task_num end 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. end begin tasklist = Tasklist.new(taskfile) rescue Tasklist::ParseError => e abort e. end if edit_target before_text = tasklist[edit_target] tasklist[edit_target] = argv.join(' ') tasklist.write! if !before_text puts "Added ##{edit_target}: #{tasklist[edit_target]}" else puts "Changed ##{edit_target}:" puts "#{RED}-#{before_text}#{RESET}" puts "#{GREEN}+#{tasklist[edit_target]}#{RESET}" end exit end if argv.length == 0 tasklist.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 tasklist.tasks.key?(task_num) task = tasklist[task_num] tasklist.delete(task_num) tasklist.write! puts "Erased ##{task_num}: #{task}" else # If nothing else matches, treat the arguments as a task description. task = argv.join(' ') task_num = tasklist.add(task) tasklist.write! puts "Added ##{task_num}: #{task}" end end |
.run_editor(file, task_num = nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/do_stuff/runner.rb', line 154 def self.run_editor(file, task_num=nil) editor = ENV['EDITOR'] unless editor $stderr.puts "The EDITOR environment variable isn't set (defaulting to vim)." editor = 'vim' end if task_num target = File.readlines(file).find_index do |line| line.start_with?("#{task_num}. ") end abort "Could not find task ##{task_num}." unless target system(editor, file, "+#{target + 1}") else system(editor, file) end end |
.usage ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/do_stuff/runner.rb', line 174 def self.usage program = File.basename($0) print "usage: \#{program} list unfinished tasks\n \#{program} <task desc> add a new task\n \#{program} <task num> erase task\n \#{program} -e [task num] edit task file [and jump to given task]\n \#{program} -e<task num> <text> replace task with given text\n \#{program} -h, --help show this message\n EOS\n\n if defined?(::DoStuff::Standalone)\n print <<-EOS\n \#{program} --standalone FILE generate a standalone version of do_stuff\n EOS\n end\nend\n" |