Skip to content

Commit

Permalink
Fix Reline::Unicode.calculate_width when input is not a TTY
Browse files Browse the repository at this point in the history
This fixes an error when output is redirected:

```
$ run_ruby -rreline -e '$stderr.puts Reline::Unicode.calculate_width("\u221a").inspect' </dev/null >/dev/null
/home/jeremy/tmp/ruby/lib/reline/ansi.rb:189:in `raw': Operation not supported by device (Errno::ENODEV)
```

The @@encoding -> defined?(@@encoding) changes is necessary because
without that part of the commit, the following error would be raised
by the above command:

```
/home/jeremy/tmp/reline/lib/reline/general_io.rb:10:in `encoding': uninitialized class variable @@encoding in Reline::GeneralIO (NameError)
```

Problem reported and initial patch for Windows provided by
Richard Sharman.

I tested this only on OpenBSD, but hopefully it works for other
operating systems.

Fixes [Bug #17493]
  • Loading branch information
jeremyevans committed May 13, 2021
1 parent e5ee7df commit c001971
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,25 @@ def self.line_editor
end
end

require 'reline/general_io'
if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
require 'reline/windows'
if Reline::Windows.msys_tty?
require 'reline/ansi'
Reline::IOGate = Reline::ANSI
Reline::IOGate = if ENV['TERM'] == 'dumb'
Reline::GeneralIO
else
require 'reline/ansi'
Reline::ANSI
end
else
Reline::IOGate = Reline::Windows
end
else
require 'reline/ansi'
Reline::IOGate = Reline::ANSI
Reline::IOGate = if $stdout.isatty
require 'reline/ansi'
Reline::ANSI
else
Reline::GeneralIO
end
end
Reline::HISTORY = Reline::History.new(Reline.core.config)
require 'reline/general_io'
2 changes: 1 addition & 1 deletion lib/reline/general_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.reset(encoding: nil)
end

def self.encoding
if @@encoding
if defined?(@@encoding)
@@encoding
elsif RUBY_PLATFORM =~ /mswin|mingw/
Encoding::UTF_8
Expand Down

0 comments on commit c001971

Please sign in to comment.