Class: RubyCurses::SimpleUndo

Inherits:
AbstractUndo show all
Defined in:
lib/rbcurse/experimental/widgets/undomanager.rb

Overview

An implementation of AbstractUndo for textarea. Very basic.

Instance Method Summary collapse

Methods inherited from AbstractUndo

#add_edit, #redo, #undo

Constructor Details

#initialize(_source) ⇒ SimpleUndo

Returns a new instance of SimpleUndo.



110
111
112
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 110

def initialize _source
  super
end

Instance Method Details

#perform_redo(act) ⇒ Object

this has to be bound in source



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 153

def perform_redo act
  row = act.row
  col = act.index0
  $log.debug " REDO processing #{act} "
  case act.type
  when :INSERT
    @source.list[row].insert(col, act.text)
  when :DELETE
    row = act.row
    col = act.index0
    howmany = act.index1 - col
    @source.list[row].slice!(col,howmany)
  when :DELETE_LINE
    #$log.debug " UNDO redo got deleteline #{row} "
    #@source.list.delete_at row
    # changed on 2010-05-23 12:21 since was not handling mult delete
    case act.text
    when Array
      act.text.size.times { @source.list.delete_at row }
    when String
      @source.list.delete_at row
    end
  when :INSERT_LINE
    case act.text
    when Array
      index = row
      act.text.each_with_index{|r,i| @source.list.insert index+i, r}
    when String
      @source.list.insert row, act.text
    end
  else
    $log.warn "unhandled change type #{act.type} "
  end
end

#perform_undo(act) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 117

def perform_undo act
  row = act.row
  col = act.index0
  $log.debug " processing #{act} "
  case act.type
  when :INSERT
    howmany = act.index1 - col
    @source.list[row].slice!(col,howmany)
  when :DELETE
    $log.debug " UNDO processing DELETE #{col}, (#{act.text})  "
    @source.list[row].insert(col, act.text.chomp)
  when :DELETE_LINE
    $log.debug " UNDO inside delete-line #{row} "
    #@source.list.insert(row, act.text) # insert a blank line, since one was deleted
    case act.text
    when Array
      index = row
      act.text.each_with_index{|r,i| @source.list.insert index+i, r}
    when String
      @source.list.insert row, act.text
    end
  when :INSERT_LINE
    $log.debug " UNDO inside insert-line #{row} "
    case act.text
    when Array
      act.text.size.times { @source.list.delete_at row }
    when String
      @source.list.delete_at row
    end
  else
    $log.warn "unhandled change type #{act.type} "
  end
  @source.repaint_required true
  @reject_update = false
end

#source(_source) ⇒ Object



113
114
115
116
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 113

def source(_source)
  super
  _source.bind(:CHANGE){|eve| add_edit(eve) }
end