io/input.rb in trepanning-0.1.0 vs io/input.rb in trepanning-0.1.1
- old
+ new
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
-# Copyright (C) 2010 Rocky Bernstein <rockyb@rubyforge.net>
+# Copyright (C) 2010, 2011 Rocky Bernstein <rockyb@rubyforge.net>
# Debugger user/command-oriented input possibly attached to IO-style
# input or GNU Readline.
#
@@ -11,36 +11,44 @@
# Debugger user/command-oriented input possibly attached to IO-style
# input or GNU Readline.
class UserInput < Trepan::InputBase
+ @@readline_finalized = false
+
def initialize(inp, opts={})
- @opts = DEFAULT_OPTS.merge(opts)
- @input = inp || STDIN
- @eof = false
+ @opts = DEFAULT_OPTS.merge(opts)
+ @input = inp || STDIN
+ @eof = false
+ @line_edit = @opts[:line_edit]
+ @use_readline = @opts[:readline]
end
def closed?; @input.closed? end
def eof?; @eof end
def interactive?
@input.respond_to?(:isatty) && @input.isatty
end
-
# Read a line of input. EOFError will be raised on EOF.
- #
- # Note that we don't support prompting first. Instead, arrange
- # to call Trepan::Output.write() first with the prompt.
- def readline
- # FIXME we don't do command completion.
+ def readline(prompt='')
raise EOFError if eof?
begin
- line = @opts[:line_edit] ? Readline.readline : @input.gets
- @eof = !line
- rescue
+ if @line_edit && @use_readline
+ line = Readline.readline(prompt, true)
+ else
+ line = @input.gets
+ end
+ rescue Interrupt
+ return ''
+ rescue EOFError
+ rescue => e
+ puts $!.backtrace
+ puts "Exception caught #{e.inspect}"
@eof = true
end
+ @eof = !line
raise EOFError if eof?
return line
end
class << self
@@ -50,20 +58,36 @@
# GNU-like readline library. By default, we will assume to try
# using readline.
def open(inp=nil, opts={})
inp ||= STDIN
inp = File.new(inp, 'r') if inp.is_a?(String)
- opts[:line_edit] = false unless
+ opts[:line_edit] = @line_edit =
inp.respond_to?(:isatty) && inp.isatty && Trepan::GNU_readline?
self.new(inp, opts)
end
+
+ def finalize
+ if defined?(RbReadline) && !@@readline_finalized
+ begin
+ RbReadline.rl_cleanup_after_signal()
+ rescue
+ end
+ begin
+ RbReadline.rl_deprep_terminal()
+ rescue
+ end
+ @@readline_finalized = true
+ end
+ end
end
end
end
def Trepan::GNU_readline?
begin
- require 'readline'
+ return @have_readline unless @have_readline.nil?
+ @have_readline = require 'readline'
+ at_exit { Trepan::UserInput::finalize }
return true
rescue LoadError
return false
end
end