/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. #
  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 $-'
  22. def initialize(file = STDIN_FILE_NAME)
  23. @file_name = file
  24. end
  25. attr_reader :file_name
  26. attr_accessor :prompt
  27. def gets
  28. IRB.fail NotImplementedError, "gets"
  29. end
  30. public :gets
  31. def readable_atfer_eof?
  32. false
  33. end
  34. end
  35. class StdioInputMethod < InputMethod
  36. def initialize
  37. super
  38. @line_no = 0
  39. @line = []
  40. end
  41. def gets
  42. print @prompt
  43. @line[@line_no += 1] = $stdin.gets
  44. end
  45. def eof?
  46. $stdin.eof?
  47. end
  48. def readable_atfer_eof?
  49. true
  50. end
  51. def line(line_no)
  52. @line[line_no]
  53. end
  54. end
  55. class FileInputMethod < InputMethod
  56. def initialize(file)
  57. super
  58. @io = open(file)
  59. end
  60. attr_reader :file_name
  61. def eof?
  62. @io.eof?
  63. end
  64. def gets
  65. print @prompt
  66. l = @io.gets
  67. # print @prompt, l
  68. l
  69. end
  70. end
  71. begin
  72. require "readline"
  73. class ReadlineInputMethod < InputMethod
  74. include Readline
  75. def initialize
  76. super
  77. @line_no = 0
  78. @line = []
  79. @eof = false
  80. end
  81. def gets
  82. if l = readline(@prompt, false)
  83. HISTORY.push(l) if !l.empty?
  84. @line[@line_no += 1] = l + "\n"
  85. else
  86. @eof = true
  87. l
  88. end
  89. end
  90. def eof?
  91. @eof
  92. end
  93. def readable_atfer_eof?
  94. true
  95. end
  96. def line(line_no)
  97. @line[line_no]
  98. end
  99. end
  100. rescue LoadError
  101. end
  102. end