[macruby-changes] [4665] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:56:22 PDT 2010


Revision: 4665
          http://trac.macosforge.org/projects/ruby/changeset/4665
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:56:21 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Show an overview of N most recent history entries with Kernel#history(n) or the #h alias. Defaults to 50 entries.

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

Modified Paths:
--------------
    DietRB/trunk/lib/irb/ext/history.rb
    DietRB/trunk/spec/history_spec.rb

Modified: DietRB/trunk/lib/irb/ext/history.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 10:56:12 UTC (rev 4664)
+++ DietRB/trunk/lib/irb/ext/history.rb	2010-10-08 10:56:21 UTC (rev 4665)
@@ -1,7 +1,7 @@
 module IRB
   module History
     class << self
-      attr_accessor :file
+      attr_accessor :file, :max_entries_in_overview
       
       def new
         to_a.each do |source|
@@ -18,9 +18,40 @@
       def to_a
         File.read(@file).split("\n")
       end
+      
+      def history(number_of_entries = max_entries_in_overview)
+        history_size = Readline::HISTORY.size
+        start_index = 0
+        
+        if history_size <= number_of_entries
+          end_index = history_size - 1
+        else
+          end_index = history_size - 1
+          start_index = history_size - number_of_entries
+        end
+        
+        start_index.upto(end_index) { |i| print_line(i) }
+        nil
+      end
+      
+      private
+      
+      def print_line(line_number, show_line_numbers = true)
+        print "#{line_number}: " if show_line_numbers
+        puts Readline::HISTORY[line_number]
+      end
     end
   end
 end
 
+module Kernel
+  def history(number_of_entries = IRB::History.max_entries_in_overview)
+    IRB::History.history(number_of_entries)
+  end
+  
+  alias_method :h, :history
+end
+
 IRB::Context.processors << IRB::History
-IRB::History.file = File.expand_path("~/.irb_history")
\ No newline at end of file
+IRB::History.file = File.expand_path("~/.irb_history")
+IRB::History.max_entries_in_overview = 50
\ No newline at end of file

Modified: DietRB/trunk/spec/history_spec.rb
===================================================================
--- DietRB/trunk/spec/history_spec.rb	2010-10-08 10:56:12 UTC (rev 4664)
+++ DietRB/trunk/spec/history_spec.rb	2010-10-08 10:56:21 UTC (rev 4665)
@@ -44,4 +44,89 @@
     
     Readline::HISTORY.to_a.should == ["puts :ok", "foo(x)"]
   end
+end
+
+class << IRB::History
+  attr_reader :printed
+  
+  def reset
+    @printed = ""
+  end
+  
+  def print(s)
+    printed << s
+  end
+  
+  def puts(s)
+    printed << "#{s}\n"
+  end
+end
+
+describe "IRB::History, concerning the user api" do
+  it "shows by default a maximum of 50 history entries" do
+    IRB::History.max_entries_in_overview.should == 50
+  end
+  
+  before do
+    @sources = [
+      "puts :ok",
+      "foo(x)",
+      "class A",
+      "  def bar",
+      "    p :ok",
+      "  end",
+      "end",
+    ]
+    
+    Readline::HISTORY.clear
+    @sources.each { |source| Readline::HISTORY.push(source) }
+    
+    IRB::History.max_entries_in_overview = 5
+    
+    IRB::History.reset
+  end
+  
+  it "returns nil so that IRB doesn't cache some arbitrary line number" do
+    history.should == nil
+  end
+  
+  it "prints a formatted list with, by default IRB::History.max_entries_in_overview, number of history entries" do
+    history
+    
+    IRB::History.printed.should == %{
+2: class A
+3:   def bar
+4:     p :ok
+5:   end
+6: end
+}.sub(/\n/, '')
+  end
+  
+  it "prints a formatted list of N most recent history entries" do
+    history(7)
+    
+    IRB::History.printed.should == %{
+0: puts :ok
+1: foo(x)
+2: class A
+3:   def bar
+4:     p :ok
+5:   end
+6: end
+}.sub(/\n/, '')
+  end
+  
+  it "prints a formatted list of all history entries if the request number of entries is more than there is" do
+    history(777)
+
+    IRB::History.printed.should == %{
+0: puts :ok
+1: foo(x)
+2: class A
+3:   def bar
+4:     p :ok
+5:   end
+6: end
+}.sub(/\n/, '')
+  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/4833e9e0/attachment.html>


More information about the macruby-changes mailing list