1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
# frozen_string_literal: true
module Byebug
#
# Interface class for standard byebug use.
#
class LocalInterface < Interface
EOF_ALIAS = "continue"
def initialize
super()
@input = $stdin
@output = $stdout
@error = $stderr
end
#
# Reads a single line of input using Readline. If Ctrl-D is pressed, it
# returns "continue", meaning that program's execution will go on.
#
# @param prompt Prompt to be displayed.
#
def readline(prompt)
with_repl_like_sigint { without_readline_completion { Readline.readline(prompt) || EOF_ALIAS } }
end
#
# Yields the block handling Ctrl-C the following way: if pressed while
# waiting for input, the line is reset to only the prompt and we ask for
# input again.
#
# @note Any external 'INT' traps are overriden during this method.
#
def with_repl_like_sigint
orig_handler = trap("INT") { raise Interrupt }
yield
rescue Interrupt
puts("^C")
retry
ensure
trap("INT", orig_handler)
end
#
# Disable any Readline completion procs.
#
# Other gems, for example, IRB could've installed completion procs that are
# dependent on them being loaded. Disable those while byebug is the REPL
# making use of Readline.
#
def without_readline_completion
orig_completion = Readline.completion_proc
return yield unless orig_completion
begin
Readline.completion_proc = ->(_) { nil }
yield
ensure
Readline.completion_proc = orig_completion
end
end
end
end
|