lib/irb/input-method.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   irb/input-method.rb - input methods using irb
   3  #       $Release Version: 0.9$
   4  #       $Revision: 1.3 $
   5  #       $Date: 2002/07/09 11:17:16 $
   6  #       by Keiju ISHITSUKA(keiju@ishitsuka.com)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  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,v 1.3 2002/07/09 11:17:16 keiju Exp $-'
  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 NotImplementError, "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, true)
  98            HISTORY.pop 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
 120  end