Class: GoogleTasks::CLI
- Inherits:
-
Thor
- Object
- Thor
- GoogleTasks::CLI
- Includes:
- Thor::Actions
- Defined in:
- lib/goot/cli.rb
Instance Method Summary collapse
- #add(*names) ⇒ Object
- #clear ⇒ Object
- #delete(*indices) ⇒ Object
-
#initialize(*args) ⇒ CLI
constructor
A new instance of CLI.
- #listadd(name) ⇒ Object
- #listdelete(index) ⇒ Object
- #ls ⇒ Object
- #move(index) ⇒ Object
- #summary ⇒ Object
- #toggle(*indices) ⇒ Object
Constructor Details
#initialize(*args) ⇒ CLI
Returns a new instance of CLI.
4 5 6 7 |
# File 'lib/goot/cli.rb', line 4 def initialize(*args) super @api = GoogleTasks::GoogleAPI.new end |
Instance Method Details
#add(*names) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/goot/cli.rb', line 110 def add(*names) tasklist = @api.get_tasklist([:tasklist]) tasks = @api.get_tasks(tasklist).map(&:to_hash) names.each do |name| task = { :title => name } previous = tasks[[:after].to_i] if ![:after].nil? parent = tasks[[:parent].to_i] if ![:parent].nil? @api.insert_task(tasklist, task, previous, parent) end invoke :ls, [], :tasklist => [:tasklist] end |
#clear ⇒ Object
70 71 72 73 74 75 |
# File 'lib/goot/cli.rb', line 70 def clear tasklist = @api.get_tasklist([:tasklist]) @api.clear_tasks(tasklist) invoke :ls, [], :tasklist => [:tasklist] end |
#delete(*indices) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/goot/cli.rb', line 79 def delete(*indices) tasklist = @api.get_tasklist([:tasklist]) tasks = @api.get_tasks(tasklist).map(&:to_hash) indices.each do |index| @api.delete_task(tasklist, tasks[index.to_i]) end invoke :ls, [], :tasklist => [:tasklist] end |
#listadd(name) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/goot/cli.rb', line 127 def listadd(name) tasklist = {:title => name} @api.insert_tasklist(tasklist) invoke :summary, [] end |
#listdelete(index) ⇒ Object
135 136 137 138 139 |
# File 'lib/goot/cli.rb', line 135 def listdelete(index) @api.delete_tasklist(@api.get_lists[index.to_i]) invoke :summary, [] end |
#ls ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/goot/cli.rb', line 11 def ls tab = ' ' depths = {} tasklist = @api.get_tasklist([:tasklist]) tasks = @api.get_tasks(tasklist) say 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 say "#{tab * depth} #{status} #{i}: #{task.title}" say "#{tab * (depth + 1)} Notes: #{task.notes}", :yellow if task.notes end end |
#move(index) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/goot/cli.rb', line 94 def move(index) tasklist = @api.get_tasklist([:tasklist]) tasks = @api.get_tasks(tasklist).map(&:to_hash) previous = tasks[[:after].to_i] if ![:after].nil? parent = tasks[[:parent].to_i] if ![:parent].nil? @api.move_task(tasklist, tasks[index.to_i], previous, parent) invoke :ls, [], :tasklist => [:tasklist] end |
#summary ⇒ Object
34 35 36 37 38 39 |
# File 'lib/goot/cli.rb', line 34 def summary @api.get_lists.each.with_index do |tasklist, i| tasks = @api.get_tasks(tasklist) say "#{i} #{tasklist.title} (#{tasks.size})" end end |
#toggle(*indices) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/goot/cli.rb', line 43 def toggle(*indices) tasklist = @api.get_tasklist([:tasklist]) tasks = @api.get_tasks(tasklist).map(&:to_hash) indices.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 invoke :ls, [], :tasklist => [:tasklist] end |