-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress
executable file
·47 lines (41 loc) · 868 Bytes
/
progress
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
STDERR.sync = true
$bytes = true
$max = nil
$count = 0
until ARGV.empty?
case (arg = ARGV.shift)
when '-l'
$bytes = false
when '-b'
$bytes = true
when /\A(\d+)([kmg]?)\Z/
units = {'k'=>2**10, 'm'=>2**20, 'g'=>2**10, nil=>1}
$max = $1.to_i * units[$2]
else
raise "Unrecognized argument: `#{arg}'"
end
end
$max = STDIN.stat.size if $bytes and STDIN.stat.file? and $max.nil?
Thread.new{
last_count = nil
while true
if $count != last_count
if $max
STDERR.print "\r#{$count}/#{$max} [#{$count*100/$max}%]"
else
STDERR.print "\r#{$count}"
end
last_count = $count
end
sleep 1
end
}
begin
while data = ($bytes ? STDIN.read(2**12) : STDIN.gets)
STDOUT.print(data)
$count += $bytes ? data.length : 1
end
STDERR.print "\n"
rescue Errno::EPIPE
end