1# 2# irb/input-method.rb - input methods used irb 3# $Release Version: 0.9.6$ 4# $Revision: 38544 $ 5# by Keiju ISHITSUKA(keiju@ruby-lang.org) 6# 7# -- 8# 9# 10# 11require 'irb/src_encoding' 12require 'irb/magic-file' 13 14module IRB 15 STDIN_FILE_NAME = "(line)" # :nodoc: 16 class InputMethod 17 @RCS_ID='-$Id: input-method.rb 38544 2012-12-21 17:29:18Z zzak $-' 18 19 # Creates a new input method object 20 def initialize(file = STDIN_FILE_NAME) 21 @file_name = file 22 end 23 # The file name of this input method, usually given during initialization. 24 attr_reader :file_name 25 26 # The irb prompt associated with this input method 27 attr_accessor :prompt 28 29 # Reads the next line from this input method. 30 # 31 # See IO#gets for more information. 32 def gets 33 IRB.fail NotImplementedError, "gets" 34 end 35 public :gets 36 37 # Whether this input method is still readable when there is no more data to 38 # read. 39 # 40 # See IO#eof for more information. 41 def readable_after_eof? 42 false 43 end 44 end 45 46 class StdioInputMethod < InputMethod 47 # Creates a new input method object 48 def initialize 49 super 50 @line_no = 0 51 @line = [] 52 @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") 53 @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") 54 end 55 56 # Reads the next line from this input method. 57 # 58 # See IO#gets for more information. 59 def gets 60 print @prompt 61 line = @stdin.gets 62 @line[@line_no += 1] = line 63 end 64 65 # Whether the end of this input method has been reached, returns +true+ if 66 # there is no more data to read. 67 # 68 # See IO#eof? for more information. 69 def eof? 70 @stdin.eof? 71 end 72 73 # Whether this input method is still readable when there is no more data to 74 # read. 75 # 76 # See IO#eof for more information. 77 def readable_after_eof? 78 true 79 end 80 81 # Returns the current line number for #io. 82 # 83 # #line counts the number of times #gets is called. 84 # 85 # See IO#lineno for more information. 86 def line(line_no) 87 @line[line_no] 88 end 89 90 # The external encoding for standard input. 91 def encoding 92 @stdin.external_encoding 93 end 94 end 95 96 # Use a File for IO with irb, see InputMethod 97 class FileInputMethod < InputMethod 98 # Creates a new input method object 99 def initialize(file) 100 super 101 @io = IRB::MagicFile.open(file) 102 end 103 # The file name of this input method, usually given during initialization. 104 attr_reader :file_name 105 106 # Whether the end of this input method has been reached, returns +true+ if 107 # there is no more data to read. 108 # 109 # See IO#eof? for more information. 110 def eof? 111 @io.eof? 112 end 113 114 # Reads the next line from this input method. 115 # 116 # See IO#gets for more information. 117 def gets 118 print @prompt 119 l = @io.gets 120# print @prompt, l 121 l 122 end 123 124 # The external encoding for standard input. 125 def encoding 126 @io.external_encoding 127 end 128 end 129 130 begin 131 require "readline" 132 class ReadlineInputMethod < InputMethod 133 include Readline 134 # Creates a new input method object using Readline 135 def initialize 136 super 137 138 @line_no = 0 139 @line = [] 140 @eof = false 141 142 @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") 143 @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") 144 end 145 146 # Reads the next line from this input method. 147 # 148 # See IO#gets for more information. 149 def gets 150 Readline.input = @stdin 151 Readline.output = @stdout 152 if l = readline(@prompt, false) 153 HISTORY.push(l) if !l.empty? 154 @line[@line_no += 1] = l + "\n" 155 else 156 @eof = true 157 l 158 end 159 end 160 161 # Whether the end of this input method has been reached, returns +true+ 162 # if there is no more data to read. 163 # 164 # See IO#eof? for more information. 165 def eof? 166 @eof 167 end 168 169 # Whether this input method is still readable when there is no more data to 170 # read. 171 # 172 # See IO#eof for more information. 173 def readable_after_eof? 174 true 175 end 176 177 # Returns the current line number for #io. 178 # 179 # #line counts the number of times #gets is called. 180 # 181 # See IO#lineno for more information. 182 def line(line_no) 183 @line[line_no] 184 end 185 186 # The external encoding for standard input. 187 def encoding 188 @stdin.external_encoding 189 end 190 end 191 rescue LoadError 192 end 193end 194