Created
September 3, 2011 07:37
-
-
Save nanki/1190801 to your computer and use it in GitHub Desktop.
Super-interactive ruby console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: UTF-8 -*-; | |
require 'io/console' | |
require 'ansi' | |
class Textbox | |
def initialize(input=$stdin) | |
@input = input | |
end | |
def process(ch) | |
case ch | |
when "¥027" # <C-W> | |
@string = @string.rstrip.split(/¥b/)[0...-1].join | |
when "¥b" | |
@string = @string[0...-1] | |
when "¥r" | |
@string = '' | |
when "¥003", "¥004" | |
throw :exit, true | |
else | |
case ch | |
when /[[:print:]]/ | |
@string += ch | |
else | |
end | |
end | |
end | |
def main | |
@input.raw do |io| | |
yield @string = "" | |
io.chars do |ch| | |
prev = @string.dup | |
return if catch(:exit) { process(ch); nil} | |
next if @string == prev | |
yield @string | |
end | |
end | |
end | |
end | |
class SuperIrb | |
def display(str) | |
print ">> #{str}" | |
end | |
def nextline(l=nil) | |
@current_line = l if l | |
if @current_line >= $stdout.winsize.first | |
throw :exit | |
else | |
puts left($stdout.winsize.last) | |
end | |
@current_line += 1 | |
end | |
include ANSI::Code | |
def main | |
@input = Textbox.new($stdin) | |
@input.main do |str| | |
print cls | |
print reset | |
print move 0, 0 | |
display str | |
next if str.empty? | |
print save | |
nextline(1) | |
catch(:exit) do | |
begin | |
result = eval(str, TOPLEVEL_BINDING).inspect | |
rescue SyntaxError => e | |
result = e.inspect | |
rescue => e | |
result = e.inspect | |
end | |
result.each_line do |line| | |
print line.strip | |
nextline | |
end | |
end | |
print restore | |
end | |
end | |
end | |
SuperIrb.new.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment