ruco 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +8 -4
- data/Readme.md +2 -1
- data/VERSION +1 -1
- data/bin/ruco +2 -1
- data/lib/ruco.rb +0 -1
- data/lib/ruco/application.rb +12 -9
- data/lib/ruco/command_bar.rb +15 -13
- data/lib/ruco/form.rb +1 -5
- data/lib/ruco/keyboard.rb +6 -8
- data/ruco.gemspec +1 -4
- data/spec/ruco/command_bar_spec.rb +33 -55
- data/spec/ruco/form_spec.rb +7 -5
- metadata +3 -6
- data/lib/ruco/command.rb +0 -19
- data/spec/ruco/command_spec.rb +0 -11
data/Rakefile
CHANGED
@@ -20,15 +20,19 @@ end
|
|
20
20
|
task :key do
|
21
21
|
require 'curses'
|
22
22
|
|
23
|
-
Curses.
|
24
|
-
Curses.
|
25
|
-
Curses.
|
23
|
+
Curses.cbreak # provide unbuffered input
|
24
|
+
Curses.noecho # turn off input echoing
|
25
|
+
Curses.nonl # turn off newline translation
|
26
|
+
Curses.stdscr.keypad(true) # turn on keypad mode
|
27
|
+
# Curses.stdscr.nodelay = 1
|
28
|
+
|
26
29
|
|
27
30
|
count = 0
|
28
31
|
loop do
|
29
|
-
count = (count + 1) % 20
|
30
32
|
key = Curses.getch
|
33
|
+
next if key == 4294967295 # placeholder key when using nodelay mode
|
31
34
|
break if key == ?\C-c
|
35
|
+
count = (count + 1) % 20
|
32
36
|
Curses.setpos(count,0)
|
33
37
|
Curses.addstr("#{key.inspect} ");
|
34
38
|
end
|
data/Readme.md
CHANGED
@@ -23,7 +23,8 @@ Usage
|
|
23
23
|
|
24
24
|
TODO
|
25
25
|
=====
|
26
|
-
-
|
26
|
+
- support typing unicode like äöß etc (rework size / make strings utf8-aware)
|
27
|
+
- ask before quitting on changed file
|
27
28
|
- bind/action must use instance_exec
|
28
29
|
- read .rucorc.rb
|
29
30
|
- write key binding guide
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/bin/ruco
CHANGED
@@ -38,8 +38,9 @@ def write(line,row,text)
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def init_screen
|
41
|
-
Curses.noecho # do not show typed chars
|
42
41
|
Curses.init_screen
|
42
|
+
Curses.noecho # do not show typed chars
|
43
|
+
Curses.nonl # turn off newline translation
|
43
44
|
Curses.stdscr.keypad(true) # enable arrow keys
|
44
45
|
Curses.raw # give us all other keys
|
45
46
|
|
data/lib/ruco.rb
CHANGED
data/lib/ruco/application.rb
CHANGED
@@ -50,11 +50,7 @@ module Ruco
|
|
50
50
|
when :tab then @focused.insert("\t")
|
51
51
|
when 32..126 then @focused.insert(key.chr) # printable
|
52
52
|
when :enter then
|
53
|
-
|
54
|
-
if result.is_a?(Ruco::Command)
|
55
|
-
result.send_to(@editor)
|
56
|
-
@focused = @editor
|
57
|
-
end
|
53
|
+
@focused.insert("\n")
|
58
54
|
when :backspace then @focused.delete(-1)
|
59
55
|
when :delete then @focused.delete(1)
|
60
56
|
|
@@ -65,6 +61,7 @@ module Ruco
|
|
65
61
|
end
|
66
62
|
|
67
63
|
def bind(key, action=nil, &block)
|
64
|
+
raise "Ctrl+m cannot be bound" if key == :"Ctrl+m" # would shadow enter -> bad
|
68
65
|
raise if action and block
|
69
66
|
@bindings[key] = action || block
|
70
67
|
end
|
@@ -73,6 +70,14 @@ module Ruco
|
|
73
70
|
@actions[name] = block
|
74
71
|
end
|
75
72
|
|
73
|
+
def ask(question, options={}, &block)
|
74
|
+
@focused = @command
|
75
|
+
@command.ask(question, options) do |response|
|
76
|
+
@focused = @editor
|
77
|
+
block.call(response)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
76
81
|
private
|
77
82
|
|
78
83
|
def setup_actions
|
@@ -85,8 +90,7 @@ module Ruco
|
|
85
90
|
end
|
86
91
|
|
87
92
|
action :go_to_line do
|
88
|
-
@
|
89
|
-
@command.move_to_line
|
93
|
+
ask('Go to Line: '){|result| @editor.move(:to_line, result.to_i) }
|
90
94
|
end
|
91
95
|
|
92
96
|
action :delete_line do
|
@@ -94,8 +98,7 @@ module Ruco
|
|
94
98
|
end
|
95
99
|
|
96
100
|
action :find do
|
97
|
-
|
98
|
-
@command.find
|
101
|
+
ask("Find: ", :cache => true){|result| @editor.find(result) }
|
99
102
|
end
|
100
103
|
end
|
101
104
|
|
data/lib/ruco/command_bar.rb
CHANGED
@@ -25,23 +25,17 @@ module Ruco
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
29
|
-
@
|
30
|
-
@
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
def move_to_line
|
37
|
-
@form = Form.new('Go to Line: ', :columns => @options[:columns], :type => :integer) do |value|
|
38
|
-
@form = nil
|
39
|
-
Command.new(:move, :to_line, value.to_i)
|
28
|
+
def ask(question, options={}, &block)
|
29
|
+
@form = cached_form_if(options[:cache], question) do
|
30
|
+
Form.new(question, :columns => @options[:columns]) do |result|
|
31
|
+
@form = nil
|
32
|
+
block.call(result)
|
33
|
+
end
|
40
34
|
end
|
41
35
|
end
|
42
36
|
|
43
37
|
def reset
|
44
|
-
@forms_cache[
|
38
|
+
@forms_cache[@forms_cache.index(@form)] = nil
|
45
39
|
@form = nil
|
46
40
|
end
|
47
41
|
|
@@ -55,6 +49,14 @@ module Ruco
|
|
55
49
|
|
56
50
|
private
|
57
51
|
|
52
|
+
def cached_form_if(cache, question)
|
53
|
+
if cache
|
54
|
+
@forms_cache[question] ||= yield
|
55
|
+
else
|
56
|
+
yield
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
58
60
|
def available_shortcuts
|
59
61
|
used_columns = 0
|
60
62
|
spacer = ' '
|
data/lib/ruco/form.rb
CHANGED
@@ -15,11 +15,7 @@ module Ruco
|
|
15
15
|
|
16
16
|
def insert(text)
|
17
17
|
@text_field.insert(text.gsub("\n",''))
|
18
|
-
if text.include?("\n")
|
19
|
-
result = @text_field.value
|
20
|
-
result = result.to_i if @options[:type] == :integer
|
21
|
-
@submit.call(result)
|
22
|
-
end
|
18
|
+
@submit.call(@text_field.value) if text.include?("\n")
|
23
19
|
end
|
24
20
|
|
25
21
|
def cursor
|
data/lib/ruco/keyboard.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
class Keyboard
|
2
|
+
A_TO_Z = ('a'..'z').to_a
|
3
|
+
|
2
4
|
def self.listen
|
3
5
|
loop do
|
4
6
|
key = Curses.getch
|
@@ -17,19 +19,15 @@ class Keyboard
|
|
17
19
|
|
18
20
|
# modify
|
19
21
|
when 9 then :tab
|
22
|
+
when 13 then :enter # shadows Ctrl+m
|
20
23
|
when 32..126 then key # printable
|
21
|
-
when
|
22
|
-
when 263 then :backspace
|
24
|
+
when 263, 127 then :backspace # ubuntu / mac
|
23
25
|
when Curses::KEY_DC then :delete
|
24
26
|
|
25
27
|
# misc
|
26
|
-
when
|
27
|
-
when
|
28
|
-
when ?\C-g then :"Ctrl+g"
|
28
|
+
when 0 then :"Ctrl+space"
|
29
|
+
when 1..26 then :"Ctrl+#{A_TO_Z[key-1]}"
|
29
30
|
when 27 then :escape
|
30
|
-
when ?\C-s then :"Ctrl+s"
|
31
|
-
when ?\C-w then :"Ctrl+w"
|
32
|
-
when ?\C-q then :"Ctrl+q"
|
33
31
|
else
|
34
32
|
key
|
35
33
|
end
|
data/ruco.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ruco}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.9"
|
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"]
|
@@ -22,7 +22,6 @@ Gem::Specification.new do |s|
|
|
22
22
|
"bin/ruco",
|
23
23
|
"lib/ruco.rb",
|
24
24
|
"lib/ruco/application.rb",
|
25
|
-
"lib/ruco/command.rb",
|
26
25
|
"lib/ruco/command_bar.rb",
|
27
26
|
"lib/ruco/core_ext/array.rb",
|
28
27
|
"lib/ruco/core_ext/object.rb",
|
@@ -37,7 +36,6 @@ Gem::Specification.new do |s|
|
|
37
36
|
"ruco.gemspec",
|
38
37
|
"spec/ruco/application_spec.rb",
|
39
38
|
"spec/ruco/command_bar_spec.rb",
|
40
|
-
"spec/ruco/command_spec.rb",
|
41
39
|
"spec/ruco/core_ext/string_spec.rb",
|
42
40
|
"spec/ruco/editor_spec.rb",
|
43
41
|
"spec/ruco/form_spec.rb",
|
@@ -53,7 +51,6 @@ Gem::Specification.new do |s|
|
|
53
51
|
s.test_files = [
|
54
52
|
"spec/ruco/application_spec.rb",
|
55
53
|
"spec/ruco/command_bar_spec.rb",
|
56
|
-
"spec/ruco/command_spec.rb",
|
57
54
|
"spec/ruco/core_ext/string_spec.rb",
|
58
55
|
"spec/ruco/editor_spec.rb",
|
59
56
|
"spec/ruco/form_spec.rb",
|
@@ -15,91 +15,69 @@ describe Ruco::CommandBar do
|
|
15
15
|
bar.view.should == "^W Exit ^S Save"
|
16
16
|
end
|
17
17
|
|
18
|
-
describe :
|
19
|
-
it "sets command bar into
|
20
|
-
bar.
|
18
|
+
describe :ask do
|
19
|
+
it "sets command bar into question mode" do
|
20
|
+
bar.ask('Find: '){}
|
21
21
|
bar.view.should == "Find: "
|
22
22
|
bar.cursor.column.should == 6
|
23
23
|
end
|
24
24
|
|
25
|
-
it "can enter
|
26
|
-
bar.
|
25
|
+
it "can enter answer" do
|
26
|
+
bar.ask('Find: '){}
|
27
27
|
bar.insert('abc')
|
28
28
|
bar.view.should == "Find: abc"
|
29
29
|
bar.cursor.column.should == 9
|
30
30
|
end
|
31
31
|
|
32
|
-
it "
|
33
|
-
bar.
|
32
|
+
it "gets reset when submitting" do
|
33
|
+
bar.ask('Find: '){}
|
34
|
+
bar.insert("123\n")
|
35
|
+
bar.view.should == default_view
|
36
|
+
end
|
37
|
+
|
38
|
+
it "keeps entered answer when cached" do
|
39
|
+
bar.ask('Find: ', :cache => true){}
|
34
40
|
bar.insert('abc')
|
35
41
|
bar.insert("\n")
|
36
|
-
bar.
|
42
|
+
bar.ask('Find: ', :cache => true){}
|
37
43
|
bar.view.should == "Find: abc"
|
38
44
|
bar.cursor.column.should == 9
|
39
45
|
end
|
40
46
|
|
41
|
-
it "
|
42
|
-
bar.
|
47
|
+
it "reset the question when cached" do
|
48
|
+
bar.ask('Find: ', :cache => true){}
|
43
49
|
bar.insert('abc')
|
44
50
|
bar.reset
|
45
51
|
|
46
52
|
bar.view.should == default_view
|
47
|
-
bar.
|
53
|
+
bar.ask('Find: ', :cache => true){}
|
48
54
|
bar.view.should == "Find: " # term removed
|
49
55
|
end
|
50
56
|
|
51
|
-
it "
|
52
|
-
bar.
|
53
|
-
bar.insert(
|
54
|
-
result = bar.insert("d\n")
|
55
|
-
result.should == Ruco::Command.new(:find, 'abcd')
|
56
|
-
end
|
57
|
+
it "does not reset all cached questions" do
|
58
|
+
bar.ask('Find: ', :cache => true){}
|
59
|
+
bar.insert("abc\n")
|
57
60
|
|
58
|
-
|
59
|
-
bar.
|
60
|
-
bar.
|
61
|
-
bar.insert("\n")
|
62
|
-
bar.find
|
63
|
-
result = bar.insert("\n")
|
64
|
-
result.should == Ruco::Command.new(:find, 'abcd')
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe :move_to_line do
|
69
|
-
it "displays a form" do
|
70
|
-
bar.move_to_line
|
71
|
-
bar.view.should == "Go to Line: "
|
72
|
-
end
|
61
|
+
bar.ask('Foo: ', :cache => true){}
|
62
|
+
bar.reset # clears Foo not Find
|
63
|
+
bar.view.should == default_view
|
73
64
|
|
74
|
-
|
75
|
-
bar.
|
76
|
-
bar.insert('123')
|
77
|
-
result = bar.insert("\n")
|
78
|
-
result.should == Ruco::Command.new(:move, :to_line, 123)
|
65
|
+
bar.ask('Find: ', :cache => true){}
|
66
|
+
bar.view.should == "Find: abc"
|
79
67
|
end
|
80
68
|
|
81
|
-
it "gets reset when starting a new
|
82
|
-
bar.
|
69
|
+
it "gets reset when starting a new question" do
|
70
|
+
bar.ask('Find: '){}
|
83
71
|
bar.insert('123')
|
84
|
-
bar.
|
85
|
-
bar.view.should == "
|
86
|
-
end
|
87
|
-
|
88
|
-
it "gets reset when submitting" do
|
89
|
-
bar.move_to_line
|
90
|
-
bar.insert("123\n")
|
91
|
-
bar.view.should == default_view
|
72
|
+
bar.ask('Find: '){}
|
73
|
+
bar.view.should == "Find: "
|
92
74
|
end
|
93
75
|
|
94
|
-
it "
|
95
|
-
bar.
|
76
|
+
it "can execute" do
|
77
|
+
bar.ask('Find: ', :cache => true){|r| @result = r }
|
96
78
|
bar.insert('abc')
|
97
|
-
bar.
|
98
|
-
|
99
|
-
|
100
|
-
bar.view.should == default_view
|
101
|
-
bar.find
|
102
|
-
bar.view.should == "Find: abc"
|
79
|
+
bar.insert("d\n")
|
80
|
+
@result.should == 'abcd'
|
103
81
|
end
|
104
82
|
end
|
105
83
|
end
|
data/spec/ruco/form_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path('spec/spec_helper')
|
2
2
|
|
3
3
|
describe Ruco::Form do
|
4
|
-
let(:form){ Ruco::Form.new('Test', :columns => 30){|value|
|
4
|
+
let(:form){ Ruco::Form.new('Test', :columns => 30){|value| @result = value } }
|
5
5
|
|
6
6
|
it "positions cursor in text field" do
|
7
7
|
form.cursor.should == [0, 5]
|
@@ -13,13 +13,15 @@ describe Ruco::Form do
|
|
13
13
|
form.cursor.should == [0, 8]
|
14
14
|
end
|
15
15
|
|
16
|
-
it "
|
17
|
-
form.insert('abc')
|
16
|
+
it "does not return result on normal insert" do
|
17
|
+
form.insert('abc')
|
18
|
+
@result.should == nil
|
18
19
|
end
|
19
20
|
|
20
|
-
it "returns
|
21
|
+
it "returns result on enter" do
|
21
22
|
form.insert('abc')
|
22
|
-
form.insert("d\n")
|
23
|
+
form.insert("d\n")
|
24
|
+
@result.should == "abcd"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
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:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Grosser
|
@@ -36,7 +36,6 @@ files:
|
|
36
36
|
- bin/ruco
|
37
37
|
- lib/ruco.rb
|
38
38
|
- lib/ruco/application.rb
|
39
|
-
- lib/ruco/command.rb
|
40
39
|
- lib/ruco/command_bar.rb
|
41
40
|
- lib/ruco/core_ext/array.rb
|
42
41
|
- lib/ruco/core_ext/object.rb
|
@@ -51,7 +50,6 @@ files:
|
|
51
50
|
- ruco.gemspec
|
52
51
|
- spec/ruco/application_spec.rb
|
53
52
|
- spec/ruco/command_bar_spec.rb
|
54
|
-
- spec/ruco/command_spec.rb
|
55
53
|
- spec/ruco/core_ext/string_spec.rb
|
56
54
|
- spec/ruco/editor_spec.rb
|
57
55
|
- spec/ruco/form_spec.rb
|
@@ -96,7 +94,6 @@ summary: Commandline editor written in ruby
|
|
96
94
|
test_files:
|
97
95
|
- spec/ruco/application_spec.rb
|
98
96
|
- spec/ruco/command_bar_spec.rb
|
99
|
-
- spec/ruco/command_spec.rb
|
100
97
|
- spec/ruco/core_ext/string_spec.rb
|
101
98
|
- spec/ruco/editor_spec.rb
|
102
99
|
- spec/ruco/form_spec.rb
|
data/lib/ruco/command.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
module Ruco
|
2
|
-
# Used to pass around commands
|
3
|
-
class Command
|
4
|
-
attr_reader :method, :args
|
5
|
-
|
6
|
-
def initialize(method, *args)
|
7
|
-
@method = method
|
8
|
-
@args = args
|
9
|
-
end
|
10
|
-
|
11
|
-
def send_to(object)
|
12
|
-
object.send(@method, *@args)
|
13
|
-
end
|
14
|
-
|
15
|
-
def ==(other)
|
16
|
-
other.method == method and other.args == args
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/spec/ruco/command_spec.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require File.expand_path('spec/spec_helper')
|
2
|
-
|
3
|
-
describe Ruco::Command do
|
4
|
-
it "can invoke a recorded command" do
|
5
|
-
Ruco::Command.new(:slice, 0,1).send_to("ab1").should == 'a'
|
6
|
-
end
|
7
|
-
|
8
|
-
it "can invoke a recorded command without arguments" do
|
9
|
-
Ruco::Command.new(:strip).send_to(" a ").should == 'a'
|
10
|
-
end
|
11
|
-
end
|