# frozen_string_literal: false
# irb/input-method.rb - input methods used irb
# $Release Version: 0.9.6$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
require_relative 'src_encoding'
require_relative 'magic-file'
require_relative 'completion'
STDIN_FILE_NAME = "(line)" # :nodoc:
# Creates a new input method object
def initialize(file = STDIN_FILE_NAME)
# The file name of this input method, usually given during initialization.
# The irb prompt associated with this input method
# Reads the next line from this input method.
# See IO#gets for more information.
fail NotImplementedError, "gets"
if instance_variable_defined?(:@stdout)
# Whether this input method is still readable when there is no more data to
# See IO#eof for more information.
class StdioInputMethod < InputMethod
# Creates a new input method object
@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 => "-")
# Reads the next line from this input method.
# See IO#gets for more information.
@line[@line_no += 1] = line
# 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.
# Whether this input method is still readable when there is no more data to
# See IO#eof for more information.
# Returns the current line number for #io.
# #line counts the number of times #gets is called.
# See IO#lineno for more information.
# The external encoding for standard input.
# Use a File for IO with irb, see InputMethod
class FileInputMethod < InputMethod
# Creates a new input method object
@io = IRB::MagicFile.open(file)
# The file name of this input method, usually given during initialization.
# 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.
# Reads the next line from this input method.
# See IO#gets for more information.
# The external encoding for standard input.
class ReadlineInputMethod < InputMethod
def self.initialize_readline
# Creates a new input method object using Readline
self.class.initialize_readline
if Readline.respond_to?(:encoding_system_needs)
IRB.__send__(:set_encoding, Readline.encoding_system_needs.name, override: 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 Readline.respond_to?("basic_word_break_characters=")
Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
Readline.completion_append_character = nil
Readline.completion_proc = IRB::InputCompletor::CompletionProc
# Reads the next line from this input method.
# See IO#gets for more information.
Readline.output = @stdout
if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
# 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.
# Whether this input method is still readable when there is no more data to
# See IO#eof for more information.
# Returns the current line number for #io.
# #line counts the number of times #gets is called.
# See IO#lineno for more information.
# The external encoding for standard input.
readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
str = "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION}"
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
str += " and #{inputrc_path}" if File.exist?(inputrc_path)
class ReidlineInputMethod < InputMethod
# Creates a new input method object using Readline
IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: 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
Reline.completion_append_character = nil
Reline.completion_proc = IRB::InputCompletor::CompletionProc
Reline.output_modifier_proc =
if IRB.conf[:USE_COLORIZE]
proc do |output, complete: |
next unless IRB::Color.colorable?
IRB::Color.colorize_code(output, complete: complete)
Reline::Unicode.escape_for_print(output)
Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
def check_termination(&block)
@check_termination_proc = block
def dynamic_prompt(&block)
@auto_indent_proc = block
# Reads the next line from this input method.
# See IO#gets for more information.
Reline.prompt_proc = @prompt_proc
Reline.auto_indent_proc = @auto_indent_proc if @auto_indent_proc
if l = readmultiline(@prompt, false, &@check_termination_proc)
HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
# 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.
# Whether this input method is still readable when there is no more data to
# See IO#eof for more information.
# Returns the current line number for #io.
# #line counts the number of times #gets is called.
# See IO#lineno for more information.
# The external encoding for standard input.
config = Reline::Config.new
str = "ReidlineInputMethod with Reline #{Reline::VERSION}"
if config.respond_to?(:inputrc_path)
inputrc_path = File.expand_path(config.inputrc_path)
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
str += " and #{inputrc_path}" if File.exist?(inputrc_path)