download_queue: improve non-TTY output.

Print simpler output when not running in a TTY.
This commit is contained in:
Mike McQuaid 2025-07-25 16:00:45 +00:00 committed by GitHub
parent 00afc6b433
commit 332527f16f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -43,26 +43,37 @@ module Homebrew
spinner = Spinner.new spinner = Spinner.new
remaining_downloads = downloads.dup.to_a remaining_downloads = downloads.dup.to_a
previous_pending_line_count = 0 previous_pending_line_count = 0
tty = $stdout.tty?
begin begin
$stdout.print Tty.hide_cursor stdout_print_and_flush_if_tty Tty.hide_cursor
$stdout.flush
output_message = lambda do |downloadable, future, last| output_message = lambda do |downloadable, future, last|
status = case future.state status = case future.state
when :fulfilled when :fulfilled
"#{Tty.green}✔︎#{Tty.reset}" if tty
"#{Tty.green}✔︎#{Tty.reset}"
else
"✔︎"
end
when :rejected when :rejected
"#{Tty.red}#{Tty.reset}" if tty
"#{Tty.red}#{Tty.reset}"
else
""
end
when :pending, :processing when :pending, :processing
"#{Tty.blue}#{spinner}#{Tty.reset}" "#{Tty.blue}#{spinner}#{Tty.reset}" if tty
else else
raise future.state.to_s raise future.state.to_s
end end
message = "#{downloadable.download_type} #{downloadable.name}" message = "#{downloadable.download_type} #{downloadable.name}"
$stdout.print "#{status} #{message}#{"\n" unless last}" if tty
$stdout.flush stdout_print_and_flush "#{status} #{message}#{"\n" unless last}"
elsif status
puts "#{status} #{message}"
end
if future.rejected? if future.rejected?
if (e = future.reason).is_a?(ChecksumMismatchError) if (e = future.reason).is_a?(ChecksumMismatchError)
@ -90,8 +101,7 @@ module Homebrew
finished_downloads.each do |downloadable, future| finished_downloads.each do |downloadable, future|
previous_pending_line_count -= 1 previous_pending_line_count -= 1
$stdout.print Tty.clear_to_end stdout_print_and_flush_if_tty Tty.clear_to_end
$stdout.flush
output_message.call(downloadable, future, false) output_message.call(downloadable, future, false)
end end
@ -100,19 +110,17 @@ module Homebrew
remaining_downloads.each_with_index do |(downloadable, future), i| remaining_downloads.each_with_index do |(downloadable, future), i|
break if previous_pending_line_count >= max_lines break if previous_pending_line_count >= max_lines
$stdout.print Tty.clear_to_end stdout_print_and_flush_if_tty Tty.clear_to_end
$stdout.flush
last = i == max_lines - 1 || i == remaining_downloads.count - 1 last = i == max_lines - 1 || i == remaining_downloads.count - 1
previous_pending_line_count += output_message.call(downloadable, future, last) previous_pending_line_count += output_message.call(downloadable, future, last)
end end
if previous_pending_line_count.positive? if previous_pending_line_count.positive?
if (previous_pending_line_count - 1).zero? if (previous_pending_line_count - 1).zero?
$stdout.print Tty.move_cursor_beginning stdout_print_and_flush_if_tty Tty.move_cursor_beginning
else else
$stdout.print Tty.move_cursor_up_beginning(previous_pending_line_count - 1) stdout_print_and_flush_if_tty Tty.move_cursor_up_beginning(previous_pending_line_count - 1)
end end
$stdout.flush
end end
sleep 0.05 sleep 0.05
@ -124,22 +132,31 @@ module Homebrew
cancel cancel
if previous_pending_line_count.positive? if previous_pending_line_count.positive?
$stdout.print Tty.move_cursor_down(previous_pending_line_count - 1) stdout_print_and_flush_if_tty Tty.move_cursor_down(previous_pending_line_count - 1)
$stdout.flush
end end
raise raise
end end
end end
ensure ensure
$stdout.print Tty.show_cursor stdout_print_and_flush_if_tty Tty.show_cursor
$stdout.flush
end end
end end
downloads.clear downloads.clear
end end
sig { params(message: String).void }
def stdout_print_and_flush_if_tty(message)
stdout_print_and_flush(message) if $stdout.tty?
end
sig { params(message: String).void }
def stdout_print_and_flush(message)
$stdout.print(message)
$stdout.flush
end
sig { void } sig { void }
def shutdown def shutdown
pool.shutdown pool.shutdown