rawline 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rawline.rb CHANGED
@@ -1,119 +1,109 @@
1
- #!usr/bin/env ruby
2
-
3
- #
4
- # RawLine.rb
5
- #
6
- # Created by Fabio Cevasco on 2008-03-01.
7
- # Copyright (c) 2008 Fabio Cevasco. All rights reserved.
8
- #
9
- # This is Free Software. See LICENSE for details.
10
- #
11
-
12
- require "rubygems"
13
-
14
- #
15
- # The RawLine (or Rawline) module can be used in the same way
16
- # as the Readline one.
17
- #
18
- # <tt>require 'rawline'</tt>
19
- # <tt>include Rawline</tt>
20
- #
21
- # You'll get...
22
- #
23
- # * <tt>readline(prompt="", add_history=false)</tt> - to read characters from $stdin
24
- # * <tt>Rawline::HISTORY</tt> - to access line history (an instance of RawLine::HistoryBuffer)
25
- # * <tt>Rawline::FILENAME_COMPLETION_PROC</tt> - a Proc object used for filename completion
26
- # * <tt>Rawline.completion_proc</tt> - the Proc object used for TAB completion (defaults to FILENAME_COMPLETION_PROC).
27
- # * <tt>Rawline.completion_matches</tt> - an array of completion matches.
28
- # * <tt>Rawline.completion_append_char</tt> - a character to append after a successful completion.
29
- # * <tt>Rawline.basic_word_break_characters</tt> - a String listing all the characters used as word separators.
30
- # * <tt>Rawline.completer_word_break_characters</tt> - same as above.
31
- # * <tt>Rawline.library_version</tt> - the current version of the Rawline library.
32
- # * <tt>Rawline.clear_history</tt> - to clear the current history.
33
- # * <tt>Rawline.match_hidden_files</tt> - whether FILENAME_COMPLETION_PROC matches hidden files and folders or not.
34
- #
35
- # And also <tt>Rawline.editor</tt>, an instance of RawLine::Editor which can be used for anything you like.
36
- #
37
- module RawLine
38
-
39
- def self.rawline_version
40
- "0.3.0"
41
- end
42
-
43
- class BindingException < RuntimeError; end
44
-
45
- if RUBY_PLATFORM.match(/mswin/i) then
46
- begin
47
- def self.win32console?; true; end
48
- def self.ansi?; true; end
49
- rescue Exception
50
- def self.win32console?; false; end
51
- def self.ansi?; false; end
52
- end
53
- else # Unix-like
54
- def self.ansi?; true; end
55
- end
56
- end
57
-
58
- Rawline = RawLine
59
-
60
- dir = File.dirname(File.expand_path(__FILE__))
61
- require "highline"
62
- require "#{dir}/rawline/terminal"
63
- require "#{dir}/rawline/terminal/windows_terminal"
64
- require "#{dir}/rawline/terminal/vt220_terminal"
65
- require "#{dir}/rawline/history_buffer"
66
- require "#{dir}/rawline/line"
67
- require "#{dir}/rawline/editor"
68
-
69
- module RawLine
70
- self.instance_eval do
71
- class << self; attr_accessor :editor, :implemented_methods; end
72
- @editor = RawLine::Editor.new
73
-
74
- @implemented_methods =
75
- [
76
- :completion_proc,
77
- :completion_proc=,
78
- :completion_matches,
79
- :completion_append_char,
80
- :completion_append_char=,
81
- :basic_word_break_characters,
82
- :basic_word_break_characters=,
83
- :completer_word_break_characters,
84
- :completer_word_break_characters=,
85
- :library_version,
86
- :clear_history,
87
- :match_hidden_files,
88
- :match_hidden_files=
89
- ]
90
-
91
- self.module_eval do
92
- HISTORY = RawLine.editor.history
93
- FILENAME_COMPLETION_PROC = RawLine.editor.filename_completion_proc
94
-
95
- def readline(prompt="", add_history=false)
96
- RawLine.editor.read prompt, add_history
97
- end
98
-
99
- alias rawline readline
100
- end
101
-
102
- @implemented_methods.each do |meth|
103
- self.class.module_eval do
104
- define_method meth do |*args|
105
- case args.length
106
- when 0 then
107
- @editor.send(meth)
108
- when 1 then
109
- @editor.send(meth, args[0])
110
- else
111
- raise ArgumentError, "There are no Readline methods with more than one parameter"
112
- end
113
- end
114
- end
115
- end
116
-
117
- end
118
-
119
- end
1
+ #!usr/bin/env ruby
2
+
3
+ #
4
+ # RawLine.rb
5
+ #
6
+ # Created by Fabio Cevasco on 2008-03-01.
7
+ # Copyright (c) 2008 Fabio Cevasco. All rights reserved.
8
+ #
9
+ # This is Free Software. See LICENSE for details.
10
+ #
11
+
12
+ require "rubygems"
13
+
14
+ #
15
+ # The RawLine (or Rawline) module can be used in the same way
16
+ # as the Readline one.
17
+ #
18
+ module RawLine
19
+
20
+ def self.rawline_version
21
+ "0.3.1"
22
+ end
23
+
24
+ class BindingException < RuntimeError; end
25
+
26
+ if RUBY_PLATFORM.match(/mswin/i) then
27
+ begin
28
+ require 'win32console'
29
+ def self.win32console?; true; end
30
+ def self.ansi?; true; end
31
+ rescue Exception
32
+ def self.win32console?; false; end
33
+ def self.ansi?; false; end
34
+ end
35
+ else # Unix-like
36
+ def self.ansi?; true; end
37
+ end
38
+ end
39
+
40
+ # Adding Fixnum#ord for Ruby 1.8.6
41
+ class Fixnum; def ord; self; end; end unless Fixnum.method_defined? :ord
42
+
43
+ Rawline = RawLine
44
+
45
+ dir = File.dirname(File.expand_path(__FILE__))
46
+ require "highline"
47
+ require "#{dir}/rawline/terminal"
48
+ require "#{dir}/rawline/terminal/windows_terminal"
49
+ require "#{dir}/rawline/terminal/vt220_terminal"
50
+ require "#{dir}/rawline/history_buffer"
51
+ require "#{dir}/rawline/line"
52
+ require "#{dir}/rawline/editor"
53
+
54
+ module RawLine
55
+ self.instance_eval do
56
+ class << self; attr_accessor :editor end
57
+ @editor = RawLine::Editor.new
58
+
59
+ @implemented_methods =
60
+ [
61
+ :completion_proc,
62
+ :completion_proc=,
63
+ :completion_matches,
64
+ :completion_append_character,
65
+ :completion_append_character=,
66
+ :basic_word_break_characters,
67
+ :basic_word_break_characters=,
68
+ :completer_word_break_characters,
69
+ :completer_word_break_characters=,
70
+ :library_version,
71
+ :clear_history,
72
+ :match_hidden_files,
73
+ :match_hidden_files=
74
+ ]
75
+
76
+ @implemented_methods.each do |meth|
77
+ self.class.module_eval do
78
+ define_method meth do |*args|
79
+ case args.length
80
+ when 0 then
81
+ @editor.send(meth)
82
+ when 1 then
83
+ @editor.send(meth, args[0])
84
+ else
85
+ raise ArgumentError, "There are no Readline methods with more than one parameter"
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ HISTORY = RawLine.editor.history
94
+ FILENAME_COMPLETION_PROC = RawLine.editor.filename_completion_proc
95
+
96
+ readline_method = lambda do
97
+
98
+ def readline(prompt="", add_history=false)
99
+ RawLine.editor.read prompt, add_history
100
+ end
101
+
102
+ alias rawline readline
103
+ end
104
+
105
+ self.class.module_eval &readline_method
106
+ self.module_eval &readline_method
107
+
108
+ end
109
+ end
data/spec/editor_spec.rb CHANGED
@@ -1,167 +1,167 @@
1
- #!/usr/bin/env ruby
2
-
3
- dir = File.dirname(File.expand_path(__FILE__))+'/..'
4
-
5
- require 'highline/system_extensions'
6
-
7
- module HighLine::SystemExtensions
8
- # Override Windows' character reading so it's not tied to STDIN.
9
- def get_character( input = STDIN )
10
- input.getbyte
11
- end
12
- end
13
-
14
- require 'stringio'
15
- require "#{dir}/lib/rawline"
16
-
17
- describe RawLine::Editor do
18
-
19
- before :each do
20
- @output = StringIO.new
21
- @input = StringIO.new
22
- @editor = RawLine::Editor.new(@input, @output)
23
- end
24
-
25
- it "reads raw characters from @input" do
26
- @input << "test #1"
27
- @input.rewind
28
- @editor.read
29
- @editor.line.text.should == "test #1"
30
- @output.string.should == "test #1\n"
31
- end
32
-
33
- it "can bind keys to code blocks" do
34
- @editor.bind(:ctrl_w) { @editor.write "test #2a" }
35
- @editor.bind(?\C-q) { "test #2b" }
36
- @editor.bind(21) { "test #2c" }
37
- @editor.bind([22]) { "test #2d" }
38
- @editor.terminal.escape_codes = [] # remove any existing escape codes
39
- lambda {@editor.bind({:test => [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord]}) { "test #2e" }}.should raise_error(RawLine::BindingException)
40
- @editor.terminal.escape_codes << ?\e.ord
41
- lambda {@editor.bind({:test => "\etest"}) { "test #2e" }}.should_not raise_error(RawLine::BindingException)
42
- lambda {@editor.bind("\etest2") { "test #2f" }}.should_not raise_error(RawLine::BindingException)
43
- @input << ?\C-w.chr
44
- @input.rewind
45
- @editor.read
46
- @editor.line.text.should == "test #2a"
47
- @editor.char = [?\C-q.ord]
48
- @editor.press_key.should == "test #2b"
49
- @editor.char = [?\C-u.ord]
50
- @editor.press_key.should == "test #2c"
51
- @editor.char = [?\C-v.ord]
52
- @editor.press_key.should == "test #2d"
53
- @editor.char = [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord]
54
- @editor.press_key.should == "test #2e"
55
- @editor.char = [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord, ?2.ord]
56
- @editor.press_key.should == "test #2f"
57
- end
58
-
59
- it "keeps track of the cursor position" do
60
- @input << "test #4"
61
- @input.rewind
62
- @editor.read
63
- @editor.line.position.should == 7
64
- 3.times { @editor.move_left }
65
- @editor.line.position.should == 4
66
- 2.times { @editor.move_right }
67
- @editor.line.position.should == 6
68
- end
69
-
70
- it "can delete characters" do
71
- @input << "test #5"
72
- @input.rewind
73
- @editor.read
74
- 3.times { @editor.move_left }
75
- 4.times { @editor.delete_left_character }
76
- 3.times { @editor.delete_character }
77
- @editor.line.text.should == ""
78
- @editor.line.position.should == 0
79
- end
80
-
81
- it "can clear the whole line" do
82
- @input << "test #5"
83
- @input.rewind
84
- @editor.read
85
- @editor.clear_line
86
- @editor.line.text.should == ""
87
- @editor.line.position.should == 0
88
- end
89
-
90
- it "supports undo and redo" do
91
- @input << "test #6"
92
- @input.rewind
93
- @editor.read
94
- 3.times { @editor.delete_left_character }
95
- 2.times { @editor.undo }
96
- @editor.line.text.should == "test #"
97
- 2.times { @editor.redo }
98
- @editor.line.text.should == "test"
99
- end
100
-
101
- it "supports history" do
102
- @input << "test #7a"
103
- @input.rewind
104
- @editor.read "", true
105
- @editor.newline
106
- @input << "test #7b"
107
- @input.pos = 8
108
- @editor.read "", true
109
- @editor.newline
110
- @input << "test #7c"
111
- @input.pos = 16
112
- @editor.read "", true
113
- @editor.newline
114
- @input << "test #7d"
115
- @input.pos = 24
116
- @editor.read "", true
117
- @editor.newline
118
- @editor.history_back
119
- @editor.line.text.should == "test #7c"
120
- 10.times { @editor.history_back }
121
- @editor.line.text.should == "test #7a"
122
- 2.times { @editor.history_forward }
123
- @editor.line.text.should == "test #7c"
124
- end
125
-
126
- it "can overwrite lines" do
127
- @input << "test #8a"
128
- @input.rewind
129
- @editor.read
130
- @editor.overwrite_line("test #8b", 2)
131
- @editor.line.text.should == "test #8b"
132
- @editor.line.position.should == 2
133
- end
134
-
135
- it "can complete words" do
136
- @editor.completion_append_string = "\t"
137
- @editor.bind(:tab) { @editor.complete }
138
- @editor.completion_proc = lambda do |word|
139
- if word then
140
- ['select', 'update', 'delete', 'debug', 'destroy'].find_all { |e| e.match(/^#{Regexp.escape(word)}/) }
141
- end
142
- end
143
- @input << "test #9 de" << ?\t.chr << ?\t.chr
144
- @input.rewind
145
- @editor.read
146
- @editor.line.text.should == "test #9 delete\t"
147
- end
148
-
149
- it "supports INSERT and REPLACE modes" do
150
- @input << "test 0"
151
- @editor.terminal.keys[:left_arrow].each { |k| @input << k.chr }
152
- @input << "#1"
153
- @input.rewind
154
- @editor.read
155
- @editor.line.text.should == "test #10"
156
- @editor.toggle_mode
157
- @input << "test 0"
158
- @editor.terminal.keys[:left_arrow].each { |k| @input << k.chr }
159
- @input << "#1"
160
- @input.rewind
161
- @editor.read
162
- @editor.line.text.should == "test #1test #1"
163
- end
164
-
165
-
166
- end
167
-
1
+ #!/usr/bin/env ruby
2
+
3
+ dir = File.dirname(File.expand_path(__FILE__))+'/..'
4
+
5
+ require 'highline/system_extensions'
6
+
7
+ module HighLine::SystemExtensions
8
+ # Override Windows' character reading so it's not tied to STDIN.
9
+ def get_character( input = STDIN )
10
+ (RUBY_VERSION.gsub(/1\./, '').to_f >= 8.7) ? input.getbyte : input.getc
11
+ end
12
+ end
13
+
14
+ require 'stringio'
15
+ require "#{dir}/lib/rawline"
16
+
17
+ describe RawLine::Editor do
18
+
19
+ before :each do
20
+ @output = StringIO.new
21
+ @input = StringIO.new
22
+ @editor = RawLine::Editor.new(@input, @output)
23
+ end
24
+
25
+ it "reads raw characters from @input" do
26
+ @input << "test #1"
27
+ @input.rewind
28
+ @editor.read
29
+ @editor.line.text.should == "test #1"
30
+ @output.string.should == "test #1\n"
31
+ end
32
+
33
+ it "can bind keys to code blocks" do
34
+ @editor.bind(:ctrl_w) { @editor.write "test #2a" }
35
+ @editor.bind(?\C-q) { "test #2b" }
36
+ @editor.bind(21) { "test #2c" }
37
+ @editor.bind([22]) { "test #2d" }
38
+ @editor.terminal.escape_codes = [] # remove any existing escape codes
39
+ lambda {@editor.bind({:test => [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord]}) { "test #2e" }}.should raise_error(RawLine::BindingException)
40
+ @editor.terminal.escape_codes << ?\e.ord
41
+ lambda {@editor.bind({:test => "\etest"}) { "test #2e" }}.should_not raise_error(RawLine::BindingException)
42
+ lambda {@editor.bind("\etest2") { "test #2f" }}.should_not raise_error(RawLine::BindingException)
43
+ @input << ?\C-w.chr
44
+ @input.rewind
45
+ @editor.read
46
+ @editor.line.text.should == "test #2a"
47
+ @editor.char = [?\C-q.ord]
48
+ @editor.press_key.should == "test #2b"
49
+ @editor.char = [?\C-u.ord]
50
+ @editor.press_key.should == "test #2c"
51
+ @editor.char = [?\C-v.ord]
52
+ @editor.press_key.should == "test #2d"
53
+ @editor.char = [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord]
54
+ @editor.press_key.should == "test #2e"
55
+ @editor.char = [?\e.ord, ?t.ord, ?e.ord, ?s.ord, ?t.ord, ?2.ord]
56
+ @editor.press_key.should == "test #2f"
57
+ end
58
+
59
+ it "keeps track of the cursor position" do
60
+ @input << "test #4"
61
+ @input.rewind
62
+ @editor.read
63
+ @editor.line.position.should == 7
64
+ 3.times { @editor.move_left }
65
+ @editor.line.position.should == 4
66
+ 2.times { @editor.move_right }
67
+ @editor.line.position.should == 6
68
+ end
69
+
70
+ it "can delete characters" do
71
+ @input << "test #5"
72
+ @input.rewind
73
+ @editor.read
74
+ 3.times { @editor.move_left }
75
+ 4.times { @editor.delete_left_character }
76
+ 3.times { @editor.delete_character }
77
+ @editor.line.text.should == ""
78
+ @editor.line.position.should == 0
79
+ end
80
+
81
+ it "can clear the whole line" do
82
+ @input << "test #5"
83
+ @input.rewind
84
+ @editor.read
85
+ @editor.clear_line
86
+ @editor.line.text.should == ""
87
+ @editor.line.position.should == 0
88
+ end
89
+
90
+ it "supports undo and redo" do
91
+ @input << "test #6"
92
+ @input.rewind
93
+ @editor.read
94
+ 3.times { @editor.delete_left_character }
95
+ 2.times { @editor.undo }
96
+ @editor.line.text.should == "test #"
97
+ 2.times { @editor.redo }
98
+ @editor.line.text.should == "test"
99
+ end
100
+
101
+ it "supports history" do
102
+ @input << "test #7a"
103
+ @input.rewind
104
+ @editor.read "", true
105
+ @editor.newline
106
+ @input << "test #7b"
107
+ @input.pos = 8
108
+ @editor.read "", true
109
+ @editor.newline
110
+ @input << "test #7c"
111
+ @input.pos = 16
112
+ @editor.read "", true
113
+ @editor.newline
114
+ @input << "test #7d"
115
+ @input.pos = 24
116
+ @editor.read "", true
117
+ @editor.newline
118
+ @editor.history_back
119
+ @editor.line.text.should == "test #7c"
120
+ 10.times { @editor.history_back }
121
+ @editor.line.text.should == "test #7a"
122
+ 2.times { @editor.history_forward }
123
+ @editor.line.text.should == "test #7c"
124
+ end
125
+
126
+ it "can overwrite lines" do
127
+ @input << "test #8a"
128
+ @input.rewind
129
+ @editor.read
130
+ @editor.overwrite_line("test #8b", 2)
131
+ @editor.line.text.should == "test #8b"
132
+ @editor.line.position.should == 2
133
+ end
134
+
135
+ it "can complete words" do
136
+ @editor.completion_append_string = "\t"
137
+ @editor.bind(:tab) { @editor.complete }
138
+ @editor.completion_proc = lambda do |word|
139
+ if word then
140
+ ['select', 'update', 'delete', 'debug', 'destroy'].find_all { |e| e.match(/^#{Regexp.escape(word)}/) }
141
+ end
142
+ end
143
+ @input << "test #9 de" << ?\t.chr << ?\t.chr
144
+ @input.rewind
145
+ @editor.read
146
+ @editor.line.text.should == "test #9 delete\t"
147
+ end
148
+
149
+ it "supports INSERT and REPLACE modes" do
150
+ @input << "test 0"
151
+ @editor.terminal.keys[:left_arrow].each { |k| @input << k.chr }
152
+ @input << "#1"
153
+ @input.rewind
154
+ @editor.read
155
+ @editor.line.text.should == "test #10"
156
+ @editor.toggle_mode
157
+ @input << "test 0"
158
+ @editor.terminal.keys[:left_arrow].each { |k| @input << k.chr }
159
+ @input << "#1"
160
+ @input.rewind
161
+ @editor.read
162
+ @editor.line.text.should == "test #1test #1"
163
+ end
164
+
165
+
166
+ end
167
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rawline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Cevasco
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-01 00:00:00 +01:00
12
+ date: 2009-03-07 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -43,8 +43,10 @@ files:
43
43
  - lib/rawline/terminal/windows_terminal.rb
44
44
  - lib/rawline/terminal/vt220_terminal.rb
45
45
  - examples/readline_emulation.rb
46
+ - examples/rawline_rush.rb
46
47
  - examples/key_tester.rb
47
48
  - examples/rawline_shell.rb
49
+ - examples/rawline_irb.rb
48
50
  - spec/history_buffer_spec.rb
49
51
  - spec/line_spec.rb
50
52
  - spec/rawline_spec.rb