[ruby/reline] Fix Reline::Unicode.calculate_width when input is not a TTY

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]

https://github.com/ruby/reline/commit/c001971bb3
This commit is contained in:
Jeremy Evans 2021-05-13 14:42:00 -07:00 коммит произвёл aycabta
Родитель 26f31f880c
Коммит 242bad9a87
2 изменённых файлов: 14 добавлений и 6 удалений

Просмотреть файл

@ -453,17 +453,25 @@ module Reline
end end
end end
require 'reline/general_io'
if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
require 'reline/windows' require 'reline/windows'
if Reline::Windows.msys_tty? if Reline::Windows.msys_tty?
require 'reline/ansi' Reline::IOGate = if ENV['TERM'] == 'dumb'
Reline::IOGate = Reline::ANSI Reline::GeneralIO
else
require 'reline/ansi'
Reline::ANSI
end
else else
Reline::IOGate = Reline::Windows Reline::IOGate = Reline::Windows
end end
else else
require 'reline/ansi' Reline::IOGate = if $stdout.isatty
Reline::IOGate = Reline::ANSI require 'reline/ansi'
Reline::ANSI
else
Reline::GeneralIO
end
end end
Reline::HISTORY = Reline::History.new(Reline.core.config) Reline::HISTORY = Reline::History.new(Reline.core.config)
require 'reline/general_io'

Просмотреть файл

@ -7,7 +7,7 @@ class Reline::GeneralIO
end end
def self.encoding def self.encoding
if @@encoding if defined?(@@encoding)
@@encoding @@encoding
elsif RUBY_PLATFORM =~ /mswin|mingw/ elsif RUBY_PLATFORM =~ /mswin|mingw/
Encoding::UTF_8 Encoding::UTF_8