]> git.cworth.org Git - sup/blob - lib/sup/undo.rb
Merge branch 'scanning-speedups' into next
[sup] / lib / sup / undo.rb
1 module Redwood
2
3 ## Implements a single undo list for the Sup instance
4 ##
5 ## The basic idea is to keep a list of lambdas to undo
6 ## things. When an action is called (such as 'archive'),
7 ## a lambda is registered with UndoManager that will
8 ## undo the archival action
9
10 class UndoManager
11   include Singleton
12
13   def initialize
14     @@actionlist = []
15     self.class.i_am_the_instance self
16   end
17
18   def register desc, actions
19     actions = [actions] unless actions.is_a?Array
20     raise StandardError, "when would I need to undo 'nothing?'" unless actions.length > 0
21     Redwood::log "registering #{actions.length} actions: #{desc}"
22     @@actionlist.push({:desc => desc, :actions => actions})
23   end
24
25   def undo
26     unless @@actionlist.length == 0 then
27       actionset = @@actionlist.pop
28       Redwood::log "undoing #{actionset[:desc]}..."
29       actionset[:actions].each{|action|
30         action.call
31       }
32       BufferManager.flash "undid #{actionset[:desc]}"
33     else
34       BufferManager.flash "nothing more to undo"
35     end
36   end
37
38   def clear
39     @@actionlist = []
40   end
41 end
42 end