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

Class Method Details

.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
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

      # If there were no errors, compare the old todolist with the new
      # one, finding what was added, removed, and changed.
      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
    # If nothing else matches, treat the arguments as a task description.
    task = argv.join(' ')
    task_num = todolist.add(task)
    todolist.write!
    puts "Added ##{task_num}: #{task}"
  end
end

.run_editor(file, task_num) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/do_stuff/runner.rb', line 126

def self.run_editor(file, task_num)
  target_line = nil

  File.readlines(file).each_with_index do |line, line_num|
    target_line = line_num + 1 if line.start_with?("#{task_num}. ")
  end

  if target_line
    system(ENV['EDITOR'], file, "+#{target_line}")
  else
    system(ENV['EDITOR'], file)
  end
end

.usageObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/do_stuff/runner.rb', line 140

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} -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"