ruby/lib/irb/ext/loader.rb
aycabta 46825a7462
Backport lib/reline, and lib/irb for 3.0.1 3rd (#4228)
* [ruby/irb] Suppress error when File::ALT_SEPARATOR is nil

96accf3b95

* [ruby/irb] Suppress colorize on Windows tests

5be9354cf9

* [ruby/irb] The command "irb_info" should show RUBY_PLATFORM

39d1cd874f

* [ruby/irb] Fix inverse separator condition

33f933196f

* [ruby/reline] Return 1 when char width not found

This fixes ruby/reline#261.

3cf1213014

* [ruby/reline] Avoid tripping over nil prompt

d4d9d3e3d4

* [ruby/reline] Move script files for yamatanooroti tests

03031b885d

* [ruby/reline] Add a test for suppressing crash when dynamic prompt returns empty

ref. https://github.com/ruby/reline/pull/262

b98bc3c329

* [ruby/reline] We still need support new and legacy behavior.
Revert "Support for change in Windows-specific behavior at eol"

This reverts commit cad4de6ee841b43f3f0e441626f9415c3eda0f82.

646587fc2c

* [ruby/reline] check ENABLE_VIRTUAL_TERMINAL_PROCESSING flag and switch eof processing

3535676689

* [ruby/reline] Use UTF-8 only for width calc, rest uses original encoding

I confirmed that libvterm supports only which are UTF-8, US ASCII, UK, and
DEC graphics by reading source code, so can't test this patch by
yamatanoorogi gem through vterm gem for now.

This fixes ruby/irb#190.

44596c0cc7

* [ruby/reline] fix Reline::Windows.getconsolemode buffer

use double quotes to properly convert the \000 escape sequence.

236dfe5683

* [ruby/irb] Version 1.3.4

ab9852ccc5

* [ruby/reline] Version 0.2.4

462f971bd3

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Eamonn Webster <eamonn.webster@gmail.com>
Co-authored-by: Yoshinao Muramatu <ysno@ac.auone-net.jp>
Co-authored-by: cremno <cremno@mail.ru>
2021-02-26 10:32:34 +09:00

155 lines
3.8 KiB
Ruby

# frozen_string_literal: false
#
# loader.rb -
# $Release Version: 0.9.6$
# $Revision$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
#
#
module IRB # :nodoc:
# Raised in the event of an exception in a file loaded from an Irb session
class LoadAbort < Exception;end
# Provides a few commands for loading files within an irb session.
#
# See ExtendCommandBundle for more information.
module IrbLoader
alias ruby_load load
alias ruby_require require
# Loads the given file similarly to Kernel#load
def irb_load(fn, priv = nil)
path = search_file_from_ruby_path(fn)
raise LoadError, "No such file to load -- #{fn}" unless path
load_file(path, priv)
end
if File.respond_to?(:absolute_path?)
def absolute_path?(path)
File.absolute_path?(path)
end
else
separator =
if File::ALT_SEPARATOR
"[#{Regexp.quote(File::SEPARATOR + File::ALT_SEPARATOR)}]"
else
File::SEPARATOR
end
ABSOLUTE_PATH_PATTERN = # :nodoc:
case Dir.pwd
when /\A\w:/, /\A#{separator}{2}/
/\A(?:\w:|#{separator})#{separator}/
else
/\A#{separator}/
end
def absolute_path?(path)
ABSOLUTE_PATH_PATTERN =~ path
end
end
def search_file_from_ruby_path(fn) # :nodoc:
if absolute_path?(fn)
return fn if File.exist?(fn)
return nil
end
for path in $:
if File.exist?(f = File.join(path, fn))
return f
end
end
return nil
end
# Loads a given file in the current session and displays the source lines
#
# See Irb#suspend_input_method for more information.
def source_file(path)
irb.suspend_name(path, File.basename(path)) do
FileInputMethod.open(path) do |io|
irb.suspend_input_method(io) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
else
begin
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
end
end
end
end
end
end
end
# Loads the given file in the current session's context and evaluates it.
#
# See Irb#suspend_input_method for more information.
def load_file(path, priv = nil)
irb.suspend_name(path, File.basename(path)) do
if priv
ws = WorkSpace.new(Module.new)
else
ws = WorkSpace.new
end
irb.suspend_workspace(ws) do
FileInputMethod.open(path) do |io|
irb.suspend_input_method(io) do
|back_io|
irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
irb.eval_input
else
begin
irb.eval_input
rescue LoadAbort
print "load abort!!\n"
end
end
end
end
end
end
end
end
def old # :nodoc:
back_io = @io
back_path = @irb_path
back_name = @irb_name
back_scanner = @irb.scanner
begin
@io = FileInputMethod.new(path)
@irb_name = File.basename(path)
@irb_path = path
@irb.signal_status(:IN_LOAD) do
if back_io.kind_of?(FileInputMethod)
@irb.eval_input
else
begin
@irb.eval_input
rescue LoadAbort
print "load abort!!\n"
end
end
end
ensure
@io = back_io
@irb_name = back_name
@irb_path = back_path
@irb.scanner = back_scanner
end
end
end
end