[macruby-changes] [4667] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Oct 8 03:56:39 PDT 2010
Revision: 4667
http://trac.macosforge.org/projects/ruby/changeset/4667
Author: eloy.de.enige at gmail.com
Date: 2010-10-08 03:56:38 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Execute entries from the history with Kernel#history!(entry_or_ranger), or its alias #h!
From: Eloy Duran <eloy.de.enige at gmail.com>
Modified Paths:
--------------
DietRB/trunk/lib/irb/context.rb
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:56:28 UTC (rev 4666)
+++ DietRB/trunk/lib/irb/context.rb 2010-10-08 10:56:38 UTC (rev 4667)
@@ -7,6 +7,8 @@
attr_accessor :current
def make_current(context)
+ # Messing with a current context is gonna bite me in the ass when we
+ # get to multi-threading, but we'll it when we get there.
before, @current = @current, context
yield
ensure
@@ -26,7 +28,7 @@
@line = 1
clear_buffer
- @processors = self.class.processors.map(&:new)
+ @processors = self.class.processors.map { |processor| processor.new(self) }
end
def __evaluate__(source, file = __FILE__, line = __LINE__)
@@ -88,8 +90,6 @@
true
end
- private
-
def formatter
IRB.formatter
end
Modified: DietRB/trunk/lib/irb/ext/history.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/history.rb 2010-10-08 10:56:28 UTC (rev 4666)
+++ DietRB/trunk/lib/irb/ext/history.rb 2010-10-08 10:56:38 UTC (rev 4667)
@@ -1,55 +1,84 @@
module IRB
- module History
+ class History
class << self
attr_accessor :file, :max_entries_in_overview
- def new
- to_a.each do |source|
- Readline::HISTORY.push(source)
- end if Readline::HISTORY.to_a.empty?
- self
+ def current
+ IRB::Context.current.processors.find do |processor|
+ processor.is_a?(IRB::History)
+ end
end
+ end
+
+ def initialize(context)
+ @context = context
- def input(source)
- File.open(@file, "a") { |f| f.puts(source) }
- source
- end
+ to_a.each do |source|
+ Readline::HISTORY.push(source)
+ end if Readline::HISTORY.to_a.empty?
+ end
+
+ def input(source)
+ File.open(self.class.file, "a") { |f| f.puts(source) }
+ source
+ end
+
+ def to_a
+ File.read(self.class.file).split("\n")
+ end
+
+ def history(number_of_entries = max_entries_in_overview)
+ history_size = Readline::HISTORY.size
+ start_index = 0
- def to_a
- File.read(@file).split("\n")
+ 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
- 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
+ start_index.upto(end_index) { |i| print_line(i) }
+ nil
+ end
+
+ def history!(entry_or_range)
+ # we don't want to execute history! again
+ @context.clear_buffer
+
+ lines = if entry_or_range.is_a?(Range)
+ entry_or_range.to_a.map { |i| Readline::HISTORY[i] }
+ else
+ [Readline::HISTORY[entry_or_range]]
end
- private
-
- def print_line(line_number, show_line_numbers = true)
- print "#{line_number}: " if show_line_numbers
- puts Readline::HISTORY[line_number]
+ lines.each do |line|
+ # TODO: this is also done by ./bin/dietrb when replaying a file
+ puts IRB.formatter.prompt(@context) + line
+ @context.process_line(line)
end
+ 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
module Kernel
def history(number_of_entries = IRB::History.max_entries_in_overview)
- IRB::History.history(number_of_entries)
+ IRB::History.current.history(number_of_entries)
end
+ alias_method :h, :history
- alias_method :h, :history
+ def history!(entry_or_range)
+ IRB::History.current.history!(entry_or_range)
+ end
+ alias_method :h!, :history!
end
IRB::Context.processors << IRB::History
Modified: DietRB/trunk/spec/history_spec.rb
===================================================================
--- DietRB/trunk/spec/history_spec.rb 2010-10-08 10:56:28 UTC (rev 4666)
+++ DietRB/trunk/spec/history_spec.rb 2010-10-08 10:56:38 UTC (rev 4667)
@@ -9,6 +9,7 @@
before do
@file = Tempfile.new("irb_history.txt")
IRB::History.file = @file.path
+ @history = IRB::History.new(nil)
end
after do
@@ -16,41 +17,39 @@
end
it "adds input to the history file" do
- IRB::History.input "puts :ok"
+ @history.input "puts :ok"
@file.rewind; @file.read.should == "puts :ok\n"
- IRB::History.input "foo(x)"
+ @history.input "foo(x)"
@file.rewind; @file.read.should == "puts :ok\nfoo(x)\n"
end
it "returns the same input value" do
- IRB::History.input("foo(x)").should == "foo(x)"
+ @history.input("foo(x)").should == "foo(x)"
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)"]
+ @history.input "puts :ok"
+ @history.to_a.should == ["puts :ok"]
+ @history.input "foo(x)"
+ @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)"
+ @history.input "puts :ok"
+ @history.input "foo(x)"
- IRB::History.new.should == IRB::History
- IRB::History.new.should == IRB::History
+ IRB::History.new(nil)
+ IRB::History.new(nil)
Readline::HISTORY.to_a.should == ["puts :ok", "foo(x)"]
end
end
-class << IRB::History
- attr_reader :printed
-
- def reset
- @printed = ""
+class IRB::History
+ def printed
+ @printed ||= ""
end
def print(s)
@@ -68,24 +67,31 @@
end
before do
- @sources = [
+ sources = [
"puts :ok",
- "foo(x)",
- "class A",
+ "x = foo(x)",
+ "class AAA",
" def bar",
- " p :ok",
+ " :ok",
" end",
"end",
]
Readline::HISTORY.clear
- @sources.each { |source| Readline::HISTORY.push(source) }
+ sources.each { |source| Readline::HISTORY.push(source) }
IRB::History.max_entries_in_overview = 5
- IRB::History.reset
+ @context = IRB::Context.new(Object.new)
+ IRB::Context.current = @context
+
+ @history = @context.processors.find { |p| p.is_a?(IRB::History) }
end
+ after do
+ IRB::Context.current = nil
+ end
+
it "returns nil so that IRB doesn't cache some arbitrary line number" do
history.should == nil
end
@@ -93,10 +99,10 @@
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
+ @history.printed.should == %{
+2: class AAA
3: def bar
-4: p :ok
+4: :ok
5: end
6: end
}.sub(/\n/, '')
@@ -105,12 +111,12 @@
it "prints a formatted list of N most recent history entries" do
history(7)
- IRB::History.printed.should == %{
+ @history.printed.should == %{
0: puts :ok
-1: foo(x)
-2: class A
+1: x = foo(x)
+2: class AAA
3: def bar
-4: p :ok
+4: :ok
5: end
6: end
}.sub(/\n/, '')
@@ -119,14 +125,25 @@
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 == %{
+ @history.printed.should == %{
0: puts :ok
-1: foo(x)
-2: class A
+1: x = foo(x)
+2: class AAA
3: def bar
-4: p :ok
+4: :ok
5: end
6: end
}.sub(/\n/, '')
end
+
+ it "evaluates the history entry specified" do
+ @context.__evaluate__("x = 2; def foo(x); x * 2; end")
+ history! 1
+ @context.__evaluate__("x").should == 4
+ end
+
+ it "evaluates the history entries specified by a range" do
+ history! 2..6
+ @context.__evaluate__("AAA.new.bar").should == :ok
+ 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/4af5055d/attachment-0001.html>
More information about the macruby-changes
mailing list