-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathslow_process.rb
More file actions
31 lines (23 loc) · 664 Bytes
/
Copy pathslow_process.rb
File metadata and controls
31 lines (23 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true
require_relative "../lib/tty-progressbar"
CONTENT_SIZE = 2048
CHUNK_SIZE = 255
# Dummy "long responding server"
def download_from_server(offset, limit)
sleep(0.1)
"<chunk #{offset}..#{offset + limit}>"
end
def download_finished?(position)
position >= CONTENT_SIZE
end
downloader = Enumerator.new do |y|
start = 0
loop do
y.yield(download_from_server(start, CHUNK_SIZE))
start += CHUNK_SIZE
raise StopIteration if download_finished?(start)
end
end
bar = TTY::ProgressBar.new("[:bar] :current_byte/:total_byte", total: CONTENT_SIZE)
response = bar.iterate(downloader, CHUNK_SIZE).to_a.join
puts response