aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xgoforthanddie.rb73
1 files changed, 42 insertions, 31 deletions
diff --git a/goforthanddie.rb b/goforthanddie.rb
index 5e4af9e..cf24acd 100755
--- a/goforthanddie.rb
+++ b/goforthanddie.rb
@@ -1,15 +1,28 @@
#!/usr/bin/env ruby
+trap("INT") do
+ quit
+end
+
+def quit
+ hfile = File.join(ENV['HOME'], ".goforthanddie_history")
+ File.open(hfile, ?w) do |f|
+ f.puts(Readline::HISTORY.to_a)
+ f.flush
+ end
+ exit
+end
+
def getlist(stack, markermode, markerautoclear)
if markermode
h = []
wm = false
loop do
+ break if stack.empty?
t = stack.pop
wm = t == :mark
break if wm
h << t
- break if stack.empty?
end
stack.push(:mark) if wm && !markerautoclear
h.reverse
@@ -20,7 +33,6 @@ def getlist(stack, markermode, markerautoclear)
end
def handle(str, stack, markermode, markerautoclear, destructiveprint)
- exit if str.nil?
str.split.each do |token|
case token
when /\A[+-]?[0-9]+\Z/
@@ -95,46 +107,29 @@ def handle(str, stack, markermode, markerautoclear, destructiveprint)
h = getlist(stack, markermode, markerautoclear)
h.sort!
stack.push(*h.shift(k))
- when ">", "gt", "greaterthan"
- c = stack.pop
- h = stack.pop
- stack.push(c > h ? 1 : 0)
when ".>", ".gt", ".greaterthan"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
- stack.push(h.count{ |d| c > d })
- when ">=", "gteq", "greaterthanorequalto"
- c = stack.pop
- h = stack.pop
- stack.push(c >= h ? 1 : 0)
+ stack.push(*h.select{ |d| d > c })
when ".>=", ".gteq", ".greaterthanorequalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
- stack.push(h.count{ |d| c >= d })
- when "=", "eq", "equalto"
- c = stack.pop
- h = stack.pop
- stack.push(c == h ? 1 : 0)
+ stack.push(*h.select{ |d| d >= c })
when ".=", ".eq", ".equalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
- stack.push(h.count{ |d| c == d })
- when "<=", "lteq", "lessthanorequalto"
- c = stack.pop
- h = stack.pop
- stack.push(c <= h ? 1 : 0)
+ stack.push(*h.select{ |d| d == c })
when ".<=", ".lteq", ".lessthanorequalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
- stack.push(h.count{ |d| c <= d })
- when "<", "lt", "lessthan"
- c = stack.pop
- h = stack.pop
- stack.push(c < h ? 1 : 0)
+ stack.push(*h.select{ |d| d <= c })
when ".<", ".lt", ".lessthan"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
- stack.push(h.count{ |d| c < d })
+ stack.push(*h.select{ |d| d < c })
+ when ".count"
+ h = getlist(stack, markermode, markerautoclear)
+ stack.push(h.count)
when "!", "drop"
stack.pop
when ".!", ".drop"
@@ -174,7 +169,7 @@ def handle(str, stack, markermode, markerautoclear, destructiveprint)
puts " %#{namemax}s: #{defn}" % "'#{name}"
end
when "~q", "~quit"
- exit
+ quit
else
puts "I don't know how to #{token}!"
end
@@ -188,10 +183,26 @@ mac = false
dp = true
if $stdin.tty?
- loop do
- print "> "
- mm, mac, dp = handle(gets, stk, mm, mac, dp)
+ begin
+ require "readline"
+ rescue LoadError
+ Gem.install "readline"
+ Gem.install "readline-ext"
+ retry
end
+
+ hfile = File.join(ENV['HOME'], ".goforthanddie_history")
+ if File.exist?(hfile)
+ File.readlines(hfile).each do |line|
+ Readline::HISTORY << line.strip
+ end
+ end
+
+ while buf = Readline.readline("> ", true)
+ mm, mac, dp = handle(buf, stk, mm, mac, dp)
+ end
+
+ quit
else
mm, mac, dp = handle($stdin.read, stk, mm, mac, dp)
end