Permalink
Browse files

Add ability to move a task's position (has bugs)

  • Loading branch information...
1 parent 3420aa1 commit cbaf2142c6a18b698ba07ed8c151d1f3ea72cc0e @langhorst committed May 3, 2011
Showing with 41 additions and 2 deletions.
  1. +14 −0 bin/pomo
  2. +27 −2 lib/pomo/list.rb
View
@@ -237,3 +237,17 @@ command :list do |c|
end
end
+command :move do |c|
+ c.syntax = 'pomo move [from] [to]'
+ c.description = 'Move a task to a different position'
+ 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|
+ list.find(args[0]) do |task, i|
+ list.move(args[0], args[1])
+ say " - moved #{task} from position #{args[0]} to #{args[1]}"
+ end
+ list.save
+ end
+end
+
View
@@ -69,6 +69,23 @@ def add task
@tasks << task
end
alias :<< :add
+
+ ##
+ ## Move _task_ from current position to new position (requres saving).
+
+ def move from, to
+ list = []
+ @tasks.each_with_index do |task, index|
+ next if index == position(from)
+ if index == position(to)
+ list << @tasks[position(from)]
+ list << task
+ else
+ list << task
+ end
+ end
+ @tasks = list
+ end
##
# Save the list.
@@ -87,6 +104,14 @@ def load
@tasks = YAML.load_file path
self
end
-
+
+ def position arg
+ return case arg.downcase
+ when 'first' then 0
+ when 'last' then @tasks.size-1
+ else arg.to_i
+ end
+ end
+ private :position
end
-end
+end

0 comments on commit cbaf214

Please sign in to comment.