]> git.cworth.org Git - sup/blob - lib/sup/undo.rb
fix garbaged text in textfield when using ncursesw
[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, &b
19     actions = [*actions.flatten]
20     actions << b if b
21     raise ArgumentError, "need at least one action" unless actions.length > 0
22     @@actionlist.push :desc => desc, :actions => actions
23   end
24
25   def undo
26     unless @@actionlist.empty?
27       actionset = @@actionlist.pop
28       actionset[:actions].each { |action| action.call }
29       BufferManager.flash "undid #{actionset[:desc]}"
30     else
31       BufferManager.flash "nothing more to undo!"
32     end
33   end
34
35   def clear
36     @@actionlist = []
37   end
38 end
39 end