ftp -o - https://jcs.org/move_in | sh -
1#!/usr/bin/env ruby
2#
3# $ progress dump.sql.gz
4# gzip (68010): 56.13%, 27m52s left (1804580 bytes/sec)
5#
6
7DIFFS_COUNT = 20
8
9if !(file = ARGV[0])
10 puts "usage: #{$0} <file>"
11 exit 1
12end
13
14diffs = []
15
16last_off = -1
17loop do
18 fs = `fstat -o #{file}`.split("\n")
19 if fs.count == 1
20 if last_off > -1
21 exit
22 end
23 puts "nothing has #{file} open"
24 exit 1
25 end
26 fields = fs.last.split(" ")
27 cmd = fields[1]
28 pid = fields[2]
29 size, off = fields[8].split(":")
30
31 if off == "*"
32 puts "no access to read status of pid #{pid}"
33 exit 1
34 end
35 off = off.to_i
36
37 if last_off > -1
38 diffs.push (off - last_off)
39 if diffs.count == DIFFS_COUNT
40 diffs.shift
41 end
42 end
43 last_off = off
44
45 if diffs.any?
46 avg = diffs.sum / diffs.count.to_f
47 secs = ((size.to_i - off.to_i) / avg.to_f).floor
48 rem = ""
49 if secs > (60 * 60)
50 h = (secs / (60 * 60).to_f).floor
51 rem = "#{sprintf("%02d", h)}h"
52 secs -= (h * 60 * 60)
53 end
54 if secs > 60 || rem.length > 0
55 m = (secs / 60.0).floor
56 rem += "#{sprintf("%02d", m)}m"
57 secs -= (m * 60)
58 end
59 rem += "#{sprintf("%02d", secs)}s left"
60 else
61 avg = 0
62 rem = "-"
63 end
64
65 perc = sprintf("%0.2f%%", (off / size.to_f) * 100.0)
66 print "\r\e[K#{cmd} (#{pid}): #{perc}, #{rem} (#{avg.floor} bytes/sec)"
67
68 sleep 1
69rescue Interrupt
70 puts ""
71 exit
72end