Class: Intent::Todo::Manager

Inherits:
Object
  • Object
show all
Includes:
Todo::Syntax
Defined in:
lib/intent/todo/manager.rb

Class Method Summary collapse

Class Method Details



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/intent/todo/manager.rb', line 152

def self.print_help(output)
  output.puts "usage: todo"
  output.puts
  output.puts "A set of tasks for managing a plain text todo list."
  output.puts
  output.puts "todo list       - list all items in the list"
  output.puts "todo add        - add a new task to the list"
  output.puts "todo focus      - find focus by randomly selecting a task"
  output.puts "todo projects   - list all project tags in the list"
  output.puts "todo contexts   - list all context tags in the list"
  output.puts "todo archive    - archive completed tasks in the nearest `done.txt`"
  output.puts "todo status     - show completion status for all projects"
  output.puts "todo sync       - synchronize local changes with remote git repo"
end

.run(args, output = STDOUT) ⇒ 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
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
# File 'lib/intent/todo/manager.rb', line 10

def self.run(args, output=STDOUT)
  if args.empty?
    print_help(output)
  else
    list = List.new(ENV['TODO_TXT'])
    case args.first.to_sym
    when :sync
      todo_path = File.dirname(ENV['TODO_TXT'])
      todo_file = File.basename(ENV['TODO_TXT'])
      git = Git.open(todo_path, :log => Logger.new(STDOUT))
      git.add(todo_file)
      git.commit("Update todo list [#{Time.new}]")
      git.push
    when :add
      output.print "add task: $ "
      input = STDIN.readline.chop
      list.unshift(Task.new("#{Date.today} #{input}")) if input
      list.save!
    when :edit
      exec("#{ENV['EDITOR']} #{ENV['TODO_TXT']}")
    when :list
      filtered_list = list.by_not_done

      unless args[1].nil?
        case args[1][0]
        when '@'
          filtered_list = filtered_list.by_context(args[1]).by_not_done
        when '+'
          filtered_list = filtered_list.by_project(args[1])
        end
      end

      filtered_list.by_not_done.each do |task|
        output.puts task
      end
    when :focus
      focused_list = list.by_not_done

      unless args[1].nil?
        case args[1][0]
        when '@'
          focused_list = focused_list.by_context(args[1])
        when '+'
          focused_list = focused_list.by_project(args[1])
        end
      end

      prioritised_list = focused_list.by_priority('A')
      if prioritised_list.any?
        output.puts prioritised_list.sample
      else
        if focused_list.any?
          output.puts focused_list.sample
        else
          output.puts "No tasks found."
        end
      end
    when :projects
      output.puts list.by_not_done.inject([]) { |p, t| p.concat t.projects }.uniq
    when :contexts
      output.puts list.by_not_done.inject([]) { |c, t| c.concat t.contexts }.uniq
    when :archive
      archive_path = File.dirname(ENV['TODO_TXT'])
      todo_file = ENV['TODO_TXT']
      done_file = "#{archive_path}/done.txt"
      backup_file = "#{archive_path}/.todo.#{Date.today.iso8601}.txt"

      FileUtils.copy(todo_file, backup_file)

      unless File.exists?(done_file)
        output.puts "Creating new `done.txt` in #{archive_path}."
        File.write(done_file, '')
      end

      done_list = ::Todo::List.new(done_file)

      list.by_done.each do |task|
        done_list.push(task)
      end

      File.open(done_file, "w") do |file|
        done_list.sort!.by_done.each do |task|
          file.puts(task)
        end
      end

      File.open(todo_file, "w") do |file|
        list.by_not_done.each do |task|
          file.puts(task)
        end
      end
    when :status
      pastel = Pastel.new
      percentage = true

      active_projects = File.read(ENV['PROJECTS_TXT']).lines.map(&:strip)

      project_names = list.inject([]) do |names, task|
        if (task.projects - active_projects) != task.projects
          names.concat(task.projects)
        else
          names
        end
      end.uniq

      projects = project_names.map do |project|
        high_priority = list.by_not_done.by_project(project).by_priority('A').size
        {
          name: project,
          done: list.by_done.by_project(project).size,
          not_done: list.by_not_done.by_project(project).size - high_priority,
          priority: high_priority
        }
      end

      pad_to = project_names.max { |a,b| a.length <=> b.length }.length + 1
      bar_to = (pad_to * 2).to_f

      projects.each do |project|
        total = project[:done] + project[:not_done] + project[:priority]

        if percentage
          done_ratio = bar_to / total * project[:done]
          not_done_ratio = bar_to / total * project[:not_done]
          priority_ratio = bar_to / total * project[:priority]
        else
          done_ratio = project[:done]
          not_done_ratio = project[:not_done]
          priority_ratio =  project[:priority]
        end

        print project[:name].ljust(pad_to), "|"

        done_ratio.round.times { print pastel.green("█") }
        not_done_ratio.round.times { print pastel.yellow("█") }
        priority_ratio.floor.times { print pastel.red("█") }
        print "\n"
      end
    end
  end
end