[前][次][番号順一覧][スレッド一覧]

ruby-changes:66534

From: Jeremy <ko1@a...>
Date: Mon, 21 Jun 2021 18:44:45 +0900 (JST)
Subject: [ruby-changes:66534] 242bad9a87 (master): [ruby/reline] Fix Reline::Unicode.calculate_width when input is not a TTY

https://git.ruby-lang.org/ruby.git/commit/?id=242bad9a87

From 242bad9a87dbfc8a6af829ee3da3853925be14ab Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 13 May 2021 14:42:00 -0700
Subject: [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
---
 lib/reline.rb            | 18 +++++++++++++-----
 lib/reline/general_io.rb |  2 +-
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/reline.rb b/lib/reline.rb
index 6fc27ca..26cf911 100644
--- a/lib/reline.rb
+++ b/lib/reline.rb
@@ -453,17 +453,25 @@ module Reline https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L453
   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'
diff --git a/lib/reline/general_io.rb b/lib/reline/general_io.rb
index 4f60562..2f87d71 100644
--- a/lib/reline/general_io.rb
+++ b/lib/reline/general_io.rb
@@ -7,7 +7,7 @@ class Reline::GeneralIO https://github.com/ruby/ruby/blob/trunk/lib/reline/general_io.rb#L7
   end
 
   def self.encoding
-    if @@encoding
+    if defined?(@@encoding)
       @@encoding
     elsif RUBY_PLATFORM =~ /mswin|mingw/
       Encoding::UTF_8
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]