Class: Byebug::LocalInterface

Inherits:
Interface show all
Defined in:
lib/byebug/interfaces/local_interface.rb

Overview

Interface class for standard byebug use.

Constant Summary collapse

EOF_ALIAS =
"continue"

Instance Attribute Summary

Attributes inherited from Interface

#command_queue, #error, #history, #input, #output

Instance Method Summary collapse

Methods inherited from Interface

#autorestore, #autosave, #close, #confirm, #errmsg, #last_if_empty, #prepare_input, #print, #puts, #read_command, #read_file, #read_input

Methods included from Helpers::FileHelper

#get_line, #get_lines, #n_lines, #normalize, #shortpath, #virtual_file?

Constructor Details

#initializeLocalInterface

Returns a new instance of LocalInterface.



10
11
12
13
14
15
# File 'lib/byebug/interfaces/local_interface.rb', line 10

def initialize
  super()
  @input = $stdin
  @output = $stdout
  @error = $stderr
end

Instance Method Details

#readline(prompt) ⇒ Object

Reads a single line of input using Readline. If Ctrl-D is pressed, it returns “continue”, meaning that program’s execution will go on.

Parameters:

  • prompt

    Prompt to be displayed.



23
24
25
# File 'lib/byebug/interfaces/local_interface.rb', line 23

def readline(prompt)
  with_repl_like_sigint { without_readline_completion { Readline.readline(prompt) || EOF_ALIAS } }
end

#with_repl_like_sigintObject

Note:

Any external ‘INT’ traps are overriden during this method.

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.



34
35
36
37
38
39
40
41
42
# File 'lib/byebug/interfaces/local_interface.rb', line 34

def with_repl_like_sigint
  orig_handler = trap("INT") { raise Interrupt }
  yield
rescue Interrupt
  puts("^C")
  retry
ensure
  trap("INT", orig_handler)
end

#without_readline_completionObject

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.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/byebug/interfaces/local_interface.rb', line 51

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