ruco 0.0.40 → 0.0.41

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.40
1
+ 0.0.41
@@ -68,7 +68,8 @@ module Ruco
68
68
  end
69
69
 
70
70
  def screen_position=(pos)
71
- @window.top, @window.left = pos
71
+ @window.set_top(pos[0], @lines.size)
72
+ @window.left = pos[1]
72
73
  end
73
74
 
74
75
  def adjust_to_indentation(first, last=nil)
@@ -8,21 +8,21 @@ module Ruco
8
8
  @column = 0
9
9
  @line = 0
10
10
  @window = Window.new(@options.delete(:lines), @options.delete(:columns))
11
- @window.position = position
11
+ adjust_window
12
12
  end
13
13
 
14
14
  def view
15
- @window.position = position
15
+ adjust_window
16
16
  @window.crop(lines) * "\n" + "\n"
17
17
  end
18
18
 
19
19
  def cursor
20
- @window.position = position
20
+ adjust_window
21
21
  @window.cursor
22
22
  end
23
23
 
24
24
  def color_mask
25
- @window.position = position
25
+ adjust_window
26
26
  @window.color_mask(@selection)
27
27
  end
28
28
 
@@ -39,15 +39,11 @@ module Ruco
39
39
  when :to_column then self.column = args.first
40
40
  when :to_index then move(:to, *position_for_index(*args))
41
41
  when :page_down then
42
- shift = @window.lines - 1
43
- old = self.line
44
- self.line += shift
45
- @window.top += (line - old) # force scroll
42
+ self.line += @window.lines
43
+ @window.set_top(@window.top + @window.lines, @lines.size)
46
44
  when :page_up then
47
- shift = @window.lines - 1
48
- old = self.line
49
- self.line -= shift
50
- @window.top += (line - old) # force scroll
45
+ self.line -= @window.lines
46
+ @window.set_top(@window.top - @window.lines, @lines.size)
51
47
  else
52
48
  raise "Unknown move type #{where} with #{args.inspect}"
53
49
  end
@@ -245,5 +241,9 @@ module Ruco
245
241
  self.line = line
246
242
  self.column = column
247
243
  end
244
+
245
+ def adjust_window
246
+ @window.set_position(position, :max_lines => @lines.size)
247
+ end
248
248
  end
249
249
  end
data/lib/ruco/window.rb CHANGED
@@ -2,10 +2,17 @@ module Ruco
2
2
  class Window
3
3
  OFFSET = 5
4
4
 
5
- attr_accessor :position, :lines, :columns, :top, :left
6
- attr_reader :cursor
5
+ attr_accessor :lines, :columns, :left
6
+ attr_reader :cursor, :top
7
+
8
+ def initialize(lines, columns, options={})
9
+ @options = options
10
+
11
+ @options[:line_scroll_threshold] ||= 1
12
+ @options[:line_scroll_offset] ||= 1
13
+ @options[:column_scroll_threshold] ||= 1
14
+ @options[:column_scroll_offset] ||= 5
7
15
 
8
- def initialize(lines, columns)
9
16
  @lines = lines
10
17
  @columns = columns
11
18
  @top = 0
@@ -13,19 +20,33 @@ module Ruco
13
20
  @cursor = Position.new(0,0)
14
21
  end
15
22
 
16
- def crop(content)
17
- lines = content[visible_lines] || []
18
- lines[@lines-1] ||= nil
19
- lines.map do |line|
23
+ def position=(x)
24
+ set_position(x)
25
+ end
26
+
27
+ def set_position(position, options={})
28
+ scroll_line_into_view position.line, (options[:max_lines] || 9999)
29
+ scroll_column_into_view position.column
30
+ @cursor = Position.new(position.line - @top, position.column - @left)
31
+ end
32
+
33
+ def crop(lines)
34
+ lines_to_display = lines[visible_lines] || []
35
+ lines_to_display[@lines-1] ||= nil
36
+ lines_to_display.map do |line|
20
37
  line ||= ''
21
38
  line[visible_columns] || ''
22
39
  end
23
40
  end
24
41
 
25
- def position=(pos)
26
- self.top = pos.line - line_offset unless visible_lines.include?(pos.line)
27
- self.left = pos.column - column_offset unless visible_columns.include?(pos.column)
28
- @cursor = Position.new(pos.line - @top, pos.column - @left)
42
+ def scroll_line_into_view(line, total_lines)
43
+ result = adjustment(line, visible_lines, @options[:line_scroll_threshold], @options[:line_scroll_offset])
44
+ set_top result, total_lines if result
45
+ end
46
+
47
+ def scroll_column_into_view(column)
48
+ result = adjustment(column, visible_columns, @options[:column_scroll_threshold], @options[:column_scroll_offset])
49
+ self.left = result if result
29
50
  end
30
51
 
31
52
  def color_mask(selection)
@@ -46,16 +67,26 @@ module Ruco
46
67
  end
47
68
  end
48
69
 
49
- def top=(x)
50
- @top = [x,0].max
51
- end
52
-
53
70
  def left=(x)
54
71
  @left = [x,0].max
55
72
  end
56
73
 
74
+ def set_top(line, total_lines)
75
+ max_top = total_lines - lines + 1 + @options[:line_scroll_offset]
76
+ @top = [[line, max_top].min, 0].max
77
+ end
78
+
57
79
  private
58
80
 
81
+ def adjustment(current, allowed, threshold, offset)
82
+ if current < (allowed.first + threshold)
83
+ current - offset
84
+ elsif current > (allowed.last - threshold)
85
+ size = allowed.last - allowed.first + 1
86
+ current - size + 1 + offset
87
+ end
88
+ end
89
+
59
90
  def visible_area(line)
60
91
  line += @top
61
92
  start_of_line = [line, @left]
@@ -64,14 +95,6 @@ module Ruco
64
95
  start_of_line..end_of_line
65
96
  end
66
97
 
67
- def line_offset
68
- @lines / 2
69
- end
70
-
71
- def column_offset
72
- @columns / 2
73
- end
74
-
75
98
  def visible_lines
76
99
  @top..(@top+@lines-1)
77
100
  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.40"
8
+ s.version = "0.0.41"
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-04}
12
+ s.date = %q{2011-02-05}
13
13
  s.default_executable = %q{ruco}
14
14
  s.email = %q{michael@grosser.it}
15
15
  s.executables = ["ruco"]
@@ -39,7 +39,7 @@ describe Ruco::Application do
39
39
  app.key(:enter)
40
40
  app.key('2')
41
41
  app.key(:enter)
42
- app.view.should == "#{status.sub('.txt ','.txt*')}22\n2\n\n#{command}"
42
+ app.view.should == "#{status.sub('.txt ','.txt*')}2\n\n\n#{command}"
43
43
  end
44
44
 
45
45
  it "does not enter key-codes" do
@@ -204,7 +204,7 @@ describe Ruco::Application do
204
204
  app.key(:tab)
205
205
  app.key(:tab)
206
206
  app.key(:'Ctrl+v') # paste
207
- editor_part(app.view).should == " ab\n cd\n ef"
207
+ editor_part(app.view).should == " cd\n ef\n"
208
208
  end
209
209
 
210
210
  it "indents when typing" do
@@ -5,7 +5,19 @@ describe Ruco::Editor do
5
5
  File.open(@file,'w'){|f| f.write(content) }
6
6
  end
7
7
 
8
- let(:editor){ Ruco::Editor.new(@file, :lines => 3, :columns => 5, :line_scrolling_offset => 5, :column_scrolling_offset => 5) }
8
+ let(:editor){
9
+ editor = Ruco::Editor.new(@file, :lines => 3, :columns => 5)
10
+ # only scroll when we reach end of lines/columns <-> able to test with smaller area
11
+ editor.send(:text_area).instance_eval{
12
+ @window.instance_eval{
13
+ @options[:line_scroll_threshold] = 0
14
+ @options[:line_scroll_offset] = 1
15
+ @options[:column_scroll_threshold] = 0
16
+ @options[:column_scroll_offset] = 1
17
+ }
18
+ }
19
+ editor
20
+ }
9
21
 
10
22
  before do
11
23
  @file = 'spec/temp.txt'
@@ -113,26 +125,26 @@ describe Ruco::Editor do
113
125
  editor.cursor.column.should == 4
114
126
 
115
127
  editor.move(:relative, 0,1)
116
- editor.view.should == "45678\n\n\n"
117
- editor.cursor.column.should == 2
128
+ editor.view.should == "34567\n3\n\n"
129
+ editor.cursor.column.should == 3
118
130
  end
119
131
 
120
132
  it "cannot scroll past the screen" do
121
133
  write('123456789')
122
134
  editor.move(:relative, 0,4)
123
135
  6.times{ editor.move(:relative, 0,1) }
124
- editor.view.should == "89\n\n\n"
125
- editor.cursor.column.should == 2
136
+ editor.view.should == "789\n\n\n"
137
+ editor.cursor.column.should == 3
126
138
  end
127
139
 
128
140
  it "can scroll columns backwards" do
129
- write('123456789')
141
+ write('0123456789')
130
142
  editor.move(:relative, 0,5)
131
- editor.view.should == "45678\n\n\n"
143
+ editor.view.should == "23456\n\n\n"
132
144
 
133
- editor.move(:relative, 0,-3)
134
- editor.view.should == "12345\n\n\n"
135
- editor.cursor.column.should == 2
145
+ editor.move(:relative, 0,-4)
146
+ editor.view.should == "01234\n\n\n"
147
+ editor.cursor.column.should == 1
136
148
  end
137
149
  end
138
150
 
@@ -7,14 +7,14 @@ describe Ruco::TextArea do
7
7
  text = Ruco::TextArea.new("1\n2\n3\n4\n5\n6\n7\n8\n9\n", :lines => 3, :columns => 3)
8
8
  text.move(:page_down)
9
9
  text.view.should == "3\n4\n5\n"
10
- text.cursor.should == [0,0]
10
+ text.cursor.should == [1,0]
11
11
  end
12
12
 
13
13
  it "keeps cursor position when moving down" do
14
- text = Ruco::TextArea.new("1\n2abc\n3\n4ab\n5\n6\n7\n8\n9\n", :lines => 3, :columns => 5)
14
+ text = Ruco::TextArea.new("1\n2abc\n3\n4\n5ab\n6\n7\n8\n9\n", :lines => 3, :columns => 5)
15
15
  text.move(:to, 1,4)
16
16
  text.move(:page_down)
17
- text.view.should == "3\n4ab\n5\n"
17
+ text.view.should == "4\n5ab\n6\n"
18
18
  text.cursor.should == [1,3]
19
19
  end
20
20
 
@@ -24,17 +24,17 @@ describe Ruco::TextArea do
24
24
  text.view.should == "3\n4\n5\n"
25
25
  text.cursor.should == [1,0]
26
26
  text.move(:page_up)
27
- text.view.should == "1\n2\n3\n"
27
+ text.view.should == "0\n1\n2\n"
28
28
  text.cursor.should == [1,0]
29
29
  end
30
30
 
31
31
  it "keeps column position when moving up" do
32
- text = Ruco::TextArea.new("0\n1\n2\n3ab\n4\n5abc\n6\n7\n8\n", :lines => 3, :columns => 5)
33
- text.move(:to, 5, 4)
32
+ text = Ruco::TextArea.new("0\n1\n2ab\n3\n4\n5abc\n6\n7\n8\n9\n10\n11\n", :lines => 3, :columns => 5)
33
+ text.move(:to, 5, 3)
34
34
  text.view.should == "4\n5abc\n6\n"
35
- text.cursor.should == [1,4]
35
+ text.cursor.should == [1,3]
36
36
  text.move(:page_up)
37
- text.view.should == "2\n3ab\n4\n"
37
+ text.view.should == "1\n2ab\n3\n"
38
38
  text.cursor.should == [1,3]
39
39
  end
40
40
 
@@ -4,7 +4,12 @@ describe Ruco::Window do
4
4
  let(:window){ Ruco::Window.new(10,10) }
5
5
 
6
6
  describe :crop do
7
- let(:window){ Ruco::Window.new(2,4) }
7
+ let(:window){
8
+ Ruco::Window.new(2,4,
9
+ :line_scroll_threshold => 0, :line_scroll_offset => 1,
10
+ :column_scroll_threshold => 0, :column_scroll_offset => 1
11
+ )
12
+ }
8
13
 
9
14
  it "does not modify given lines" do
10
15
  original = ['1234','1234']
@@ -39,67 +44,84 @@ describe Ruco::Window do
39
44
 
40
45
  describe 'scrolled' do
41
46
  it "goes out of frame if line is out of frame" do
42
- window = Ruco::Window.new(6,1)
47
+ window = Ruco::Window.new(6,1, :line_scroll_offset => 0, :line_scroll_threshold => 0)
43
48
  window.position = Ruco::Position.new(6,0)
44
49
  result = window.crop(['1','2','3','4','5','6','7','8','9'])
45
- result.should == ['4','5','6','7','8','9']
50
+ result.should == ['2','3','4','5','6','7']
46
51
  end
47
52
 
48
53
  it "goes out of frame if column is out of frame" do
49
- window = Ruco::Window.new(1,6)
54
+ window = Ruco::Window.new(1,6, :column_scroll_offset => 0, :column_scroll_threshold => 0)
50
55
  window.position = Ruco::Position.new(0,6)
51
56
  result = window.crop(['1234567890'])
52
- result.should == ['456789']
57
+ result.should == ['234567']
53
58
  end
54
59
  end
55
60
  end
56
61
 
57
62
  describe :top do
63
+ let(:window){ Ruco::Window.new(10,10, :line_scroll_threshold => 1, :line_scroll_offset => 3) }
64
+
58
65
  it "does not change when staying in frame" do
59
66
  window.top.should == 0
60
- window.position = Ruco::Position.new(9,0)
67
+ window.position = Ruco::Position.new(8,0)
61
68
  window.top.should == 0
62
69
  end
63
70
 
64
- it "changes by offset when going vertically out of frame" do
65
- window.position = Ruco::Position.new(10,0)
66
- window.top.should == 5
71
+ it "changes by offset when going down out of frame" do
72
+ window.position = Ruco::Position.new(9,0)
73
+ window.top.should == 3
67
74
  end
68
75
 
69
- it "changes to x - offset when going down out of frame" do
76
+ it "stays at bottom when going down out of frame" do
70
77
  window.position = Ruco::Position.new(20,0)
71
- window.top.should == 15
78
+ window.top.should == 20 - 10 + 3 + 1
72
79
  end
73
80
 
74
- it "changes to x - offset when going down out of frame" do
81
+ it "stays at top when going up out of frame" do
75
82
  window.position = Ruco::Position.new(20,0)
76
83
  window.position = Ruco::Position.new(7,0)
77
- window.top.should == 2
84
+ window.top.should == 7 - 3
85
+ end
86
+
87
+ it "changes to 0 when going up to 1" do
88
+ window.position = Ruco::Position.new(20,0)
89
+ window.position = Ruco::Position.new(1,0)
90
+ window.top.should == 0
91
+ end
92
+
93
+ it "does not change when staying in changed frame" do
94
+ window.position = Ruco::Position.new(9,0)
95
+ window.top.should == 3
96
+ window.position = Ruco::Position.new(11,0)
97
+ window.top.should == 3
78
98
  end
79
99
  end
80
100
 
81
101
  describe :left do
102
+ let(:window){ Ruco::Window.new(10,10, :column_scroll_threshold => 1, :column_scroll_offset => 3) }
103
+
82
104
  it "does not change when staying in frame" do
83
105
  window.left.should == 0
84
- window.position = Ruco::Position.new(0,9)
106
+ window.position = Ruco::Position.new(0,8)
85
107
  window.left.should == 0
86
108
  end
87
109
 
88
110
  it "changes by offset when going vertically out of frame" do
111
+ window.position = Ruco::Position.new(0,8)
89
112
  window.position = Ruco::Position.new(0,9)
90
- window.position = Ruco::Position.new(0,10)
91
- window.left.should == 5
113
+ window.left.should == 3
92
114
  end
93
115
 
94
- it "changes to x - offset when going right out of frame" do
116
+ it "stays right when going right out of frame" do
95
117
  window.position = Ruco::Position.new(0,20)
96
- window.left.should == 15
118
+ window.left.should == 20 - 10 + 3 + 1
97
119
  end
98
120
 
99
- it "changes to x - offset when going left out of frame" do
121
+ it "stays left when going left out of frame" do
100
122
  window.position = Ruco::Position.new(0,20)
101
123
  window.position = Ruco::Position.new(0,7)
102
- window.left.should == 2
124
+ window.left.should == 7 - 3
103
125
  end
104
126
 
105
127
  it "changes to 0 when going left out of frame to 1" do
@@ -109,24 +131,29 @@ describe Ruco::Window do
109
131
  end
110
132
 
111
133
  it "does not change when staying in changed frame" do
134
+ window.position = Ruco::Position.new(0,8)
112
135
  window.position = Ruco::Position.new(0,9)
113
- window.position = Ruco::Position.new(0,10)
114
- window.left.should == 5
115
- window.position = Ruco::Position.new(0,14)
116
- window.left.should == 5
136
+ window.left.should == 3
137
+ window.position = Ruco::Position.new(0,11)
138
+ window.left.should == 3
117
139
  end
118
140
  end
119
141
 
120
- describe :top= do
142
+ describe :set_top do
121
143
  it "sets" do
122
- window.top = 1
144
+ window.set_top 1, 20
123
145
  window.top.should == 1
124
146
  end
125
147
 
126
148
  it "does not allow negative" do
127
- window.top = -1
149
+ window.set_top -1, 20
128
150
  window.top.should == 0
129
151
  end
152
+
153
+ it "does not go above maximum top" do
154
+ window.set_top 20, 20
155
+ window.top.should == 20 - 10 + 3 - 1
156
+ end
130
157
  end
131
158
 
132
159
  describe :left= do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 40
9
- version: 0.0.40
8
+ - 41
9
+ version: 0.0.41
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-04 00:00:00 +01:00
17
+ date: 2011-02-05 00:00:00 +01:00
18
18
  default_executable: ruco
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: -441011373
97
+ hash: 282935183
98
98
  segments:
99
99
  - 0
100
100
  version: "0"