ruco 0.0.32 → 0.0.33

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -10,6 +10,7 @@ Features:
10
10
  - find / go to line / delete line / search & replace
11
11
  - configuration via `~/.ruco.rb`
12
12
  - cut, copy and paste -> Ctrl+x/c/v
13
+ - undo / redo
13
14
 
14
15
  Install
15
16
  =======
@@ -57,12 +58,16 @@ TIPS
57
58
 
58
59
  TODO
59
60
  =====
61
+ - a = replace all
62
+ - limit possible file size to e.g. 1MB (would hang/be too slow with insanely big files)
63
+ - find next (Alt+n)
60
64
  - add selection colors to forms in command_bar
61
65
  - session storage (stay at same line/column when reopening)
62
66
  - smart staying at end of line/column when changing line
63
67
  - warnings / messages
64
68
  - syntax highlighting
65
69
  - raise when binding to a unsupported key
70
+ - search history via up/down arrow
66
71
  - search options regex + case-sensitive
67
72
  - add auto-confirm to 'replace?' dialog -> type s == skip, no enter needed
68
73
  - 1.8: unicode support <-> already finished but unusable due to Curses (see encoding branch)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.32
1
+ 0.0.33
@@ -189,6 +189,9 @@ module Ruco
189
189
  end
190
190
  end
191
191
  end
192
+
193
+ action(:undo){ @editor.undo if @focused == @editor }
194
+ action(:redo){ @editor.redo if @focused == @editor }
192
195
  end
193
196
 
194
197
  def setup_keys
@@ -204,6 +207,8 @@ module Ruco
204
207
  bind :"Ctrl+x", :cut
205
208
  bind :"Ctrl+c", :copy
206
209
  bind :"Ctrl+v", :paste
210
+ bind :"Ctrl+z", :undo
211
+ bind :"Ctrl+y", :redo
207
212
  end
208
213
 
209
214
  def load_user_config
data/lib/ruco/editor.rb CHANGED
@@ -5,7 +5,7 @@ module Ruco
5
5
  private :text_area
6
6
  delegate :view, :color_mask, :cursor,
7
7
  :selecting, :selection, :text_in_selection, :reset,
8
- :move, :resize,
8
+ :move, :resize, :undo, :redo,
9
9
  :to => :text_area
10
10
 
11
11
  def initialize(file, options)
@@ -2,6 +2,26 @@ module Ruco
2
2
  # everything that does not belong to a text-area
3
3
  # but is needed for Ruco::Editor
4
4
  class EditorArea < TextArea
5
+ def initialize(*args)
6
+ super(*args)
7
+ @history = History.new(:state => state, :track => [:content], :entries => 100)
8
+ end
9
+
10
+ def undo
11
+ @history.undo
12
+ self.state = @history.state
13
+ end
14
+
15
+ def redo
16
+ @history.redo
17
+ self.state = @history.state
18
+ end
19
+
20
+ def view
21
+ @history.add(state)
22
+ super
23
+ end
24
+
5
25
  def delete_line
6
26
  lines.slice!(@line, 1)
7
27
  adjust_view
@@ -29,6 +49,27 @@ module Ruco
29
49
 
30
50
  private
31
51
 
52
+ def state
53
+ {
54
+ :content => content,
55
+ :position => position,
56
+ :screen_position => screen_position
57
+ }
58
+ end
59
+
60
+ def state=(data)
61
+ @selection = nil
62
+ @lines = data[:content].naive_split("\n")
63
+ @line, @column = data[:position]
64
+ @scrolled_lines, @scrolled_columns = data[:screen_position]
65
+ adjust_view
66
+ end
67
+
68
+ # TODO use this instead of instance variables
69
+ def screen_position
70
+ Position.new(@scrolled_lines, @scrolled_columns)
71
+ end
72
+
32
73
  def adjust_to_indentation(first, last=nil)
33
74
  last ||= first
34
75
  if selection
@@ -0,0 +1,48 @@
1
+ module Ruco
2
+ class History
3
+ def initialize(options)
4
+ @options = options
5
+ @stack = [@options.delete(:state)]
6
+ @position = 0
7
+ end
8
+
9
+ def state
10
+ @stack[@position]
11
+ end
12
+
13
+ def add(state)
14
+ return unless tracked_field_changes?(state)
15
+ remove_undone_states
16
+ @position += 1
17
+ @stack << state
18
+ limit_stack
19
+ end
20
+
21
+ def undo
22
+ @position = [@position - 1, 0].max
23
+ end
24
+
25
+ def redo
26
+ @position = [@position + 1, @stack.size - 1].min
27
+ end
28
+
29
+ private
30
+
31
+ def remove_undone_states
32
+ @stack.slice!(@position + 1, 9999999)
33
+ end
34
+
35
+ def tracked_field_changes?(data)
36
+ @options[:track].any? do |field|
37
+ state[field] != data[field]
38
+ end
39
+ end
40
+
41
+ def limit_stack
42
+ to_remove = @stack.size - @options[:entries]
43
+ return if to_remove < 1
44
+ @stack.slice!(0, to_remove)
45
+ @position -= to_remove
46
+ end
47
+ end
48
+ end
data/lib/ruco.rb CHANGED
@@ -8,6 +8,7 @@ require 'ruco/core_ext/range'
8
8
 
9
9
  require 'ruco/keyboard'
10
10
  require 'ruco/position'
11
+ require 'ruco/history'
11
12
 
12
13
  require 'ruco/editor'
13
14
  require 'ruco/status_bar'
data/ruco.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruco}
8
- s.version = "0.0.32"
8
+ s.version = "0.0.33"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-01-27}
12
+ s.date = %q{2011-01-28}
13
13
  s.default_executable = %q{ruco}
14
14
  s.email = %q{michael@grosser.it}
15
15
  s.executables = ["ruco"]
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/ruco/editor.rb",
32
32
  "lib/ruco/editor_area.rb",
33
33
  "lib/ruco/form.rb",
34
+ "lib/ruco/history.rb",
34
35
  "lib/ruco/keyboard.rb",
35
36
  "lib/ruco/position.rb",
36
37
  "lib/ruco/status_bar.rb",
@@ -44,6 +45,7 @@ Gem::Specification.new do |s|
44
45
  "spec/ruco/core_ext/string_spec.rb",
45
46
  "spec/ruco/editor_spec.rb",
46
47
  "spec/ruco/form_spec.rb",
48
+ "spec/ruco/history_spec.rb",
47
49
  "spec/ruco/keyboard_spec.rb",
48
50
  "spec/ruco/status_bar_spec.rb",
49
51
  "spec/ruco/text_area_spec.rb",
@@ -61,6 +63,7 @@ Gem::Specification.new do |s|
61
63
  "spec/ruco/core_ext/string_spec.rb",
62
64
  "spec/ruco/editor_spec.rb",
63
65
  "spec/ruco/form_spec.rb",
66
+ "spec/ruco/history_spec.rb",
64
67
  "spec/ruco/keyboard_spec.rb",
65
68
  "spec/ruco/status_bar_spec.rb",
66
69
  "spec/ruco/text_area_spec.rb",
@@ -578,6 +578,28 @@ describe Ruco::Editor do
578
578
  end
579
579
  end
580
580
 
581
+ describe 'history' do
582
+ it "can undo an action" do
583
+ write("a")
584
+ editor.insert("b")
585
+ editor.view # trigger save point
586
+ editor.insert("c")
587
+ editor.view # trigger save point
588
+ editor.undo
589
+ editor.view.should == "ba\n\n\n"
590
+ editor.cursor.should == [0,1]
591
+ end
592
+
593
+ it "removes selection on undo" do
594
+ editor.insert('a')
595
+ editor.selecting{move(:to, 1,1)}
596
+ editor.selection.should != nil
597
+ editor.view # trigger save point
598
+ editor.undo
599
+ editor.selection.should == nil
600
+ end
601
+ end
602
+
581
603
  describe :save do
582
604
  it 'stores the file' do
583
605
  write('xxx')
@@ -0,0 +1,77 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe Ruco::History do
4
+ let(:history){ Ruco::History.new(:state => {:x => 1}, :track => [:x], :entries => 3) }
5
+
6
+ it "knows its state" do
7
+ history.state.should == {:x => 1}
8
+ end
9
+
10
+ it "can add a state" do
11
+ history.add :x => 2
12
+ history.state.should == {:x => 2}
13
+ end
14
+
15
+ it "can undo a state" do
16
+ history.add :x => 2
17
+ history.undo
18
+ history.state.should == {:x => 1}
19
+ end
20
+
21
+ it "can undo-redo-undo a state" do
22
+ history.add :x => 2
23
+ history.undo
24
+ history.redo
25
+ history.state.should == {:x => 2}
26
+ end
27
+
28
+ it "cannot redo a modified stack" do
29
+ history.add :x => 2
30
+ history.undo
31
+ history.add :x => 3
32
+ history.redo
33
+ history.state.should == {:x => 3}
34
+ history.redo
35
+ history.state.should == {:x => 3}
36
+ end
37
+
38
+ it "cannot undo into nirvana" do
39
+ history.add :x => 2
40
+ history.undo
41
+ history.undo
42
+ history.state.should == {:x => 1}
43
+ end
44
+
45
+ it "cannot redo into nirvana" do
46
+ history.add :x => 2
47
+ history.redo
48
+ history.state.should == {:x => 2}
49
+ end
50
+
51
+ it "cannot undo unimportant changes" do
52
+ history.add(:x => 1, :y => 1)
53
+ history.undo
54
+ history.state.should == {:x => 1}
55
+ end
56
+
57
+ it "tracks unimportant fields when an important one changes" do
58
+ history.add(:x => 2, :y => 1)
59
+ history.add(:x => 3)
60
+ history.undo
61
+ history.state.should == {:x => 2, :y => 1}
62
+ history.undo
63
+ history.state.should == {:x => 1}
64
+ end
65
+
66
+ it "does not track more than x states" do
67
+ history.add(:x => 2)
68
+ history.add(:x => 3)
69
+ history.add(:x => 4)
70
+ history.add(:x => 5)
71
+ history.undo
72
+ history.undo
73
+ history.undo
74
+ history.undo
75
+ history.state.should == {:x => 3}
76
+ end
77
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 32
9
- version: 0.0.32
8
+ - 33
9
+ version: 0.0.33
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Grosser
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-27 00:00:00 +01:00
17
+ date: 2011-01-28 00:00:00 +01:00
18
18
  default_executable: ruco
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,6 +58,7 @@ files:
58
58
  - lib/ruco/editor.rb
59
59
  - lib/ruco/editor_area.rb
60
60
  - lib/ruco/form.rb
61
+ - lib/ruco/history.rb
61
62
  - lib/ruco/keyboard.rb
62
63
  - lib/ruco/position.rb
63
64
  - lib/ruco/status_bar.rb
@@ -71,6 +72,7 @@ files:
71
72
  - spec/ruco/core_ext/string_spec.rb
72
73
  - spec/ruco/editor_spec.rb
73
74
  - spec/ruco/form_spec.rb
75
+ - spec/ruco/history_spec.rb
74
76
  - spec/ruco/keyboard_spec.rb
75
77
  - spec/ruco/status_bar_spec.rb
76
78
  - spec/ruco/text_area_spec.rb
@@ -90,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
92
  requirements:
91
93
  - - ">="
92
94
  - !ruby/object:Gem::Version
93
- hash: -723463789
95
+ hash: -1049918031
94
96
  segments:
95
97
  - 0
96
98
  version: "0"
@@ -116,6 +118,7 @@ test_files:
116
118
  - spec/ruco/core_ext/string_spec.rb
117
119
  - spec/ruco/editor_spec.rb
118
120
  - spec/ruco/form_spec.rb
121
+ - spec/ruco/history_spec.rb
119
122
  - spec/ruco/keyboard_spec.rb
120
123
  - spec/ruco/status_bar_spec.rb
121
124
  - spec/ruco/text_area_spec.rb