2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2005-04-13 11:27:09 -04:00
|
|
|
# irb/input-method.rb - input methods used irb
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2018-11-02 13:52:33 -04:00
|
|
|
require_relative 'src_encoding'
|
2018-11-02 13:52:43 -04:00
|
|
|
require_relative 'magic-file'
|
2019-04-27 17:37:39 -04:00
|
|
|
require_relative 'completion'
|
|
|
|
require 'reline'
|
2008-12-18 08:09:26 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
module IRB
|
2012-12-21 00:45:50 -05:00
|
|
|
STDIN_FILE_NAME = "(line)" # :nodoc:
|
2000-05-12 05:07:57 -04:00
|
|
|
class InputMethod
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new input method object
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize(file = STDIN_FILE_NAME)
|
|
|
|
@file_name = file
|
|
|
|
end
|
2012-12-21 00:45:50 -05:00
|
|
|
# The file name of this input method, usually given during initialization.
|
2001-04-30 13:54:55 -04:00
|
|
|
attr_reader :file_name
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The irb prompt associated with this input method
|
2001-04-30 13:54:55 -04:00
|
|
|
attr_accessor :prompt
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Reads the next line from this input method.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def gets
|
2019-11-24 15:38:09 -05:00
|
|
|
fail NotImplementedError, "gets"
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
public :gets
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether this input method is still readable when there is no more data to
|
|
|
|
# read.
|
|
|
|
#
|
|
|
|
# See IO#eof for more information.
|
2012-12-21 12:29:18 -05:00
|
|
|
def readable_after_eof?
|
2000-05-12 05:07:57 -04:00
|
|
|
false
|
|
|
|
end
|
2020-04-27 05:27:19 -04:00
|
|
|
|
|
|
|
# For debug message
|
|
|
|
def inspect
|
|
|
|
'Abstract InputMethod'
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
class StdioInputMethod < InputMethod
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new input method object
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@line_no = 0
|
|
|
|
@line = []
|
2008-12-18 08:09:26 -05:00
|
|
|
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
2009-01-12 22:37:15 -05:00
|
|
|
@stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Reads the next line from this input method.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def gets
|
|
|
|
print @prompt
|
2008-12-18 08:09:26 -05:00
|
|
|
line = @stdin.gets
|
|
|
|
@line[@line_no += 1] = line
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether the end of this input method has been reached, returns +true+ if
|
|
|
|
# there is no more data to read.
|
|
|
|
#
|
|
|
|
# See IO#eof? for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def eof?
|
2008-12-18 08:09:26 -05:00
|
|
|
@stdin.eof?
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether this input method is still readable when there is no more data to
|
|
|
|
# read.
|
|
|
|
#
|
|
|
|
# See IO#eof for more information.
|
2012-12-21 12:29:18 -05:00
|
|
|
def readable_after_eof?
|
2000-05-12 05:07:57 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the current line number for #io.
|
|
|
|
#
|
|
|
|
# #line counts the number of times #gets is called.
|
|
|
|
#
|
|
|
|
# See IO#lineno for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def line(line_no)
|
|
|
|
@line[line_no]
|
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The external encoding for standard input.
|
2008-12-18 08:09:26 -05:00
|
|
|
def encoding
|
|
|
|
@stdin.external_encoding
|
|
|
|
end
|
2020-04-27 05:27:19 -04:00
|
|
|
|
|
|
|
# For debug message
|
|
|
|
def inspect
|
|
|
|
'StdioInputMethod'
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Use a File for IO with irb, see InputMethod
|
2000-05-12 05:07:57 -04:00
|
|
|
class FileInputMethod < InputMethod
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new input method object
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize(file)
|
|
|
|
super
|
2008-12-18 08:09:26 -05:00
|
|
|
@io = IRB::MagicFile.open(file)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2012-12-21 00:45:50 -05:00
|
|
|
# The file name of this input method, usually given during initialization.
|
2001-04-30 13:54:55 -04:00
|
|
|
attr_reader :file_name
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether the end of this input method has been reached, returns +true+ if
|
|
|
|
# there is no more data to read.
|
|
|
|
#
|
|
|
|
# See IO#eof? for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def eof?
|
|
|
|
@io.eof?
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Reads the next line from this input method.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def gets
|
2002-07-09 07:17:17 -04:00
|
|
|
print @prompt
|
2019-11-26 05:07:29 -05:00
|
|
|
@io.gets
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The external encoding for standard input.
|
2008-12-18 08:09:26 -05:00
|
|
|
def encoding
|
|
|
|
@io.external_encoding
|
|
|
|
end
|
2020-04-27 05:27:19 -04:00
|
|
|
|
|
|
|
# For debug message
|
|
|
|
def inspect
|
|
|
|
'FileInputMethod'
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
class ReadlineInputMethod < InputMethod
|
2020-08-11 22:21:36 -04:00
|
|
|
def self.initialize_readline
|
|
|
|
require "readline"
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
include ::Readline
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new input method object using Readline
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize
|
2020-08-11 22:21:36 -04:00
|
|
|
self.class.initialize_readline
|
2020-01-14 01:40:03 -05:00
|
|
|
if Readline.respond_to?(:encoding_system_needs)
|
|
|
|
IRB.__send__(:set_encoding, Readline.encoding_system_needs.name, override: false)
|
|
|
|
end
|
2014-08-08 21:36:49 -04:00
|
|
|
super
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
@line_no = 0
|
|
|
|
@line = []
|
|
|
|
@eof = false
|
2008-12-18 08:09:26 -05:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
|
|
|
@stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
2019-04-27 17:37:39 -04:00
|
|
|
|
|
|
|
if Readline.respond_to?("basic_word_break_characters=")
|
|
|
|
Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
|
|
|
|
end
|
|
|
|
Readline.completion_append_character = nil
|
|
|
|
Readline.completion_proc = IRB::InputCompletor::CompletionProc
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Reads the next line from this input method.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def gets
|
2008-12-18 08:09:26 -05:00
|
|
|
Readline.input = @stdin
|
|
|
|
Readline.output = @stdout
|
2019-04-21 05:13:49 -04:00
|
|
|
if l = readline(@prompt, false)
|
2014-08-08 21:36:49 -04:00
|
|
|
HISTORY.push(l) if !l.empty?
|
|
|
|
@line[@line_no += 1] = l + "\n"
|
|
|
|
else
|
|
|
|
@eof = true
|
|
|
|
l
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether the end of this input method has been reached, returns +true+
|
|
|
|
# if there is no more data to read.
|
|
|
|
#
|
|
|
|
# See IO#eof? for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def eof?
|
2014-08-08 21:36:49 -04:00
|
|
|
@eof
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Whether this input method is still readable when there is no more data to
|
|
|
|
# read.
|
|
|
|
#
|
|
|
|
# See IO#eof for more information.
|
2012-12-21 12:29:18 -05:00
|
|
|
def readable_after_eof?
|
2014-08-08 21:36:49 -04:00
|
|
|
true
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the current line number for #io.
|
|
|
|
#
|
|
|
|
# #line counts the number of times #gets is called.
|
|
|
|
#
|
|
|
|
# See IO#lineno for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def line(line_no)
|
2014-08-08 21:36:49 -04:00
|
|
|
@line[line_no]
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The external encoding for standard input.
|
2008-12-18 08:09:26 -05:00
|
|
|
def encoding
|
2014-08-08 21:36:49 -04:00
|
|
|
@stdin.external_encoding
|
2008-12-18 08:09:26 -05:00
|
|
|
end
|
2019-04-27 17:37:39 -04:00
|
|
|
|
2020-04-27 05:27:19 -04:00
|
|
|
# For debug message
|
|
|
|
def inspect
|
|
|
|
readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
|
2020-04-28 04:06:43 -04:00
|
|
|
str = "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION}"
|
|
|
|
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
|
|
|
|
str += " and #{inputrc_path}" if File.exist?(inputrc_path)
|
|
|
|
str
|
2020-04-27 05:27:19 -04:00
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-27 17:37:39 -04:00
|
|
|
|
|
|
|
class ReidlineInputMethod < InputMethod
|
|
|
|
include Reline
|
|
|
|
# Creates a new input method object using Readline
|
|
|
|
def initialize
|
2020-01-14 01:40:03 -05:00
|
|
|
IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false)
|
2019-04-27 17:37:39 -04:00
|
|
|
super
|
|
|
|
|
|
|
|
@line_no = 0
|
|
|
|
@line = []
|
|
|
|
@eof = false
|
|
|
|
|
|
|
|
@stdin = ::IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
|
|
|
@stdout = ::IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
|
|
|
|
|
|
|
|
if Reline.respond_to?("basic_word_break_characters=")
|
|
|
|
Reline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
|
|
|
|
end
|
|
|
|
Reline.completion_append_character = nil
|
|
|
|
Reline.completion_proc = IRB::InputCompletor::CompletionProc
|
2019-05-25 09:52:10 -04:00
|
|
|
Reline.output_modifier_proc =
|
|
|
|
if IRB.conf[:USE_COLORIZE]
|
2020-08-11 22:21:36 -04:00
|
|
|
proc do |output, complete: |
|
2019-05-25 09:52:10 -04:00
|
|
|
next unless IRB::Color.colorable?
|
2019-05-30 17:03:18 -04:00
|
|
|
IRB::Color.colorize_code(output, complete: complete)
|
2019-05-25 09:52:10 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
proc do |output|
|
|
|
|
Reline::Unicode.escape_for_print(output)
|
|
|
|
end
|
2019-05-25 09:31:13 -04:00
|
|
|
end
|
2019-04-27 18:47:33 -04:00
|
|
|
Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
|
2019-04-27 17:37:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_termination(&block)
|
|
|
|
@check_termination_proc = block
|
|
|
|
end
|
|
|
|
|
2019-06-13 19:26:06 -04:00
|
|
|
def dynamic_prompt(&block)
|
|
|
|
@prompt_proc = block
|
|
|
|
end
|
|
|
|
|
2019-06-18 07:57:58 -04:00
|
|
|
def auto_indent(&block)
|
|
|
|
@auto_indent_proc = block
|
|
|
|
end
|
|
|
|
|
2019-04-27 17:37:39 -04:00
|
|
|
# Reads the next line from this input method.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
|
|
|
def gets
|
|
|
|
Reline.input = @stdin
|
|
|
|
Reline.output = @stdout
|
2019-06-13 19:26:06 -04:00
|
|
|
Reline.prompt_proc = @prompt_proc
|
2019-06-18 20:19:41 -04:00
|
|
|
Reline.auto_indent_proc = @auto_indent_proc if @auto_indent_proc
|
2019-04-27 17:37:39 -04:00
|
|
|
if l = readmultiline(@prompt, false, &@check_termination_proc)
|
|
|
|
HISTORY.push(l) if !l.empty?
|
|
|
|
@line[@line_no += 1] = l + "\n"
|
|
|
|
else
|
|
|
|
@eof = true
|
|
|
|
l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Whether the end of this input method has been reached, returns +true+
|
|
|
|
# if there is no more data to read.
|
|
|
|
#
|
|
|
|
# See IO#eof? for more information.
|
|
|
|
def eof?
|
2019-11-29 21:14:35 -05:00
|
|
|
@eof
|
2019-04-27 17:37:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Whether this input method is still readable when there is no more data to
|
|
|
|
# read.
|
|
|
|
#
|
|
|
|
# See IO#eof for more information.
|
|
|
|
def readable_after_eof?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the current line number for #io.
|
|
|
|
#
|
|
|
|
# #line counts the number of times #gets is called.
|
|
|
|
#
|
|
|
|
# See IO#lineno for more information.
|
|
|
|
def line(line_no)
|
|
|
|
@line[line_no]
|
|
|
|
end
|
|
|
|
|
|
|
|
# The external encoding for standard input.
|
|
|
|
def encoding
|
|
|
|
@stdin.external_encoding
|
|
|
|
end
|
2020-04-27 05:27:19 -04:00
|
|
|
|
|
|
|
# For debug message
|
|
|
|
def inspect
|
|
|
|
config = Reline::Config.new
|
2020-04-28 04:06:43 -04:00
|
|
|
str = "ReidlineInputMethod with Reline #{Reline::VERSION}"
|
2020-04-27 05:27:19 -04:00
|
|
|
if config.respond_to?(:inputrc_path)
|
2020-08-13 06:19:00 -04:00
|
|
|
inputrc_path = File.expand_path(config.inputrc_path)
|
2020-04-27 05:27:19 -04:00
|
|
|
else
|
|
|
|
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
|
|
|
|
end
|
2020-04-28 04:06:43 -04:00
|
|
|
str += " and #{inputrc_path}" if File.exist?(inputrc_path)
|
|
|
|
str
|
2020-04-27 05:27:19 -04:00
|
|
|
end
|
2019-12-07 06:21:08 -05:00
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|