[macruby-changes] [4661] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Oct 8 03:55:43 PDT 2010
Revision: 4661
http://trac.macosforge.org/projects/ruby/changeset/4661
Author: eloy.de.enige at gmail.com
Date: 2010-10-08 03:55:42 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Stash history work
From: Eloy Duran <eloy at Eloy-Durans-Mac-Pro.local>
Modified Paths:
--------------
DietRB/trunk/lib/irb/context.rb
DietRB/trunk/lib/irb.rb
Added Paths:
-----------
DietRB/trunk/lib/irb/ext/history.rb
DietRB/trunk/spec/history_spec.rb
Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb 2010-10-08 10:55:30 UTC (rev 4660)
+++ DietRB/trunk/lib/irb/context.rb 2010-10-08 10:55:42 UTC (rev 4661)
@@ -21,6 +21,8 @@
@binding = explicit_binding || object.instance_eval { binding }
@line = 1
clear_buffer
+
+ IRB::History.init
end
def __evaluate__(source, file = __FILE__, line = __LINE__)
@@ -36,7 +38,9 @@
end
def readline
- Readline.readline(formatter.prompt(self), true)
+ input = Readline.readline(formatter.prompt(self), true)
+ input = IRB::History.input(input)
+ input
rescue Interrupt
clear_buffer
""
Added: DietRB/trunk/lib/irb/ext/history.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/history.rb (rev 0)
+++ DietRB/trunk/lib/irb/ext/history.rb 2010-10-08 10:55:42 UTC (rev 4661)
@@ -0,0 +1,26 @@
+module IRB
+ module History
+ class << self
+ attr_accessor :file
+
+ def init
+ unless @initialized
+ @initialized = true
+ to_a.each do |source|
+ Readline::HISTORY.push(source)
+ end
+ end
+ end
+
+ def input(source)
+ File.open(@file, "a") { |f| f.puts(source) }
+ end
+
+ def to_a
+ File.read(@file).split("\n")
+ end
+ end
+ end
+end
+
+IRB::History.file = File.expand_path("~/.irb_history")
\ No newline at end of file
Modified: DietRB/trunk/lib/irb.rb
===================================================================
--- DietRB/trunk/lib/irb.rb 2010-10-08 10:55:30 UTC (rev 4660)
+++ DietRB/trunk/lib/irb.rb 2010-10-08 10:55:42 UTC (rev 4661)
@@ -1,6 +1,8 @@
require 'irb/context'
require 'irb/source'
+require 'irb/ext/history'
+
if !ENV['SPECCING'] && defined?(RUBY_ENGINE) && RUBY_ENGINE == "macruby"
require 'irb/ext/macruby'
end
Added: DietRB/trunk/spec/history_spec.rb
===================================================================
--- DietRB/trunk/spec/history_spec.rb (rev 0)
+++ DietRB/trunk/spec/history_spec.rb 2010-10-08 10:55:42 UTC (rev 4661)
@@ -0,0 +1,43 @@
+require File.expand_path('../spec_helper', __FILE__)
+require "tempfile"
+
+describe "IRB::History" do
+ it "stores the history by default in ~/.irb_history" do
+ IRB::History.file.should == File.expand_path("~/.irb_history")
+ end
+
+ before do
+ @file = Tempfile.new("irb_history.txt")
+ IRB::History.file = @file.path
+ end
+
+ after do
+ @file.close
+ end
+
+ it "adds input to the history file" do
+ IRB::History.input "puts :ok"
+ @file.rewind; @file.read.should == "puts :ok\n"
+ IRB::History.input "foo(x)"
+ @file.rewind; @file.read.should == "puts :ok\nfoo(x)\n"
+ end
+
+ it "returns the contents of the history file as an array of lines" do
+ IRB::History.input "puts :ok"
+ IRB::History.to_a.should == ["puts :ok"]
+ IRB::History.input "foo(x)"
+ IRB::History.to_a.should == ["puts :ok", "foo(x)"]
+ end
+
+ it "stores the contents of the history file in Readline::HISTORY once" do
+ Readline::HISTORY.clear
+
+ IRB::History.input "puts :ok"
+ IRB::History.input "foo(x)"
+
+ IRB::History.init
+ IRB::History.init
+
+ Readline::HISTORY.to_a.should == ["puts :ok", "foo(x)"]
+ 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/d1d95186/attachment-0001.html>
More information about the macruby-changes
mailing list