[macruby-changes] [4720] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 04:04:32 PDT 2010


Revision: 4720
          http://trac.macosforge.org/projects/ruby/changeset/4720
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 04:04:31 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Move Readline specific code into irb/io/readline

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/TODO
    DietRB/trunk/bin/dietrb
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/lib/irb/ext/history.rb

Added Paths:
-----------
    DietRB/trunk/lib/irb/io/
    DietRB/trunk/lib/irb/io/readline.rb

Modified: DietRB/trunk/TODO
===================================================================
--- DietRB/trunk/TODO	2010-10-08 11:04:18 UTC (rev 4719)
+++ DietRB/trunk/TODO	2010-10-08 11:04:31 UTC (rev 4720)
@@ -1,3 +1,4 @@
+* Make puts/print use the proper IO when used from user code
 * Configurable history file? (:HISTORY_FILE) Configurable number of saved history lines? (:SAVE_HISTORY)
 * Make sure the following formatters work: hirb, awesome_print, and looksee
 * Make sure the majority of the utils in utility_belt work

Modified: DietRB/trunk/bin/dietrb
===================================================================
--- DietRB/trunk/bin/dietrb	2010-10-08 11:04:18 UTC (rev 4719)
+++ DietRB/trunk/bin/dietrb	2010-10-08 11:04:31 UTC (rev 4720)
@@ -18,7 +18,7 @@
     opt.on("--simple-prompt", "Simple prompt mode") { IRB.formatter.prompt = :simple }
     opt.on("--noprompt",      "No prompt mode") { IRB.formatter.prompt = nil }
     opt.on("-v", "--version", "Print the version of #{bin}") do
-      puts IRB::VERSION::DESCRIPTION
+      $stdout.puts IRB::VERSION::DESCRIPTION
       exit
     end
   end.parse!(ARGV)

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 11:04:18 UTC (rev 4719)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 11:04:31 UTC (rev 4720)
@@ -5,7 +5,7 @@
 # Copyright (C) 2009-2010, Eloy Duran <eloy.de.enige at gmail.com>
 
 require 'irb/formatter'
-require 'readline'
+require 'irb/io/readline'
 
 module IRB
   class Context
@@ -27,6 +27,7 @@
     end
     
     attr_reader :object, :binding, :line, :source, :processors
+    attr_accessor :io
     
     def initialize(object, explicit_binding = nil)
       @object  = object
@@ -34,6 +35,8 @@
       @line    = 1
       clear_buffer
       
+      @io = IRB::IO::Readline.new
+      
       @underscore_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
       @processors = self.class.processors.map { |processor| processor.new(self) }
     end
@@ -45,26 +48,31 @@
     def evaluate(source)
       result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
       store_result(result)
-      puts formatter.result(result)
+      @io.puts(formatter.result(result))
       result
     rescue Exception => e
       store_exception(e)
-      puts formatter.exception(e)
+      @io.puts(formatter.exception(e))
     end
     
-    # Reads input and passes it to all processors.
-    def readline
-      input = Readline.readline(formatter.prompt(self), true)
-      @processors.each { |processor| input = processor.input(input) }
-      input
-    rescue Interrupt
-      clear_buffer
-      ""
+    # Prints the prompt to, and reads input from, the + at io+ object and passes
+    # it to all processors.
+    #
+    # If the object returned by  is +nil+ the buffer is
+    # cleared. This could, for example, be when the user sends a SIGINT.
+    def readline_from_io
+      if input = @io.readline(formatter.prompt(self))
+        @processors.each { |processor| input = processor.input(input) }
+        input
+      else
+        clear_buffer
+        ""
+      end
     end
     
     def run
       self.class.make_current(self) do
-        while line = readline
+        while line = readline_from_io
           continue = process_line(line)
           break unless continue
         end
@@ -88,7 +96,7 @@
       return false if @source.terminate?
       
       if @source.syntax_error?
-        puts formatter.syntax_error(@line, @source.syntax_error)
+        @io.puts(formatter.syntax_error(@line, @source.syntax_error))
         @source.pop
       elsif @source.code_block?
         evaluate(@source)
@@ -100,7 +108,7 @@
     end
     
     def input_line(line)
-      puts formatter.prompt(self) + line
+      @io.puts(formatter.prompt(self) + line)
       process_line(line)
     end
     

Modified: DietRB/trunk/lib/irb/ext/history.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 11:04:18 UTC (rev 4719)
+++ DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 11:04:31 UTC (rev 4720)
@@ -54,7 +54,7 @@
       end
       
       start_index.upto(end_index) do |i|
-        puts "#{i}: #{Readline::HISTORY[i]}"
+        @context.io.puts "#{i}: #{Readline::HISTORY[i]}"
       end
     end
     

Added: DietRB/trunk/lib/irb/io/readline.rb
===================================================================
--- DietRB/trunk/lib/irb/io/readline.rb	                        (rev 0)
+++ DietRB/trunk/lib/irb/io/readline.rb	2010-10-08 11:04:31 UTC (rev 4720)
@@ -0,0 +1,21 @@
+require 'readline'
+
+module IRB
+  module IO
+    class Readline
+      def initialize(input = $stdin, output = $stdout)
+        ::Readline.input  = @input  = input
+        ::Readline.output = @output = output
+      end
+      
+      def readline(prompt)
+        ::Readline.readline(prompt, true)
+      rescue Interrupt
+      end
+      
+      def puts(*args)
+        @output.puts(*args)
+      end
+    end
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/36e6d22b/attachment-0001.html>


More information about the macruby-changes mailing list