Skip to content

Commit

Permalink
Switch to StdioInputMethod when TERM is 'dumb'
Browse files Browse the repository at this point in the history
This works around Reline's misbehavior on low-capability terminals,
such as when IRB is launched inside Emacs (ruby/reline#616).

It should also resolve ruby#68 and resolve ruby#113 which were filed out of
similar need.
  • Loading branch information
dgutov committed Mar 22, 2024
1 parent 3c6d452 commit f04a184
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@
# ### Input Method
#
# The IRB input method determines how command input is to be read; by default,
# the input method for a session is IRB::RelineInputMethod.
# the input method for a session is IRB::RelineInputMethod. Unless the
# value of the TERM environment variable is 'dumb', in which case the
# most simplistic input method is used.
#
# You can set the input method by:
#
Expand All @@ -330,7 +332,8 @@
# IRB::ReadlineInputMethod.
# * `--nosingleline` or `--multiline` sets the input method to
# IRB::RelineInputMethod.
#
# * `--nosingleline` together with `--nomultiline` sets the
# input to IRB::StdioInputMethod.
#
#
# Method `conf.use_multiline?` and its synonym `conf.use_reline` return:
Expand Down
8 changes: 6 additions & 2 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def initialize(irb, workspace = nil, input_method = nil)
@io = nil
case use_multiline?
when nil
if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
if term_interactive? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
# Both of multiline mode and singleline mode aren't specified.
@io = RelineInputMethod.new(build_completor)
else
Expand All @@ -99,7 +99,7 @@ def initialize(irb, workspace = nil, input_method = nil)
unless @io
case use_singleline?
when nil
if (defined?(ReadlineInputMethod) && STDIN.tty? &&
if (defined?(ReadlineInputMethod) && term_interactive? &&
IRB.conf[:PROMPT_MODE] != :INF_RUBY)
@io = ReadlineInputMethod.new
else
Expand Down Expand Up @@ -151,6 +151,10 @@ def initialize(irb, workspace = nil, input_method = nil)
@command_aliases = @user_aliases.merge(KEYWORD_ALIASES)
end

private def term_interactive?
STDIN.tty? && ENV['TERM'] != 'dumb'
end

# because all input will eventually be evaluated as Ruby code,
# command names that conflict with Ruby keywords need special workaround
# we can remove them once we implemented a better command system for IRB
Expand Down

0 comments on commit f04a184

Please sign in to comment.