[macruby-changes] [4647] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:53:43 PDT 2010


Revision: 4647
          http://trac.macosforge.org/projects/ruby/changeset/4647
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:53:41 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Simplify formatter, no need for multiple classes. And add --noprompt.

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

Modified Paths:
--------------
    DietRB/trunk/bin/dietrb
    DietRB/trunk/lib/irb/formatter.rb
    DietRB/trunk/spec/formatter_spec.rb

Modified: DietRB/trunk/bin/dietrb
===================================================================
--- DietRB/trunk/bin/dietrb	2010-10-08 10:53:31 UTC (rev 4646)
+++ DietRB/trunk/bin/dietrb	2010-10-08 10:53:41 UTC (rev 4647)
@@ -11,7 +11,8 @@
     opt.on("-r load-lib",     "Loads the given library (same as `ruby -r')") { |lib| require lib }
     opt.on("-d",              "Set $DEBUG to true (same as `ruby -d')") { $DEBUG = true }
     opt.on("-I path",         "Add path to $LOAD_PATH") { |path| $LOAD_PATH.unshift(path) }
-    opt.on("--simple-prompt", "Simple prompt mode") { IRB.formatter = IRB::Formatter::SimplePrompt.new }
+    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 File.read(File.expand_path('../../VERSION', __FILE__))
       exit

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:53:31 UTC (rev 4646)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:53:41 UTC (rev 4647)
@@ -1,41 +1,40 @@
 module IRB
-  class << self
-    attr_writer :formatter
-    
-    def formatter
-      @formatter ||= Formatter::Default.new
-    end
+  def self.formatter
+    @formatter ||= Formatter.new
   end
   
-  module Formatter
-    class Default
-      PROMPT = "irb(%s):%03d:%d> "
-      
-      def prompt(context)
-        PROMPT % [context.object.inspect, context.line, context.source.level]
-      end
-      
-      def exception(exception)
-        "#{exception.class.name}: #{exception.message}\n\t#{exception.backtrace.join("\n\t")}"
-      end
-      
-      def result(object)
-        "=> #{object.inspect}"
-      end
-      
-      SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
-      
-      def syntax_error(line, message)
-        SYNTAX_ERROR % [line, message]
-      end
+  class Formatter
+    DEFAULT_PROMPT = "irb(%s):%03d:%d> "
+    SIMPLE_PROMPT  = ">> "
+    NO_PROMPT      = ""
+    
+    SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
+    
+    attr_writer :prompt
+    
+    def initialize
+      @prompt = :default
     end
     
-    class SimplePrompt < Default
-      PROMPT = ">> "
-      
-      def prompt(_)
-        PROMPT
+    def prompt(context)
+      case @prompt
+      when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.source.level]
+      when :simple  then SIMPLE_PROMPT
+      else
+        NO_PROMPT
       end
     end
+    
+    def exception(exception)
+      "#{exception.class.name}: #{exception.message}\n\t#{exception.backtrace.join("\n\t")}"
+    end
+    
+    def result(object)
+      "=> #{object.inspect}"
+    end
+    
+    def syntax_error(line, message)
+      SYNTAX_ERROR % [line, message]
+    end
   end
 end
\ No newline at end of file

Modified: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb	2010-10-08 10:53:31 UTC (rev 4646)
+++ DietRB/trunk/spec/formatter_spec.rb	2010-10-08 10:53:41 UTC (rev 4647)
@@ -2,9 +2,9 @@
 
 main = self
 
-describe "IRB::Formatter::Default" do
+describe "IRB::Formatter" do
   before do
-    @formatter = IRB::Formatter::Default.new
+    @formatter = IRB::Formatter.new
     @context = IRB::Context.new(main)
   end
   
@@ -21,6 +21,16 @@
     @formatter.prompt(IRB::Context.new(o)).should == "irb(#{o.inspect}):001:0> "
   end
   
+  it "returns a very simple prompt if specified" do
+    @formatter.prompt = :simple
+    @formatter.prompt(@context).should == ">> "
+  end
+  
+  it "returns no prompt if specified" do
+    @formatter.prompt = nil
+    @formatter.prompt(@context).should == ""
+  end
+  
   it "returns a formatted exception message" do
     begin; DoesNotExist; rescue NameError => e; exception = e; end
     @formatter.exception(exception).should ==
@@ -35,14 +45,4 @@
     @formatter.syntax_error(2, "syntax error, unexpected '}'").should ==
       "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
   end
-end
-
-describe "IRB::Formatter::SimplePrompt" do
-  before do
-    @formatter = IRB::Formatter::SimplePrompt.new
-  end
-  
-  it "returns a very simple prompt" do
-    @formatter.prompt(nil).should == ">> "
-  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/c9b94d92/attachment.html>


More information about the macruby-changes mailing list