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
|
|
|
#
|
2008-12-18 08:09:26 -05:00
|
|
|
require 'irb/src_encoding'
|
|
|
|
require 'irb/magic-file'
|
|
|
|
|
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
|
2003-03-23 12:58:57 -05:00
|
|
|
IRB.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
|
|
|
|
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
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
l = @io.gets
|
|
|
|
l
|
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
require "readline"
|
|
|
|
class ReadlineInputMethod < InputMethod
|
2009-03-05 22:56:38 -05:00
|
|
|
include Readline
|
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
|
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 => "-")
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
if l = readline(@prompt, false)
|
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|