Skip to content

Commit

Permalink
Fix to skip terminal size detection from readline on non-TTY output
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Nov 27, 2023
1 parent 64ac303 commit e342f8f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* Fix ioctl call test to stub terminal size encoding for big-endian systems
* Fix to skip terminal size detection from readline on non-TTY output

## [v0.8.1] - 2020-07-17

Expand Down
3 changes: 2 additions & 1 deletion lib/tty/screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ def size_from_ioctl; nil end
#
# @api private
def size_from_readline(verbose: false)
require "readline" unless defined?(::Readline)
return unless output.tty?

require "readline" unless defined?(::Readline)
return unless ::Readline.respond_to?(:get_screen_size)

size = ::Readline.get_screen_size
Expand Down
54 changes: 54 additions & 0 deletions spec/unit/screen_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "delegate"
require "readline"
require "stringio"

RSpec.describe TTY::Screen do
Expand Down Expand Up @@ -122,6 +123,59 @@ def ioctl(_control, buf)
end
end

describe "#size_from_readline" do
it "doesn't detect size on non-tty output" do
allow(screen.output).to receive(:tty?).and_return(false)

expect(screen.size_from_readline).to eq(nil)
expect(screen.output).to have_received(:tty?)
end

it "doesn't detect size when the readline fails to load" do
undefine_const(:Readline) do
allow(screen.output).to receive(:tty?).and_return(true)
allow(screen).to receive(:require).with("readline").and_raise(LoadError)

expect(screen.size_from_readline).to eq(nil)
expect(screen).to have_received(:require).with("readline")
end
end

it "warns in verbose mode when the readline fails to load" do
undefine_const(:Readline) do
allow(screen.output).to receive(:tty?).and_return(true)
allow(screen).to receive(:require).with("readline").and_raise(LoadError)

expect {
screen.size_from_readline(verbose: true)
}.to output("no readline gem\n").to_stderr
end
end

it "doesn't detect size when the get_screen_size method is missing" do
allow(screen.output).to receive(:tty?).and_return(true)
allow(Readline).to receive(:respond_to?)
.with(:get_screen_size).and_return(false)

expect(screen.size_from_readline).to eq(nil)
expect(Readline).to have_received(:respond_to?).with(:get_screen_size)
end

it "detects no columns", if: Readline.respond_to?(:get_screen_size) do
allow(screen.output).to receive(:tty?).and_return(true)
allow(Readline).to receive(:get_screen_size).and_return([51, 0])

expect(screen.size_from_readline).to eq(nil)
end

it "detects size", if: Readline.respond_to?(:get_screen_size) do
allow(screen.output).to receive(:tty?).and_return(true)
allow(Readline).to receive(:get_screen_size).and_return([51, 211])

expect(screen.size_from_readline).to eq([51, 211])
end
end

describe "#size_from_tput" do
it "doesn't run command if outside of terminal" do
allow(screen.output).to receive(:tty?).and_return(false)
Expand Down

0 comments on commit e342f8f

Please sign in to comment.