Imported File#ftype spec from rubyspecs.
[rbx.git] / lib / irb / input-method.rb
blob982921c273c753c22509ed194f2848e25256b778
2 #   irb/input-method.rb - input methods used irb
3 #       $Release Version: 0.9.5$
4 #       $Revision: 11708 $
5 #       $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 #       by Keiju ISHITSUKA(keiju@ruby-lang.org)
8 # --
10 #   
12 module IRB
13   # 
14   # InputMethod
15   #     StdioInputMethod
16   #     FileInputMethod
17   #     (ReadlineInputMethod)
18   #
19   STDIN_FILE_NAME = "(line)"
20   class InputMethod
21     @RCS_ID='-$Id: input-method.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
23     def initialize(file = STDIN_FILE_NAME)
24       @file_name = file
25     end
26     attr_reader :file_name
28     attr_accessor :prompt
29     
30     def gets
31       IRB.fail NotImplementedError, "gets"
32     end
33     public :gets
35     def readable_atfer_eof?
36       false
37     end
38   end
39   
40   class StdioInputMethod < InputMethod
41     def initialize
42       super
43       @line_no = 0
44       @line = []
45     end
47     def gets
48       print @prompt
49       @line[@line_no += 1] = $stdin.gets
50     end
52     def eof?
53       $stdin.eof?
54     end
56     def readable_atfer_eof?
57       true
58     end
60     def line(line_no)
61       @line[line_no]
62     end
63   end
64   
65   class FileInputMethod < InputMethod
66     def initialize(file)
67       super
68       @io = open(file)
69     end
70     attr_reader :file_name
72     def eof?
73       @io.eof?
74     end
76     def gets
77       print @prompt
78       l = @io.gets
79 #      print @prompt, l
80       l
81     end
82   end
84   begin
85     require "readline"
86     class ReadlineInputMethod < InputMethod
87       include Readline 
88       def initialize
89         super
91         @line_no = 0
92         @line = []
93         @eof = false
94       end
96       def gets
97         if l = readline(@prompt, false)
98           HISTORY.push(l) if !l.empty?
99           @line[@line_no += 1] = l + "\n"
100         else
101           @eof = true
102           l
103         end
104       end
106       def eof?
107         @eof
108       end
110       def readable_atfer_eof?
111         true
112       end
114       def line(line_no)
115         @line[line_no]
116       end
117     end
118   rescue LoadError
119   end