Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/samples/expert-irb.rb
diff options
context:
space:
mode:
authorIshan Bansal <ishan@seeta.in>2011-02-04 12:53:17 (GMT)
committer Ishan Bansal <ishan@seeta.in>2011-02-04 12:53:17 (GMT)
commitee852209b70c803e6ab1bb69b71c7dafcb1448e7 (patch)
treeac20673816893b398aed4c4da463b157d9e4899e /samples/expert-irb.rb
Imported Upstream version 1HEADmaster
Diffstat (limited to 'samples/expert-irb.rb')
-rw-r--r--samples/expert-irb.rb112
1 files changed, 112 insertions, 0 deletions
diff --git a/samples/expert-irb.rb b/samples/expert-irb.rb
new file mode 100644
index 0000000..8960ef7
--- /dev/null
+++ b/samples/expert-irb.rb
@@ -0,0 +1,112 @@
+require 'irb/ruby-lex'
+require 'stringio'
+
+class MimickIRB < RubyLex
+ attr_accessor :started
+
+ class Continue < StandardError; end
+ class Empty < StandardError; end
+
+ def initialize
+ super
+ set_input(StringIO.new)
+ end
+
+ def run(str)
+ obj = nil
+ @io << str
+ @io.rewind
+ unless l = lex
+ raise Empty if @line == ''
+ else
+ case l.strip
+ when "reset"
+ @line = ""
+ when "time"
+ @line = "puts %{You started \#{IRBalike.started.since} ago.}"
+ else
+ @line << l << "\n"
+ if @ltype or @continue or @indent > 0
+ raise Continue
+ end
+ end
+ end
+ unless @line.empty?
+ obj = eval @line, TOPLEVEL_BINDING, "(irb)", @line_no
+ end
+ @line_no += @line.scan(/\n/).length
+ @line = ''
+ @exp_line_no = @line_no
+
+ @indent = 0
+ @indent_stack = []
+
+ $stdout.rewind
+ output = $stdout.read
+ $stdout.truncate(0)
+ $stdout.rewind
+ [output, obj]
+ rescue Object => e
+ case e when Empty, Continue
+ else @line = ""
+ end
+ raise e
+ ensure
+ set_input(StringIO.new)
+ end
+
+end
+
+CURSOR = ">>"
+IRBalike = MimickIRB.new
+$stdout = StringIO.new
+
+Shoes.app do
+ @str, @cmd = [CURSOR + " "], ""
+ stack :width => 1.0, :height => 1.0 do
+ background "#555"
+ stack :width => 1.0, :height => 50 do
+ para "Interactive Ruby ready.", :fill => white, :stroke => red
+ end
+ @scroll =
+ stack :width => 1.0, :height => -50, :scroll => true do
+ background "#555"
+ @console = para @str, :font => "Monospace 12px", :stroke => "#dfa"
+ @console.cursor = -1
+ end
+ end
+ keypress do |k|
+ case k
+ when "\n"
+ begin
+ out, obj = IRBalike.run(@cmd + ';')
+ @str += ["#@cmd\n",
+ span("#{out}=> #{obj.inspect}\n", :stroke => "#fda"),
+ "#{CURSOR} "]
+ @cmd = ""
+ rescue MimickIRB::Empty
+ rescue MimickIRB::Continue
+ @str += ["#@cmd\n.. "]
+ @cmd = ""
+ rescue Object => e
+ @str += ["#@cmd\n", span("#{e.class}: #{e.message}\n", :stroke => "#fcf"),
+ "#{CURSOR} "]
+ @cmd = ""
+ end
+ when String
+ @cmd += k
+ when :backspace
+ @cmd.slice!(-1)
+ when :tab
+ @cmd += " "
+ when :alt_q
+ quit
+ when :alt_c
+ self.clipboard = @cmd
+ when :alt_v
+ @cmd += self.clipboard
+ end
+ @console.replace *(@str + [@cmd])
+ @scroll.scroll_top = @scroll.scroll_max
+ end
+end