ruco 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Readme.md CHANGED
@@ -10,7 +10,7 @@ Finished:
10
10
  - change-indicator
11
11
  - writeable indicator
12
12
  - backspace / delete
13
- - find
13
+ - find / go to line
14
14
 
15
15
  Install
16
16
  =======
@@ -22,7 +22,8 @@ Usage
22
22
 
23
23
  TODO
24
24
  =====
25
- - cursor move + delete/backspace inside find field
25
+ - delete line
26
+ - scrolling inside text field
26
27
  - smart staying at end of line/column when changing line
27
28
  - indentation + paste support
28
29
  - warnings / messages
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
data/bin/ruco CHANGED
@@ -106,6 +106,9 @@ init_screen do
106
106
  when ?\C-f then
107
107
  focused = command
108
108
  command.find
109
+ when ?\C-g then
110
+ focused = command
111
+ command.move_to_line
109
112
  when 27 then
110
113
  focused.reset
111
114
  focused = editor # escape from focused
@@ -1,11 +1,16 @@
1
+ require 'ruco/core_ext/object'
2
+ require 'ruco/core_ext/string'
3
+ require 'ruco/core_ext/array'
4
+
1
5
  require 'ruco/focusable'
2
6
  require 'ruco/command'
3
7
 
4
8
  require 'ruco/editor'
5
9
  require 'ruco/status_bar'
6
10
  require 'ruco/command_bar'
7
- require 'ruco/core_ext/string'
8
- require 'ruco/core_ext/array'
11
+
12
+ require 'ruco/form'
13
+ require 'ruco/text_field'
9
14
 
10
15
  module Ruco
11
16
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
@@ -2,49 +2,52 @@ module Ruco
2
2
  class CommandBar
3
3
  include Focusable
4
4
 
5
- attr_accessor :cursor_line
5
+ attr_accessor :cursor_line, :form
6
+ delegate :move, :delete, :insert, :to => :form
6
7
 
7
8
  SHORTCUTS = [
8
9
  '^W Exit',
9
10
  '^S Save',
10
11
  '^F Find',
11
- '^D Delete line'
12
+ '^D Delete line',
13
+ '^G Go to line'
12
14
  ]
13
15
 
14
16
  SEARCH_PREFIX = "Find: "
15
17
 
16
18
  def initialize(options)
17
19
  @options = options
20
+ @forms_cache = {}
18
21
  reset
19
22
  end
20
23
 
21
24
  def view
22
- if @find_mode
23
- SEARCH_PREFIX + @find_term
25
+ if @form
26
+ @form.view
24
27
  else
25
28
  available_shortcuts
26
29
  end
27
30
  end
28
31
 
29
32
  def find
30
- @find_mode = true
33
+ @form = @forms_cache[:find] ||= Form.new('Find: ', :columns => @options[:columns], :command => :find)
31
34
  end
32
35
 
33
- def insert(text)
34
- @find_term += text
35
- if @find_term.include?("\n")
36
- @find_term.gsub!("\n",'')
37
- Command.new(:find, @find_term)
38
- end
36
+ def move_to_line
37
+ @form = Form.new('Go to Line: ', :columns => @options[:columns], :command => :move_to_line, :type => :integer)
39
38
  end
40
39
 
41
40
  def reset
42
- @find_mode = false
43
- @find_term = ''
41
+ @forms_cache[:find] = nil if @form == @forms_cache[:find]
42
+ @form = nil
44
43
  end
45
44
 
46
45
  def cursor_column
47
- view.size
46
+ if @form
47
+ @form.cursor[1]
48
+ else
49
+ 0
50
+ end
48
51
  end
49
52
 
50
53
  private
@@ -0,0 +1,17 @@
1
+ class Object
2
+ # copy from active_support
3
+ def delegate(*methods)
4
+ options = methods.pop
5
+ unless options.is_a?(Hash) && to = options[:to]
6
+ raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
7
+ end
8
+
9
+ methods.each do |method|
10
+ module_eval(<<-EOS, "(__DELEGATION__)", 1)
11
+ def #{method}(*args, &block)
12
+ #{to}.__send__(#{method.inspect}, *args, &block)
13
+ end
14
+ EOS
15
+ end
16
+ end
17
+ end
@@ -28,8 +28,8 @@ module Ruco
28
28
  end
29
29
 
30
30
  def move(line, column)
31
- @line = [[@line + line, 0].max, lines.size].min
32
- @column = [[@column + column, 0].max, (lines[@line]||'').size].min
31
+ @line += line
32
+ @column += column
33
33
  adjust_view
34
34
  end
35
35
 
@@ -120,6 +120,8 @@ module Ruco
120
120
  end
121
121
 
122
122
  def adjust_view
123
+ @line = [[@line, 0].max, lines.size - 1].min
124
+ @column = [[@column, 0].max, (lines[@line]||'').size].min
123
125
  reposition_cursor
124
126
  scroll_column_into_view
125
127
  scroll_line_into_view
@@ -0,0 +1,36 @@
1
+ module Ruco
2
+ class Form
3
+ delegate :move, :delete, :to => :text_field
4
+
5
+ def initialize(label, options)
6
+ @options = options
7
+ @label = label.strip + ' '
8
+ reset
9
+ end
10
+
11
+ def view
12
+ @label + @text_field.view
13
+ end
14
+
15
+ def insert(text)
16
+ @text_field.insert(text.gsub("\n",''))
17
+ if text.include?("\n")
18
+ result = @text_field.value
19
+ result = result.to_i if @options[:type] == :integer
20
+ Command.new(@options[:command], result)
21
+ end
22
+ end
23
+
24
+ def cursor
25
+ [0, @label.size + @text_field.cursor[1]]
26
+ end
27
+
28
+ def reset
29
+ @text_field = TextField.new(:columns => @options[:columns] - @label.size)
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :text_field
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module Ruco
2
+ class TextField
3
+ def initialize(options)
4
+ @options = options
5
+ @content = ''
6
+ @column = 0
7
+ end
8
+
9
+ def insert(text)
10
+ @content += text
11
+ move(0, text.size)
12
+ end
13
+
14
+ def delete(count)
15
+ if count > 0
16
+ @content.slice!(@column, count)
17
+ else
18
+ delete_start = [@column - count.abs, 0].max
19
+ @content[delete_start...@column] = ''
20
+ @column = delete_start
21
+ end
22
+ end
23
+
24
+ def view
25
+ @content
26
+ end
27
+
28
+ def value
29
+ @content
30
+ end
31
+
32
+ def move(line, column)
33
+ @column = [[@column + column, 0].max, @content.size].min
34
+ end
35
+
36
+ def cursor
37
+ [0, @column]
38
+ end
39
+ end
40
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruco}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
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-09}
12
+ s.date = %q{2011-01-10}
13
13
  s.default_executable = %q{ruco}
14
14
  s.email = %q{michael@grosser.it}
15
15
  s.executables = ["ruco"]
@@ -24,15 +24,19 @@ Gem::Specification.new do |s|
24
24
  "lib/ruco/command.rb",
25
25
  "lib/ruco/command_bar.rb",
26
26
  "lib/ruco/core_ext/array.rb",
27
+ "lib/ruco/core_ext/object.rb",
27
28
  "lib/ruco/core_ext/string.rb",
28
29
  "lib/ruco/editor.rb",
29
30
  "lib/ruco/focusable.rb",
31
+ "lib/ruco/form.rb",
30
32
  "lib/ruco/status_bar.rb",
33
+ "lib/ruco/text_field.rb",
31
34
  "ruco.gemspec",
32
35
  "spec/ruco/command_bar_spec.rb",
33
36
  "spec/ruco/command_spec.rb",
34
37
  "spec/ruco/core_ext/string_spec.rb",
35
38
  "spec/ruco/editor_spec.rb",
39
+ "spec/ruco/form_spec.rb",
36
40
  "spec/ruco/status_bar_spec.rb",
37
41
  "spec/ruco_spec.rb",
38
42
  "spec/spec_helper.rb"
@@ -46,6 +50,7 @@ Gem::Specification.new do |s|
46
50
  "spec/ruco/command_spec.rb",
47
51
  "spec/ruco/core_ext/string_spec.rb",
48
52
  "spec/ruco/editor_spec.rb",
53
+ "spec/ruco/form_spec.rb",
49
54
  "spec/ruco/status_bar_spec.rb",
50
55
  "spec/ruco_spec.rb",
51
56
  "spec/spec_helper.rb"
@@ -28,6 +28,14 @@ describe Ruco::CommandBar do
28
28
  bar.cursor_column.should == 9
29
29
  end
30
30
 
31
+ it "keeps entered stuff" do
32
+ bar.find
33
+ bar.insert('abc')
34
+ bar.find
35
+ bar.view.should == "Find: abc"
36
+ bar.cursor_column.should == 9
37
+ end
38
+
31
39
  it "can reset the search" do
32
40
  bar.find
33
41
  bar.insert('abc')
@@ -54,4 +62,36 @@ describe Ruco::CommandBar do
54
62
  result.should == Ruco::Command.new(:find, 'abcd')
55
63
  end
56
64
  end
65
+
66
+ describe :move_to_line do
67
+ it "displays a form" do
68
+ bar.move_to_line
69
+ bar.view.should == "Go to Line: "
70
+ end
71
+
72
+ it "gets output" do
73
+ bar.move_to_line
74
+ bar.insert('123')
75
+ result = bar.insert("\n")
76
+ result.should == Ruco::Command.new(:move_to_line, 123)
77
+ end
78
+
79
+ it "gets reset" do
80
+ bar.move_to_line
81
+ bar.insert('123')
82
+ bar.move_to_line
83
+ bar.view.should == "Go to Line: "
84
+ end
85
+
86
+ it "does not reset search when resetting" do
87
+ bar.find
88
+ bar.insert('abc')
89
+ bar.move_to_line
90
+ bar.reset
91
+
92
+ bar.view.should include("^W Exit ") # default view
93
+ bar.find
94
+ bar.view.should == "Find: abc"
95
+ end
96
+ end
57
97
  end
@@ -48,10 +48,10 @@ describe Ruco::Editor do
48
48
  editor.cursor.should == [2,4]
49
49
  end
50
50
 
51
- it "gets reset to empty line when moving past lines" do
51
+ it "stays in last line when moving past lines" do
52
52
  write(" ")
53
53
  editor.move(6,3)
54
- editor.cursor.should == [1,0]
54
+ editor.cursor.should == [0,3]
55
55
  end
56
56
 
57
57
  describe 'column scrolling' do
@@ -101,12 +101,27 @@ describe Ruco::Editor do
101
101
 
102
102
  it "can scroll till end of file" do
103
103
  editor.move(15,0)
104
- editor.view.should == "\n\n\n"
104
+ editor.view.should == "9\n\n\n"
105
105
  editor.cursor_line.should == 0
106
106
  end
107
107
  end
108
108
  end
109
109
 
110
+ describe :move_to do
111
+ it "cannot move outside of text (bottom/right)" do
112
+ write("123\n456")
113
+ editor.move_to(10,10)
114
+ editor.cursor.should == [1,3]
115
+ end
116
+
117
+ it "cannot move outside of text (top/left)" do
118
+ write("123\n456")
119
+ editor.move(1,1)
120
+ editor.move_to(-10,-10)
121
+ editor.cursor.should == [0,0]
122
+ end
123
+ end
124
+
110
125
  describe :move_to_eol do
111
126
  before do
112
127
  write("\n aa \n ")
@@ -260,8 +275,9 @@ describe Ruco::Editor do
260
275
  end
261
276
 
262
277
  it "can add newlines to the moveable end" do
263
- write('')
264
- editor.move(1,0)
278
+ write('abc')
279
+ editor.move(0,3)
280
+ editor.insert("\n")
265
281
  editor.insert("\n")
266
282
  editor.cursor.should == [2,0]
267
283
  end
@@ -0,0 +1,75 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe Ruco::Form do
4
+ let(:form){ Ruco::Form.new('Test', :columns => 30, :command => :find) }
5
+
6
+ it "positions cursor in text field" do
7
+ form.cursor.should == [0, 5]
8
+ end
9
+
10
+ describe :insert do
11
+ it "adds label size and input size" do
12
+ form.insert('abc')
13
+ form.cursor.should == [0, 8]
14
+ end
15
+
16
+ it "returns nil on any insert" do
17
+ form.insert('abc').should == nil
18
+ end
19
+
20
+ it "returns value on enter" do
21
+ form.insert('abc')
22
+ form.insert("d\n").should == Ruco::Command.new(:find, 'abcd')
23
+ end
24
+ end
25
+
26
+ describe :move do
27
+ it "moves in text-field" do
28
+ form.insert('abc')
29
+ form.move(0, -1)
30
+ form.cursor.should == [0,7]
31
+ end
32
+
33
+ it "cannot move out of left side" do
34
+ form.move(0, -3)
35
+ form.cursor.should == [0,5]
36
+ end
37
+
38
+ it "cannot move out of right side" do
39
+ form.move(0, 4)
40
+ form.cursor.should == [0,5]
41
+ form.insert('abc')
42
+ form.move(0, 4)
43
+ form.cursor.should == [0,8]
44
+ end
45
+ end
46
+
47
+ describe :delete do
48
+ it "removes characters forward" do
49
+ form.insert('abc')
50
+ form.move(0, -2)
51
+ form.delete(1)
52
+ form.view.should == 'Test ac'
53
+ end
54
+
55
+ it "removes characters backward" do
56
+ form.insert('abc')
57
+ form.move(0, -1)
58
+ form.delete(-1)
59
+ form.view.should == 'Test ac'
60
+ end
61
+
62
+ it "moves the cursor backward" do
63
+ form.insert('abc')
64
+ form.move(0, -1)
65
+ form.delete(-1)
66
+ form.cursor.should == [0,6]
67
+ end
68
+ end
69
+
70
+ describe :view do
71
+ it "can be viewed" do
72
+ form.view.should == "Test "
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruco
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-09 00:00:00 +01:00
18
+ date: 2011-01-10 00:00:00 +01:00
19
19
  default_executable: ruco
20
20
  dependencies: []
21
21
 
@@ -38,15 +38,19 @@ files:
38
38
  - lib/ruco/command.rb
39
39
  - lib/ruco/command_bar.rb
40
40
  - lib/ruco/core_ext/array.rb
41
+ - lib/ruco/core_ext/object.rb
41
42
  - lib/ruco/core_ext/string.rb
42
43
  - lib/ruco/editor.rb
43
44
  - lib/ruco/focusable.rb
45
+ - lib/ruco/form.rb
44
46
  - lib/ruco/status_bar.rb
47
+ - lib/ruco/text_field.rb
45
48
  - ruco.gemspec
46
49
  - spec/ruco/command_bar_spec.rb
47
50
  - spec/ruco/command_spec.rb
48
51
  - spec/ruco/core_ext/string_spec.rb
49
52
  - spec/ruco/editor_spec.rb
53
+ - spec/ruco/form_spec.rb
50
54
  - spec/ruco/status_bar_spec.rb
51
55
  - spec/ruco_spec.rb
52
56
  - spec/spec_helper.rb
@@ -89,6 +93,7 @@ test_files:
89
93
  - spec/ruco/command_spec.rb
90
94
  - spec/ruco/core_ext/string_spec.rb
91
95
  - spec/ruco/editor_spec.rb
96
+ - spec/ruco/form_spec.rb
92
97
  - spec/ruco/status_bar_spec.rb
93
98
  - spec/ruco_spec.rb
94
99
  - spec/spec_helper.rb