From 984cde114a204396c5dea3a44ce02f5a74e65611 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 14 Aug 2024 21:41:01 +0200 Subject: [PATCH] Move escape codes to `Tty`. --- Library/Homebrew/cmd/fetch.rb | 13 ++++++++----- Library/Homebrew/utils/tty.rb | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb index 359b3c5d2b..484f964742 100644 --- a/Library/Homebrew/cmd/fetch.rb +++ b/Library/Homebrew/cmd/fetch.rb @@ -275,7 +275,7 @@ module Homebrew finished_downloads.each do |downloadable, future| previous_pending_line_count -= 1 - print "\033[K" + $stdout.print Tty.clear_to_end $stdout.flush output_message.call(downloadable, future) end @@ -284,13 +284,13 @@ module Homebrew remaining_downloads.each do |downloadable, future| break if previous_pending_line_count >= [concurrency, (Tty.height - 1)].min - print "\033[K" + $stdout.print Tty.clear_to_end $stdout.flush previous_pending_line_count += output_message.call(downloadable, future) end if previous_pending_line_count.positive? - $stdout.print "\033[#{previous_pending_line_count}A" + $stdout.print Tty.move_cursor_up(previous_pending_line_count) $stdout.flush end @@ -300,8 +300,11 @@ module Homebrew # FIXME: Implement cancellation of running downloads. end - print "\n" * previous_pending_line_count - $stdout.flush + if previous_pending_line_count.positive? + $stdout.print Tty.move_cursor_down(previous_pending_line_count - 1) + $stdout.flush + end + raise end end diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index cf500c0fb3..9071c00ef6 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -51,6 +51,21 @@ module Tty string.gsub(/\033\[\d+(;\d+)*m/, "") end + sig { params(line_count: Integer).returns(String) } + def move_cursor_up(line_count) + "\033[#{line_count}A" + end + + sig { params(line_count: Integer).returns(String) } + def move_cursor_down(line_count) + "\033[#{line_count}B" + end + + sig { returns(String) } + def clear_to_end + "\033[K" + end + sig { returns(String) } def hide_cursor "\033[?25l"