Permalink
Browse files

Adds move command #18

Feature inspired by @langhorst's pull request.

Closes #18
  • Loading branch information...
1 parent 3f0bf35 commit c54899fe9cad82957cfa271d3be79d6e54de8237 @stephenmckinney stephenmckinney committed Dec 27, 2012
Showing with 44 additions and 1 deletion.
  1. +23 −1 bin/pomo
  2. +21 −0 lib/pomo/list.rb
View
@@ -194,12 +194,34 @@ command :remove do |c|
list.tasks -= [task]
say " - Removed #{task}"
end
- list.save
+ list.save
end
end
alias_command :rm, :remove
alias_command :clear, :remove, 'all'
+command :move do |c|
+ c.syntax = 'pomo move [from] [to]'
+ c.summary = 'Move a task'
+ c.description = 'Move a task to a different position in the current list'
+ c.example 'Move task 4 to position 2', 'pomo move 4 2'
+ c.example 'Move last task to first position', 'pomo move last first'
+
+ c.action do |args, options|
+ config = Pomo::Configuration.load
+ from = args.shift or raise('<from> is required')
+ to = args.shift or raise('<to> is required')
+ list.find(from) do |task|
+ list.find(to) do |other_task|
+ list.move(from, to)
+ say " - Moved #{task} from position #{from} to #{to}"
+ end
+ end
+ list.save
+ end
+end
+alias_command :mv, :move
+
# TODO remove view alias in 3.0 release
command :show do |c|
c.syntax = 'pomo show [task ...] [options]'
View
@@ -69,6 +69,8 @@ def find *args, &block
end
end
+ found.compact!
+
if block.arity == 2
found.each_with_index do |task, i|
yield task, i
@@ -96,6 +98,13 @@ def add task
alias :<< :add
##
+ # Move task at _from_ index to _to_ index (requres saving).
+
+ def move from, to
+ @tasks.insert(index(to), @tasks.delete_at(index(from)))
+ end
+
+ ##
# Save the list.
def save
@@ -113,5 +122,17 @@ def load
self
end
+ private
+
+ def index arg
+ case arg
+ when 'first'
+ 0
+ when 'last'
+ @tasks.size - 1
+ when /^(\d+)$/
+ $1.to_i
+ end
+ end
end
end

0 comments on commit c54899f

Please sign in to comment.