@@ -43,26 +43,37 @@ def fetch
43
43
spinner = Spinner . new
44
44
remaining_downloads = downloads . dup . to_a
45
45
previous_pending_line_count = 0
46
+ tty = $stdout. tty?
46
47
47
48
begin
48
- $stdout. print Tty . hide_cursor
49
- $stdout. flush
49
+ stdout_print_and_flush_if_tty Tty . hide_cursor
50
50
51
51
output_message = lambda do |downloadable , future , last |
52
52
status = case future . state
53
53
when :fulfilled
54
- "#{ Tty . green } ✔︎#{ Tty . reset } "
54
+ if tty
55
+ "#{ Tty . green } ✔︎#{ Tty . reset } "
56
+ else
57
+ "✔︎"
58
+ end
55
59
when :rejected
56
- "#{ Tty . red } ✘#{ Tty . reset } "
60
+ if tty
61
+ "#{ Tty . red } ✘#{ Tty . reset } "
62
+ else
63
+ "✘"
64
+ end
57
65
when :pending , :processing
58
- "#{ Tty . blue } #{ spinner } #{ Tty . reset } "
66
+ "#{ Tty . blue } #{ spinner } #{ Tty . reset } " if tty
59
67
else
60
68
raise future . state . to_s
61
69
end
62
70
63
71
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
66
77
67
78
if future . rejected?
68
79
if ( e = future . reason ) . is_a? ( ChecksumMismatchError )
@@ -90,8 +101,7 @@ def fetch
90
101
91
102
finished_downloads . each do |downloadable , future |
92
103
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
95
105
output_message . call ( downloadable , future , false )
96
106
end
97
107
@@ -100,19 +110,17 @@ def fetch
100
110
remaining_downloads . each_with_index do |( downloadable , future ) , i |
101
111
break if previous_pending_line_count >= max_lines
102
112
103
- $stdout. print Tty . clear_to_end
104
- $stdout. flush
113
+ stdout_print_and_flush_if_tty Tty . clear_to_end
105
114
last = i == max_lines - 1 || i == remaining_downloads . count - 1
106
115
previous_pending_line_count += output_message . call ( downloadable , future , last )
107
116
end
108
117
109
118
if previous_pending_line_count . positive?
110
119
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
112
121
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 )
114
123
end
115
- $stdout. flush
116
124
end
117
125
118
126
sleep 0.05
@@ -124,22 +132,31 @@ def fetch
124
132
cancel
125
133
126
134
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 )
129
136
end
130
137
131
138
raise
132
139
end
133
140
end
134
141
ensure
135
- $stdout. print Tty . show_cursor
136
- $stdout. flush
142
+ stdout_print_and_flush_if_tty Tty . show_cursor
137
143
end
138
144
end
139
145
140
146
downloads . clear
141
147
end
142
148
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
+
143
160
sig { void }
144
161
def shutdown
145
162
pool . shutdown
0 commit comments