1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/reline/general_io.rb
Jeremy Evans 242bad9a87 [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
2021-06-21 17:58:48 +09:00

101 lines
1.4 KiB
Ruby

require 'timeout'
class Reline::GeneralIO
def self.reset(encoding: nil)
@@pasting = false
@@encoding = encoding
end
def self.encoding
if defined?(@@encoding)
@@encoding
elsif RUBY_PLATFORM =~ /mswin|mingw/
Encoding::UTF_8
else
Encoding::default_external
end
end
def self.win?
false
end
def self.set_default_key_bindings(_)
end
@@buf = []
def self.input=(val)
@@input = val
end
def self.getc
unless @@buf.empty?
return @@buf.shift
end
c = nil
loop do
result = select([@@input], [], [], 0.1)
next if result.nil?
c = @@input.read(1)
break
end
c&.ord
end
def self.ungetc(c)
@@buf.unshift(c)
end
def self.get_screen_size
[1, 1]
end
def self.cursor_pos
Reline::CursorPos.new(1, 1)
end
def self.move_cursor_column(val)
end
def self.move_cursor_up(val)
end
def self.move_cursor_down(val)
end
def self.erase_after_cursor
end
def self.scroll_down(val)
end
def self.clear_screen
end
def self.set_screen_size(rows, columns)
end
def self.set_winch_handler(&handler)
end
@@pasting = false
def self.in_pasting?
@@pasting
end
def self.start_pasting
@@pasting = true
end
def self.finish_pasting
@@pasting = false
end
def self.prep
end
def self.deprep(otio)
end
end