Skip to content

Commit 4d58e74

Browse files
authored
Merge pull request #20313 from Homebrew/download_queue_no_tty
download_queue: improve non-TTY output.
2 parents bd3461d + 332527f commit 4d58e74

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

Library/Homebrew/download_queue.rb

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,37 @@ def fetch
4343
spinner = Spinner.new
4444
remaining_downloads = downloads.dup.to_a
4545
previous_pending_line_count = 0
46+
tty = $stdout.tty?
4647

4748
begin
48-
$stdout.print Tty.hide_cursor
49-
$stdout.flush
49+
stdout_print_and_flush_if_tty Tty.hide_cursor
5050

5151
output_message = lambda do |downloadable, future, last|
5252
status = case future.state
5353
when :fulfilled
54-
"#{Tty.green}✔︎#{Tty.reset}"
54+
if tty
55+
"#{Tty.green}✔︎#{Tty.reset}"
56+
else
57+
"✔︎"
58+
end
5559
when :rejected
56-
"#{Tty.red}#{Tty.reset}"
60+
if tty
61+
"#{Tty.red}#{Tty.reset}"
62+
else
63+
"✘"
64+
end
5765
when :pending, :processing
58-
"#{Tty.blue}#{spinner}#{Tty.reset}"
66+
"#{Tty.blue}#{spinner}#{Tty.reset}" if tty
5967
else
6068
raise future.state.to_s
6169
end
6270

6371
message = "#{downloadable.download_type} #{downloadable.name}"
64-
$stdout.print "#{status} #{message}#{"\n" unless last}"
65-
$stdout.flush
72+
if tty
73+
stdout_print_and_flush "#{status} #{message}#{"\n" unless last}"
74+
elsif status
75+
puts "#{status} #{message}"
76+
end
6677

6778
if future.rejected?
6879
if (e = future.reason).is_a?(ChecksumMismatchError)
@@ -90,8 +101,7 @@ def fetch
90101

91102
finished_downloads.each do |downloadable, future|
92103
previous_pending_line_count -= 1
93-
$stdout.print Tty.clear_to_end
94-
$stdout.flush
104+
stdout_print_and_flush_if_tty Tty.clear_to_end
95105
output_message.call(downloadable, future, false)
96106
end
97107

@@ -100,19 +110,17 @@ def fetch
100110
remaining_downloads.each_with_index do |(downloadable, future), i|
101111
break if previous_pending_line_count >= max_lines
102112

103-
$stdout.print Tty.clear_to_end
104-
$stdout.flush
113+
stdout_print_and_flush_if_tty Tty.clear_to_end
105114
last = i == max_lines - 1 || i == remaining_downloads.count - 1
106115
previous_pending_line_count += output_message.call(downloadable, future, last)
107116
end
108117

109118
if previous_pending_line_count.positive?
110119
if (previous_pending_line_count - 1).zero?
111-
$stdout.print Tty.move_cursor_beginning
120+
stdout_print_and_flush_if_tty Tty.move_cursor_beginning
112121
else
113-
$stdout.print Tty.move_cursor_up_beginning(previous_pending_line_count - 1)
122+
stdout_print_and_flush_if_tty Tty.move_cursor_up_beginning(previous_pending_line_count - 1)
114123
end
115-
$stdout.flush
116124
end
117125

118126
sleep 0.05
@@ -124,22 +132,31 @@ def fetch
124132
cancel
125133

126134
if previous_pending_line_count.positive?
127-
$stdout.print Tty.move_cursor_down(previous_pending_line_count - 1)
128-
$stdout.flush
135+
stdout_print_and_flush_if_tty Tty.move_cursor_down(previous_pending_line_count - 1)
129136
end
130137

131138
raise
132139
end
133140
end
134141
ensure
135-
$stdout.print Tty.show_cursor
136-
$stdout.flush
142+
stdout_print_and_flush_if_tty Tty.show_cursor
137143
end
138144
end
139145

140146
downloads.clear
141147
end
142148

149+
sig { params(message: String).void }
150+
def stdout_print_and_flush_if_tty(message)
151+
stdout_print_and_flush(message) if $stdout.tty?
152+
end
153+
154+
sig { params(message: String).void }
155+
def stdout_print_and_flush(message)
156+
$stdout.print(message)
157+
$stdout.flush
158+
end
159+
143160
sig { void }
144161
def shutdown
145162
pool.shutdown

0 commit comments

Comments
 (0)