Skip to content

Instantly share code, notes, and snippets.

@idiomatic
Last active August 29, 2015 14:27
Show Gist options
  • Save idiomatic/7ff6ca3a21903e529aa6 to your computer and use it in GitHub Desktop.
Save idiomatic/7ff6ca3a21903e529aa6 to your computer and use it in GitHub Desktop.
rclone output polish
#!/bin/sh
# TODO: output checking
fix_rclone_output() {
perl -ne '
BEGIN {
$|++;
sub human {
my ($size) = @_;
if ($size > 1e12) { return sprintf("%.2fT", $size / 1e12) }
if ($size > 1e9) { return sprintf("%.2fG", $size / 1e9) }
if ($size > 1e6) { return sprintf("%.2fM", $size / 1e6) }
if ($size > 1e3) { return sprintf("%.2fK", $size / 1e3) }
return $size;
}
sub progress {
my ($label, $fraction) = @_;
my $index = int($fraction * (1 + length($label)));
return sprintf("\033[4m%.*s\033[0m%s", $index, $label, substr($label, $index));
}
$checkpoint = 0;
$SIG{INT} = sub { print "\n"; exit 1; };
$size = undef;
}
chomp;
if (/^(\d{4})\/(\d+)\/(\d+) ([0-9:]+) (.*)/) {
$when = "$1-$2-$3 $4";
$_ = $5;
}
if (/^$/ or /: Sending chunk/ or /: Unchanged skipping/ or /: Size and modification time the same/ or /^Go routines at exit 9/ or /: Modify window is/ or /: Buliding file list/ or /:Waiting for checks to finish/) {
next;
} elsif (/^Transferred: *(\d+) Bytes \( *([0-9\.]*) kByte\/s\)/) {
$transferred = $1;
$rate = $2 * 1024; # "kByte" is really "KiB"
} elsif (/^Errors: *(\d+)/) {
$errors = $1;
} elsif (/^Checks: *(\d+)/) {
$checks = $1;
} elsif (/^Transferred: *(\d+)$/) {
$transfers = $1;
} elsif (/^Checking: *(.+)$/) {
$checking = $1;
} elsif (/^Transferring: *(.+)$/) {
if ($transferring ne $1) {
if ($size > 0) {
$checkpoint += $size;
} elsif ($transferring) {
$checkpoint = $transferred;
}
$transferring = $1;
$size = (stat($transferring))[7];
}
} elsif (/^Elapsed time: *(?:(\d+)h)?(?:(\d+)m)?(\d+\.\d+)s/) {
$elapsed = $1 * 3600 + $2 * 60 + $3;
if ($transferring) {
if ($size > 0) {
$fraction = ($transferred - $checkpoint) / $size;
print "\r", progress(sprintf("%.44s %.2f%% of %s (%sB/s)", $transferring, $fraction * 100, human($size), human($rate)), $fraction), "\033[K";
} else {
printf "\r%.44s (%sB/s)\033[K", $transferring, human($rate);
}
} elsif ($checking) {
printf "\rchecking %.70s\033[K", $checking;
} else {
print "\r\033[K";
}
} elsif (/./) {
print "\r$_\033[K\n";
}
END {
if (defined $transferred) {
print "\n$transferred transferred ($rate kB/s)\033[K\n$errors errors\n$checks checks\n$transfers transfers\n${elapsed}s elapsed\n";
}
}
'
}
copy() {
srcdir="$1"
[ -f "$srcdir" ] && srcdir=$(dirname "$srcdir")
rclone copy -v --stats=1s --checkers=1 --transfers=1 "$@" 2>&1 | (cd "$srcdir"; fix_rclone_output)
}
copy '/Volumes/Drive/Uploaded/' 'target:Stuff/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment