Skip to content

Instantly share code, notes, and snippets.

@no6v
Created November 2, 2008 11:54
Show Gist options
  • Save no6v/21691 to your computer and use it in GitHub Desktop.
Save no6v/21691 to your computer and use it in GitHub Desktop.
block-awareness history handling for irb
diff --git a/irb/context.rb b/irb/context.rb
index de70945..0c114ef 100644
--- a/irb/context.rb
+++ b/irb/context.rb
@@ -215,6 +215,7 @@ module IRB
end
def evaluate(line, line_no)
+ @io.savehist
@line_no = line_no
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
# @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
diff --git a/irb/ext/save-history.rb b/irb/ext/save-history.rb
index f3fb259..5c32aa0 100644
--- a/irb/ext/save-history.rb
+++ b/irb/ext/save-history.rb
@@ -58,7 +58,10 @@ module IRB
file = IRB.rc_file("_history") unless file
open(file, 'w' ) do |f|
hist = HISTORY.to_a
- f.puts(hist[-num..-1] || hist)
+ (hist[-num..-1] || hist).each do |h|
+ f.puts(h)
+ f.puts
+ end
end
end
end
@@ -75,7 +78,7 @@ module IRB
hist = IRB.rc_file("_history") unless hist
if File.exist?(hist)
open(hist) do |f|
- f.each {|l| HISTORY << l.chomp}
+ f.each("\n\n") {|l| HISTORY << l.chomp("")}
end
end
end
diff --git a/irb/input-method.rb b/irb/input-method.rb
index 2e50499..67cc840 100644
--- a/irb/input-method.rb
+++ b/irb/input-method.rb
@@ -35,6 +35,10 @@ module IRB
def readable_atfer_eof?
false
end
+
+ def savehist
+ false
+ end
end
class StdioInputMethod < InputMethod
@@ -91,11 +95,12 @@ module IRB
@line_no = 0
@line = []
@eof = false
+ @stack = []
end
def gets
if l = readline(@prompt, false)
- HISTORY.push(l) if !l.empty?
+ @stack.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
@@ -111,6 +116,11 @@ module IRB
true
end
+ def savehist
+ HISTORY.push(@stack * "\n")
+ @stack.clear
+ end
+
def line(line_no)
@line[line_no]
end
diff --git a/irb/context.rb b/irb/context.rb
index d96e8af..e00e1af 100644
--- a/irb/context.rb
+++ b/irb/context.rb
@@ -214,6 +214,7 @@ module IRB
end
def evaluate(line, line_no)
+ @io.savehist
@line_no = line_no
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
# @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
diff --git a/irb/ext/save-history.rb b/irb/ext/save-history.rb
index b985725..45573e0 100644
--- a/irb/ext/save-history.rb
+++ b/irb/ext/save-history.rb
@@ -58,7 +58,10 @@ module IRB
history_file = IRB.rc_file("_history") unless history_file
open(history_file, 'w' ) do |f|
hist = HISTORY.to_a
- f.puts(hist[-num..-1] || hist)
+ (hist[-num..-1] || hist).each do |h|
+ f.puts(h)
+ f.puts
+ end
end
end
end
@@ -77,7 +80,7 @@ module IRB
history_file = IRB.rc_file("_history") unless history_file
if File.exist?(history_file)
open(history_file) do |f|
- f.each {|l| HISTORY << l.chomp}
+ f.each("\n\n") {|l| HISTORY << l.chomp("")}
end
end
end
diff --git a/irb/input-method.rb b/irb/input-method.rb
index 2f4890d..057ec93 100644
--- a/irb/input-method.rb
+++ b/irb/input-method.rb
@@ -34,6 +34,10 @@ module IRB
def readable_atfer_eof?
false
end
+
+ def savehist
+ false
+ end
end
class StdioInputMethod < InputMethod
@@ -90,13 +94,14 @@ module IRB
@line_no = 0
@line = []
@eof = false
+ @stack = []
end
def gets
Readline.input = STDIN
Readline.output = STDOUT
if l = readline(@prompt, false)
- HISTORY.push(l) if !l.empty?
+ @stack.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
@@ -112,6 +117,11 @@ module IRB
true
end
+ def savehist
+ HISTORY.push(@stack * "\n")
+ @stack.clear
+ end
+
def line(line_no)
@line[line_no]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment