ruco 0.0.41 → 0.0.42

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -11,6 +11,7 @@ Features:
11
11
  - configuration via `~/.ruco.rb`
12
12
  - cut, copy and paste -> Ctrl+x/c/v
13
13
  - undo / redo
14
+ - stays at last position when reopening a file
14
15
 
15
16
  Install
16
17
  =======
@@ -70,7 +71,6 @@ TODO
70
71
  - big warning when editing a not-writable file
71
72
  - find next (Alt+n)
72
73
  - add selection colors to forms in command_bar
73
- - session storage (stay at same line/column when reopening)
74
74
  - smart staying at column when changing line
75
75
  - warnings / messages
76
76
  - syntax highlighting
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.41
1
+ 0.0.42
data/lib/ruco.rb CHANGED
@@ -5,10 +5,12 @@ require 'ruco/core_ext/string'
5
5
  require 'ruco/core_ext/array'
6
6
  require 'ruco/core_ext/hash'
7
7
  require 'ruco/core_ext/range'
8
+ require 'ruco/core_ext/file'
8
9
 
9
10
  require 'ruco/keyboard'
10
11
  require 'ruco/position'
11
12
  require 'ruco/history'
13
+ require 'ruco/file_store'
12
14
  require 'ruco/window'
13
15
 
14
16
  require 'ruco/editor'
@@ -150,9 +150,11 @@ module Ruco
150
150
  action :quit do
151
151
  if editor.modified?
152
152
  ask("Loose changes? Enter=Yes Esc=Cancel") do
153
+ editor.store_session
153
154
  :quit
154
155
  end
155
156
  else
157
+ editor.store_session
156
158
  :quit
157
159
  end
158
160
  end
@@ -0,0 +1,5 @@
1
+ class File
2
+ def self.write(to, content)
3
+ File.open(to, 'w'){|f| f.write(content) }
4
+ end
5
+ end
@@ -1,4 +1,16 @@
1
1
  class Hash
2
2
  # 1.9 does not want index and 1.8 does not have key
3
3
  alias_method(:key, :index) unless method_defined?(:key)
4
+
5
+ # http://www.ruby-forum.com/topic/149449
6
+ def slice(*keys, &block)
7
+ if block
8
+ each do |key, val|
9
+ boolean = block.call(key, val)
10
+ keys << key if boolean
11
+ end
12
+ end
13
+ hash = self
14
+ keys.inject({}){|returned, key| returned.update key => hash[key]}
15
+ end
4
16
  end
data/lib/ruco/editor.rb CHANGED
@@ -29,6 +29,7 @@ module Ruco
29
29
 
30
30
  @saved_content = content
31
31
  @text_area = EditorArea.new(content, options)
32
+ restore_session
32
33
  end
33
34
 
34
35
  def find(text)
@@ -52,5 +53,21 @@ module Ruco
52
53
  rescue Object => e
53
54
  e.message
54
55
  end
56
+
57
+ def store_session
58
+ session_store.set(@file, text_area.state.slice(:position, :screen_position))
59
+ end
60
+
61
+ private
62
+
63
+ def restore_session
64
+ if state = session_store.get(@file)
65
+ text_area.state = state
66
+ end
67
+ end
68
+
69
+ def session_store
70
+ FileStore.new(File.expand_path('~/.ruco/sessions'), :keep => 20)
71
+ end
55
72
  end
56
73
  end
@@ -45,8 +45,6 @@ module Ruco
45
45
  adjust_to_indentation -removed.first, -removed.last
46
46
  end
47
47
 
48
- private
49
-
50
48
  def state
51
49
  {
52
50
  :content => content,
@@ -57,11 +55,13 @@ module Ruco
57
55
 
58
56
  def state=(data)
59
57
  @selection = nil
60
- @lines = data[:content].naive_split("\n")
58
+ @lines = data[:content].naive_split("\n") if data[:content]
61
59
  self.position = data[:position]
62
60
  self.screen_position = data[:screen_position]
63
61
  end
64
62
 
63
+ private
64
+
65
65
  # TODO use this instead of instance variables
66
66
  def screen_position
67
67
  Position.new(@window.top, @window.left)
@@ -0,0 +1,40 @@
1
+ require "digest/md5"
2
+
3
+ module Ruco
4
+ class FileStore
5
+ def initialize(folder, options)
6
+ @folder = folder
7
+ @options = options
8
+ end
9
+
10
+ def set(key, value)
11
+ `mkdir -p #{@folder}` unless File.exist? @folder
12
+ File.write(file(key), serialize(value))
13
+ cleanup
14
+ end
15
+
16
+ def get(key)
17
+ file = file(key)
18
+ deserialize File.read(file) if File.exist?(file)
19
+ end
20
+
21
+ private
22
+
23
+ def cleanup
24
+ delete = `ls -t #{@folder}`.split("\n")[@options[:keep]..-1] || []
25
+ delete.each{|f| File.delete("#{@folder}/#{f}") }
26
+ end
27
+
28
+ def file(key)
29
+ "#{@folder}/#{Digest::MD5.hexdigest(key)}.yml"
30
+ end
31
+
32
+ def serialize(value)
33
+ Marshal.dump(value)
34
+ end
35
+
36
+ def deserialize(value)
37
+ Marshal.load(value)
38
+ end
39
+ end
40
+ end
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.41"
8
+ s.version = "0.0.42"
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-02-05}
12
+ s.date = %q{2011-02-06}
13
13
  s.default_executable = %q{ruco}
14
14
  s.email = %q{michael@grosser.it}
15
15
  s.executables = ["ruco"]
@@ -24,12 +24,14 @@ Gem::Specification.new do |s|
24
24
  "lib/ruco/application.rb",
25
25
  "lib/ruco/command_bar.rb",
26
26
  "lib/ruco/core_ext/array.rb",
27
+ "lib/ruco/core_ext/file.rb",
27
28
  "lib/ruco/core_ext/hash.rb",
28
29
  "lib/ruco/core_ext/object.rb",
29
30
  "lib/ruco/core_ext/range.rb",
30
31
  "lib/ruco/core_ext/string.rb",
31
32
  "lib/ruco/editor.rb",
32
33
  "lib/ruco/editor_area.rb",
34
+ "lib/ruco/file_store.rb",
33
35
  "lib/ruco/form.rb",
34
36
  "lib/ruco/history.rb",
35
37
  "lib/ruco/keyboard.rb",
@@ -45,6 +47,7 @@ Gem::Specification.new do |s|
45
47
  "spec/ruco/core_ext/array_spec.rb",
46
48
  "spec/ruco/core_ext/string_spec.rb",
47
49
  "spec/ruco/editor_spec.rb",
50
+ "spec/ruco/file_store_spec.rb",
48
51
  "spec/ruco/form_spec.rb",
49
52
  "spec/ruco/history_spec.rb",
50
53
  "spec/ruco/keyboard_spec.rb",
@@ -64,6 +67,7 @@ Gem::Specification.new do |s|
64
67
  "spec/ruco/core_ext/array_spec.rb",
65
68
  "spec/ruco/core_ext/string_spec.rb",
66
69
  "spec/ruco/editor_spec.rb",
70
+ "spec/ruco/file_store_spec.rb",
67
71
  "spec/ruco/form_spec.rb",
68
72
  "spec/ruco/history_spec.rb",
69
73
  "spec/ruco/keyboard_spec.rb",
@@ -3,6 +3,7 @@ require File.expand_path('spec/spec_helper')
3
3
 
4
4
  describe Ruco::Application do
5
5
  before do
6
+ `rm -rf ~/.ruco/sessions`
6
7
  @file = 'spec/temp.txt'
7
8
  write('')
8
9
  end
@@ -15,7 +16,6 @@ describe Ruco::Application do
15
16
  File.read(@file)
16
17
  end
17
18
 
18
-
19
19
  def editor_part(view)
20
20
  view.naive_split("\n")[1..-2].join("\n")
21
21
  end
@@ -83,6 +83,22 @@ describe Ruco::Application do
83
83
  app.key(:enter).should == :quit
84
84
  end
85
85
  end
86
+
87
+ describe 'session' do
88
+ it "stores the session so I can reopen at the same position" do
89
+ write("0\n1\n2\n")
90
+ type :right, :down, :"Ctrl+q"
91
+ reopened = Ruco::Application.new(@file, :lines => 5, :columns => 10)
92
+ reopened.cursor.should == [2,1]
93
+ end
94
+
95
+ it "does not store or restore content" do
96
+ write('')
97
+ type 'x', :"Ctrl+s", :enter, :enter, 'a', :"Ctrl+q"
98
+ reopened = Ruco::Application.new(@file, :lines => 5, :columns => 10)
99
+ editor_part(reopened.view).should == "x\n\n"
100
+ end
101
+ end
86
102
 
87
103
  it "can select all" do
88
104
  write("1\n2\n3\n4\n5\n")
@@ -246,14 +262,18 @@ describe Ruco::Application do
246
262
  end
247
263
 
248
264
  describe '.ruco.rb' do
265
+ around do |block|
266
+ rucorc = File.expand_path('~/.ruco.rb')
267
+ `mv #{rucorc} #{rucorc}.backup 2>&1`
268
+ File.write(rucorc, "Ruco.configure{ bind(:'Ctrl+e'){ @editor.insert('TEST') } }")
269
+ block.call
270
+ `rm #{rucorc} && mv #{rucorc}.backup #{rucorc} 2>&1`
271
+ end
272
+
249
273
  it "loads it and can use the bound keys" do
250
- Tempfile.string_as_file("Ruco.configure{ bind(:'Ctrl+e'){ @editor.insert('TEST') } }") do |file|
251
- File.stub!(:exist?).and_return true
252
- File.should_receive(:expand_path).with("~/.ruco.rb").and_return file
253
- app.view.should_not include('TEST')
254
- app.key(:"Ctrl+e")
255
- app.view.should include("TEST")
256
- end
274
+ app.view.should_not include('TEST')
275
+ app.key(:"Ctrl+e")
276
+ app.view.should include("TEST")
257
277
  end
258
278
  end
259
279
 
@@ -20,6 +20,7 @@ describe Ruco::Editor do
20
20
  }
21
21
 
22
22
  before do
23
+ `rm -rf ~/.ruco/sessions`
23
24
  @file = 'spec/temp.txt'
24
25
  end
25
26
 
@@ -0,0 +1,47 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe Ruco::FileStore do
4
+ before do
5
+ @folder = 'spec/sessions'
6
+ `rm -rf #{@folder}`
7
+ end
8
+
9
+ after do
10
+ `rm -rf #{@folder}`
11
+ end
12
+
13
+ let(:store){ Ruco::FileStore.new(@folder, :keep => 3) }
14
+
15
+ it "can store stuff" do
16
+ store.set('xxx', 1)
17
+ store.get('xxx').should == 1
18
+ end
19
+
20
+ it "can store :keep keys" do
21
+ store.set('xxx', 1)
22
+ store.set('yyy', 1)
23
+ store.set('zzz', 1)
24
+ store.set('aaa', 1)
25
+ store.get('xxx').should == nil
26
+ end
27
+
28
+ it "drops least recently used key" do
29
+ store.set('xxx', 1)
30
+ store.set('yyy', 1)
31
+ store.set('xxx', 1)
32
+ store.set('zzz', 1)
33
+ store.set('aaa', 1)
34
+ store.get('xxx').should == 1
35
+ store.get('yyy').should == nil
36
+ end
37
+
38
+ it "does not drop if used multiple times" do
39
+ store.set('xxx', 1)
40
+ store.set('yyy', 1)
41
+ store.set('zzz', 1)
42
+ store.set('zzz', 1)
43
+ store.set('zzz', 1)
44
+ store.set('zzz', 1)
45
+ store.get('xxx').should == 1
46
+ end
47
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 41
9
- version: 0.0.41
8
+ - 42
9
+ version: 0.0.42
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-02-05 00:00:00 +01:00
17
+ date: 2011-02-06 00:00:00 +01:00
18
18
  default_executable: ruco
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -51,12 +51,14 @@ files:
51
51
  - lib/ruco/application.rb
52
52
  - lib/ruco/command_bar.rb
53
53
  - lib/ruco/core_ext/array.rb
54
+ - lib/ruco/core_ext/file.rb
54
55
  - lib/ruco/core_ext/hash.rb
55
56
  - lib/ruco/core_ext/object.rb
56
57
  - lib/ruco/core_ext/range.rb
57
58
  - lib/ruco/core_ext/string.rb
58
59
  - lib/ruco/editor.rb
59
60
  - lib/ruco/editor_area.rb
61
+ - lib/ruco/file_store.rb
60
62
  - lib/ruco/form.rb
61
63
  - lib/ruco/history.rb
62
64
  - lib/ruco/keyboard.rb
@@ -72,6 +74,7 @@ files:
72
74
  - spec/ruco/core_ext/array_spec.rb
73
75
  - spec/ruco/core_ext/string_spec.rb
74
76
  - spec/ruco/editor_spec.rb
77
+ - spec/ruco/file_store_spec.rb
75
78
  - spec/ruco/form_spec.rb
76
79
  - spec/ruco/history_spec.rb
77
80
  - spec/ruco/keyboard_spec.rb
@@ -94,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
97
  requirements:
95
98
  - - ">="
96
99
  - !ruby/object:Gem::Version
97
- hash: 282935183
100
+ hash: 916252531
98
101
  segments:
99
102
  - 0
100
103
  version: "0"
@@ -119,6 +122,7 @@ test_files:
119
122
  - spec/ruco/core_ext/array_spec.rb
120
123
  - spec/ruco/core_ext/string_spec.rb
121
124
  - spec/ruco/editor_spec.rb
125
+ - spec/ruco/file_store_spec.rb
122
126
  - spec/ruco/form_spec.rb
123
127
  - spec/ruco/history_spec.rb
124
128
  - spec/ruco/keyboard_spec.rb