From ed41a39c21552dffa05d8125202e4a8a431bc2b8 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 15 Jul 2024 21:09:26 -0400 Subject: [PATCH] Simplify `Tty` methods. --- Library/Homebrew/utils/tty.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index 2dd129eadf2135..6cb770cef99d50 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -63,27 +63,22 @@ def show_cursor sig { returns(T.nilable([Integer, Integer])) } def size - `/bin/stty size 2>/dev/null`.presence&.split&.map(&:to_i) + @size ||= begin + height, width = `/bin/stty size 2>/dev/null`.presence&.split&.map(&:to_i) + return if height.nil? || width.nil? + + [height, width] + end end sig { returns(Integer) } def height - @height ||= begin - height, = size - height, = `/usr/bin/tput lines 2>/dev/null`.split if height.zero? - height ||= 40 - height.to_i - end + @height ||= size&.first || `/usr/bin/tput lines 2>/dev/null`.presence&.to_i || 40 end sig { returns(Integer) } def width - @width ||= begin - _, width = size - width, = `/usr/bin/tput cols 2>/dev/null`.split if width.zero? - width ||= 80 - width.to_i - end + @width ||= size&.second || `/usr/bin/tput cols 2>/dev/null`.presence&.to_i || 80 end sig { params(string: String).returns(String) }