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/goot/cli.rb', line 10
def parse(argv)
  Slop.parse(argv, :help => true) do
    banner 'Usage: '
    on :i, :interactive, 'Put goot into interactive mode'
    command 'ls' do
      description 'List tasks, e.g. ls -l1'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do
        tab = '  '
        depths = {}
        tasklist = API.get_tasklist(get(:list).to_i)
        tasks = API.get_tasks(tasklist)
        puts tasklist.title.green
        tasks.each.with_index do |task, i|
          next if task.deleted
          status = task.status == 'completed' ? '✓' : '▢'
          depth = 1
          depth += depths[task.parent] if depths.include? task.parent
          depths[task.id] = depth
          puts "#{tab * depth} #{status} #{i}: #{task.title}"
          puts "#{tab * (depth + 1)} Notes: #{task.notes}".yellow if task.notes
        end
      end
    end
    command 's' do
      description 'Print a summary of all task lists'
      run do
        API.get_lists.each.with_index do |tasklist, i|
          puts "#{i} #{tasklist.title}"
        end
      end
    end
    command 't' do
      description 'Toggle a task, e.g. t 0 -l1'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do |_, args|
        tasklist = API.get_tasklist(get(:list).to_i)
        tasks = API.get_tasks(tasklist).map(&:to_hash)
                args.each do |index|
          task = tasks[index.to_i]
          task['status'] = task['completed'].nil? ? 'completed' : 'needsAction'
          task.delete 'completed'
          API.update_task(tasklist, task)
          toggle_children = lambda do |parent|
            children = tasks.select { |t| t['parent'] == parent['id'] }
            children.each do |t|
              t['status'] = task['status']
              t.delete 'completed'
              API.update_task(tasklist, t)
              toggle_children.call(t)
            end
          end
          toggle_children.call(task)
        end
      end
    end
    command 'c' do
      description 'Clear completed tasks'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do
        tasklist = API.get_tasklist(get(:list).to_i)
        API.clear_tasks(tasklist)
      end
    end
    command 'd' do
      description 'Delete a task'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do |_, args|
        tasklist = API.get_tasklist(get(:list).to_i)
        tasks = API.get_tasks(tasklist).map(&:to_hash)
                args.each do |index|
          API.delete_task(tasklist, tasks[index.to_i])
        end
      end
    end
    command 'm' do
      description 'Move a task within its list'
      on :a=, :after=,  'Move task after specified index'
      on :p=, :parent=, 'Make task a child of parent index'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do |_, args|
        tasklist = API.get_tasklist(get(:list).to_i)
        tasks = API.get_tasks(tasklist).map(&:to_hash)
        previous = tasks[get(:after).to_i] if !get(:after).nil?
        parent = tasks[get(:parent).to_i] if !get(:parent).nil?
        API.move_task(tasklist, tasks[args[0].to_i], previous, parent)
      end
    end
    command 'a' do
      description 'Add a task'
      on :a=, :after=,  'Move task after specified index'
      on :p=, :parent=, 'Make task a child of parent index'
      on :l=, :list=, 'Specify a tasklist', :default => 0
      run do |opts, args|
        tasklist = API.get_tasklist(get(:list).to_i)
        tasks = API.get_tasks(tasklist).map(&:to_hash)
        task = { :title => args[0] }
        previous = tasks[get(:after).to_i] if !get(:after).nil?
        parent = tasks[get(:parent).to_i] if !get(:parent).nil?
        API.insert_task(tasklist, task, previous, parent)
      end
    end
    command 'la' do       description 'Add a task list'
      run do |_, args|
        tasklist = { :title => args[0] }
        API.insert_tasklist(tasklist)
      end
    end
    command 'ld' do       description 'Delete a task list'
      run do |_, args|
        API.delete_tasklist(API.get_lists[args[0].to_i])
      end
    end
  end
end
     |