Skip to content

Commit

Permalink
Remove Whirly and show concurrent downloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jul 15, 2024
1 parent b17cc2a commit b1facf4
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 2,025 deletions.
1 change: 0 additions & 1 deletion Library/Homebrew/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,3 @@ gem "plist"
gem "ruby-macho"
gem "sorbet-runtime"
gem "warning"
gem "whirly"
116 changes: 100 additions & 16 deletions Library/Homebrew/cmd/fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,35 @@ def download_queue
end
end

class Spinner
FRAMES = [
"◜",
"◠",
"◝",
"◞",
"◡",
"◟",
].freeze

def initialize
@start = Time.now
@i = 0
end

sig { returns(String) }
def to_s
now = Time.now
if @start + 0.1 < now
@start = now
@i = (@i + 1) % FRAMES.count
end

FRAMES[@i]
end
end

sig { override.void }
def run
require "whirly"

Formulary.enable_factory_cache!

bucket = if args.deps?
Expand Down Expand Up @@ -194,24 +219,83 @@ def run
end
end

downloads.each do |downloadable, promise|
message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
if concurrency > 1
Whirly.start spinner: "arc", status: message
else
puts message
if concurrency == 1
downloads.each_value do |promise|
promise.wait!
rescue ChecksumMismatchError => e
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
end
else

spinner = Spinner.new

remaining_downloads = downloads.dup

previous_pending_line_count = 0

begin
print Tty.hide_cursor

output_message = lambda do |downloadable, promise|
status = case promise.state
when :fulfilled
"#{Tty.green}✔︎#{Tty.reset}"
when :rejected
"#{Tty.red}#{Tty.reset}"
when :pending
spinner
else
raise promise.state
end

promise.wait!
message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
puts "#{status} #{message}"

Whirly.configure stop: "#{Tty.green}✔︎#{Tty.reset}"
Whirly.stop if args.concurrency
rescue ChecksumMismatchError => e
Whirly.configure stop: "#{Tty.red}#{Tty.reset}"
Whirly.stop if args.concurrency
if promise.rejected? && (e = promise.reason).is_a?(ChecksumMismatchError)
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
next 2
end

1
end

until remaining_downloads.empty?
begin
finished_downloads = {}

finished_states = [:fulfilled, :rejected]
remaining_downloads.each do |downloadable, promise|
break unless finished_states.include?(promise.state)

finished_downloads[downloadable] = remaining_downloads.delete(downloadable)
end

finished_downloads.each do |downloadable, promise|
previous_pending_line_count -= 1
output_message.call(downloadable, promise)
end

previous_pending_line_count = 0
remaining_downloads.each do |downloadable, promise|
break if previous_pending_line_count >= (Tty.height - 1)

previous_pending_line_count += output_message.call(downloadable, promise)
end

opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
if previous_pending_line_count.positive?
$stdout.print "\033[#{previous_pending_line_count}A"
$stdout.flush
end
rescue Interrupt
print "\n" * previous_pending_line_count
raise
end
end
ensure
print Tty.show_cursor
end
end

download_queue.shutdown
Expand Down
29 changes: 27 additions & 2 deletions Library/Homebrew/utils/tty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,36 @@ def strip_ansi(string)
string.gsub(/\033\[\d+(;\d+)*m/, "")
end

sig { returns(String) }
def hide_cursor
"\033[?25l"
end

sig { returns(String) }
def show_cursor
"\033[?25h"
end

sig { returns(T.nilable([Integer, Integer])) }
def size
`/bin/stty size 2>/dev/null`.split&.map(&:to_i)
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
end

sig { returns(Integer) }
def width
@width ||= begin
_, width = `/bin/stty size 2>/dev/null`.split
width, = `/usr/bin/tput cols 2>/dev/null`.split if width.to_i.zero?
_, width = size
width, = `/usr/bin/tput cols 2>/dev/null`.split if width.zero?
width ||= 80
width.to_i
end
Expand Down

This file was deleted.

Loading

0 comments on commit b1facf4

Please sign in to comment.