class Goot::CLI

Public Class Methods

new() click to toggle source
# File lib/goot/cli.rb, line 2
def initialize
  parse ARGV
  exit unless ARGV.include? '-i' or ARGV.include? '--interactive'
  while argv = Readline.readline('> ', true)
    parse Shellwords.split(argv)
  end
end

Public Instance Methods

parse(argv) click to toggle source
# 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)

        # TODO: check to make sure all args are ints
        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)

        # TODO: check to make sure all args are ints
        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 # (name)
      description 'Add a task list'
      run do |_, args|
        tasklist = { :title => args[0] }
        API.insert_tasklist(tasklist)
      end
    end

    command 'ld' do # (index)
      description 'Delete a task list'
      run do |_, args|
        API.delete_tasklist(API.get_lists[args[0].to_i])
      end
    end
  end
end