Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaner Readline implementation

  • Loading branch information...
commit 90c98cea05120118298459f33840884f499ceb4c 1 parent 8262eeb
@cldwalker authored
View
4 lib/bond.rb
@@ -1,7 +1,9 @@
require 'bond/m'
require 'bond/version'
require 'bond/readline'
-require 'bond/rawline'
+require 'bond/readlines/rawline'
+require 'bond/readlines/ruby'
+require 'bond/readlines/jruby'
require 'bond/agent'
require 'bond/search'
require 'bond/input'
View
16 lib/bond/rawline.rb
@@ -1,16 +0,0 @@
-module Bond
- # A readline plugin for use with {Rawline}[http://github.com/h3rald/rawline]. This plugin
- # should be used in conjunction with {a Rawline shell}[http://www.h3rald.com/articles/real-world-rawline-usage].
- class Rawline
- def self.setup(agent)
- require 'rawline'
- ::Rawline.completion_append_character = nil
- ::Rawline.basic_word_break_characters= " \t\n\"\\'`><;|&{("
- ::Rawline.completion_proc = agent
- end
-
- def self.line_buffer
- ::Rawline.editor.line.text
- end
- end
-end
View
70 lib/bond/readline.rb
@@ -1,52 +1,38 @@
-module Bond
- # This is the default readline plugin for Bond. A valid plugin must be a module that defines methods setup
- # and line_buffer as described below.
- class Readline
- DefaultBreakCharacters = " \t\n\"\\'`><=;|&{("
+# This is the default readline plugin for Bond. A valid plugin must be an object
+# that responds to methods setup and line_buffer as described below.
+class Bond::Readline
+ DefaultBreakCharacters = " \t\n\"\\'`><=;|&{("
- # Loads the readline-like library and sets the completion_proc to the given agent.
- def self.setup(agent)
- if RUBY_PLATFORM[/mswin|mingw|bccwin|wince/i]
- require 'rb-readline'
- else
- require 'readline'
- unless ::Readline.respond_to?(:line_buffer)
- RUBY_PLATFORM =~ /java/i ? load_jruby_extension : load_extension
- end
- end
+ # Loads the readline-like library and sets the completion_proc to the given agent.
+ def self.setup(agent)
+ readline_setup
- # Reinforcing irb defaults
- ::Readline.completion_append_character = nil
- if ::Readline.respond_to?("basic_word_break_characters=")
- ::Readline.basic_word_break_characters = DefaultBreakCharacters
- end
-
- ::Readline.completion_proc = agent
- if (::Readline::VERSION rescue nil).to_s[/editline/i]
- puts "Bond has detected EditLine and may not work with it. See the README's Limitations section."
- end
+ # Reinforcing irb defaults
+ Readline.completion_append_character = nil
+ if Readline.respond_to?("basic_word_break_characters=")
+ Readline.basic_word_break_characters = DefaultBreakCharacters
end
- def self.load_jruby_extension
- require 'jruby'
+ Readline.completion_proc = agent
+ end
- class << ::Readline
- ReadlineExt = org.jruby.ext.Readline
- def line_buffer
- ReadlineExt.s_get_line_buffer(JRuby.runtime.current_context, JRuby.reference(self))
- end
- end
+ def self.readline_setup
+ require 'readline'
+ load_extension unless Readline.respond_to?(:line_buffer)
+ if (Readline::VERSION rescue nil).to_s[/editline/i]
+ puts "Bond has detected EditLine and may not work with it." +
+ " See the README's Limitations section."
end
+ end
- def self.load_extension
- require 'readline_line_buffer'
- rescue LoadError
- $stderr.puts "Bond Error: Failed to load readline_line_buffer. Ensure that it exists and was built correctly."
- end
+ def self.load_extension
+ require 'readline_line_buffer'
+ rescue LoadError
+ $stderr.puts "Bond Error: Failed to load readline_line_buffer. Ensure that it exists and was built correctly."
+ end
- # Returns full line of what the user has typed.
- def self.line_buffer
- ::Readline.line_buffer
- end
+ # Returns full line of what the user has typed.
+ def self.line_buffer
+ Readline.line_buffer
end
end
View
13 lib/bond/readlines/jruby.rb
@@ -0,0 +1,13 @@
+# RUBY_PLATFORM[/java/i]
+class Bond::Jruby < Bond::Readline
+ def self.readline_setup
+ require 'readline'
+ require 'jruby'
+ class << Readline
+ ReadlineExt = org.jruby.ext.Readline
+ def line_buffer
+ ReadlineExt.s_get_line_buffer(JRuby.runtime.current_context, JRuby.reference(self))
+ end
+ end
+ end
+end
View
17 lib/bond/readlines/rawline.rb
@@ -0,0 +1,17 @@
+# A readline plugin for use with {Rawline}[http://github.com/h3rald/rawline].
+# This plugin should be used in conjunction with {a Rawline
+# shell}[http://www.h3rald.com/articles/real-world-rawline-usage].
+class Bond::Rawline < Bond::Readline
+ def self.setup(agent)
+ require 'rawline'
+ Rawline.completion_append_character = nil
+ Rawline.basic_word_break_characters= " \t\n\"\\'`><;|&{("
+ Rawline.completion_proc = agent
+ rescue LoadError
+ abort "Bond Error: rawline gem is required for this readline plugin -> gem install rawline"
+ end
+
+ def self.line_buffer
+ Rawline.editor.line.text
+ end
+end
View
9 lib/bond/readlines/ruby.rb
@@ -0,0 +1,9 @@
+# RUBY_PLATFORM[/mswin|mingw|bccwin|wince/i]
+class Bond::Ruby < Bond::Readline
+ def self.readline_setup
+ require 'rb-readline'
+ rescue LoadError
+ abort "Bond Error: rb-readline gem is required for this readline plugin" +
+ " -> gem install rb-readline"
+ end
+end
View
8 test/bond_test.rb
@@ -7,12 +7,10 @@ def start(options={}, &block)
end
before { M.instance_eval("@started = @agent = @config = nil"); M.expects(:load_completions) }
- it "prints error if readline_plugin is not a module" do
- capture_stderr { start :readline_plugin=>false }.should =~ /Invalid/
- end
-
it "prints error if readline_plugin doesn't have all required methods" do
- capture_stderr {start :readline_plugin=>Module.new{ def setup(arg); end } }.should =~ /Invalid/
+ capture_stderr {
+ start :readline_plugin=>Module.new{ def self.setup(arg); end }
+ }.should =~ /Invalid/
end
it "prints no error if valid readline_plugin" do
Please sign in to comment.
Something went wrong with that request. Please try again.