* transcode.c (trans_open_i): check the result of rb_transcoding_open.
[ruby-svn.git] / lib / irb / input-method.rb
blobfc8d476eae785ac4fbf3a32dbf141a8bf8e19279
2 #   irb/input-method.rb - input methods used irb
3 #       $Release Version: 0.9.5$
4 #       $Revision$
5 #       by Keiju ISHITSUKA(keiju@ruby-lang.org)
7 # --
9 #   
11 module IRB
12   # 
13   # InputMethod
14   #     StdioInputMethod
15   #     FileInputMethod
16   #     (ReadlineInputMethod)
17   #
18   STDIN_FILE_NAME = "(line)"
19   class InputMethod
20     @RCS_ID='-$Id$-'
22     def initialize(file = STDIN_FILE_NAME)
23       @file_name = file
24     end
25     attr_reader :file_name
27     attr_accessor :prompt
28     
29     def gets
30       IRB.fail NotImplementedError, "gets"
31     end
32     public :gets
34     def readable_atfer_eof?
35       false
36     end
37   end
38   
39   class StdioInputMethod < InputMethod
40     def initialize
41       super
42       @line_no = 0
43       @line = []
44     end
46     def gets
47       print @prompt
48       @line[@line_no += 1] = $stdin.gets
49     end
51     def eof?
52       $stdin.eof?
53     end
55     def readable_atfer_eof?
56       true
57     end
59     def line(line_no)
60       @line[line_no]
61     end
62   end
63   
64   class FileInputMethod < InputMethod
65     def initialize(file)
66       super
67       @io = open(file)
68     end
69     attr_reader :file_name
71     def eof?
72       @io.eof?
73     end
75     def gets
76       print @prompt
77       l = @io.gets
78 #      print @prompt, l
79       l
80     end
81   end
83   begin
84     require "readline"
85     class ReadlineInputMethod < InputMethod
86       include Readline 
87       def initialize
88         super
90         @line_no = 0
91         @line = []
92         @eof = false
93       end
95       def gets
96         Readline.input = STDIN
97         Readline.output = STDOUT
98         if l = readline(@prompt, false)
99           HISTORY.push(l) if !l.empty?
100           @line[@line_no += 1] = l + "\n"
101         else
102           @eof = true
103           l
104         end
105       end
107       def eof?
108         @eof
109       end
111       def readable_atfer_eof?
112         true
113       end
115       def line(line_no)
116         @line[line_no]
117       end
118     end
119   rescue LoadError
120   end