PageRenderTime 34ms CodeModel.GetById 21ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/irb/input-method.rb

http://github.com/agross/netopenspace
Ruby | 120 lines | 84 code | 18 blank | 18 comment | 2 complexity | f76a384973f95c5c778f5c451b573129 MD5 | raw file
  1#
  2#   irb/input-method.rb - input methods used irb
  3#   	$Release Version: 0.9.5$
  4#   	$Revision: 11708 $
  5#   	$Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
  6#   	by Keiju ISHITSUKA(keiju@ruby-lang.org)
  7#
  8# --
  9#
 10#   
 11#
 12module 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 $-'
 22
 23    def initialize(file = STDIN_FILE_NAME)
 24      @file_name = file
 25    end
 26    attr_reader :file_name
 27
 28    attr_accessor :prompt
 29    
 30    def gets
 31      IRB.fail NotImplementedError, "gets"
 32    end
 33    public :gets
 34
 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
 46
 47    def gets
 48      print @prompt
 49      @line[@line_no += 1] = $stdin.gets
 50    end
 51
 52    def eof?
 53      $stdin.eof?
 54    end
 55
 56    def readable_atfer_eof?
 57      true
 58    end
 59
 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
 71
 72    def eof?
 73      @io.eof?
 74    end
 75
 76    def gets
 77      print @prompt
 78      l = @io.gets
 79#      print @prompt, l
 80      l
 81    end
 82  end
 83
 84  begin
 85    require "readline"
 86    class ReadlineInputMethod < InputMethod
 87      include Readline 
 88      def initialize
 89	super
 90
 91	@line_no = 0
 92	@line = []
 93	@eof = false
 94      end
 95
 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
105
106      def eof?
107	@eof
108      end
109
110      def readable_atfer_eof?
111	true
112      end
113
114      def line(line_no)
115	@line[line_no]
116      end
117    end
118  rescue LoadError
119  end
120end