Matthew Booth
2011-Jan-20 17:22 UTC
[Libguestfs] [PATCH] Don't display a progress bar if stderr isn't on a tty
Apart from being a good idea, this works round an apparent bug in either Term::ProgressBar or Term::ReadKey which seems to result in an attempt to write an exceptionally long progress bar. Fixes RHBZ#671083 --- lib/Sys/VirtV2V/Connection/Source.pm | 30 ++++++++++++++++++++++-------- 1 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/Sys/VirtV2V/Connection/Source.pm b/lib/Sys/VirtV2V/Connection/Source.pm index 4f94e20..5216503 100644 --- a/lib/Sys/VirtV2V/Connection/Source.pm +++ b/lib/Sys/VirtV2V/Connection/Source.pm @@ -126,9 +126,20 @@ sub _volume_copy # Copy the contents of the source stream to the destination stream my $total = 0; - my $progress = new Term::ProgressBar({name => $src->get_name(), - count => $expected, - ETA => 'linear' }); + + # Initialize a progress bar if STDERR is on a tty + my $progress; + if (-t STDERR) { + $progress = new Term::ProgressBar({name => $src->get_name(), + count => $expected, + ETA => 'linear' }); + } else { + print STDERR user_message(__x("Transferring storage volume {name}: ". + "{size} bytes", + name => $src->get_name(), + size => $expected)); + } + my $next_update = 0; for (;;) { my $buf = $src_s->read(4 * 1024 * 1024); @@ -137,12 +148,15 @@ sub _volume_copy $total += length($buf); $dst_s->write($buf); - $next_update = $progress->update($total) if ($total > $next_update); + $next_update = $progress->update($total) + if (defined($progress) && $total > $next_update); + } + if (defined($progress)) { + # Indicate that we finished regardless of how much data was written + $progress->update($expected); + # The progress bar doesn't print a newline on completion + print STDERR "\n"; } - # Indicate that we finished regardless of how much data was written - $progress->update($expected); - # The progress bar doesn't print a newline on completion - print STDERR "\n"; # This would be closed implicitly, but we want to report read/write errors # before checking for a short volume -- 1.7.3.5
Richard W.M. Jones
2011-Jan-20 17:58 UTC
[Libguestfs] [PATCH] Don't display a progress bar if stderr isn't on a tty
On Thu, Jan 20, 2011 at 05:22:28PM +0000, Matthew Booth wrote:> Apart from being a good idea, this works round an apparent bug in either > Term::ProgressBar or Term::ReadKey which seems to result in an attempt to write > an exceptionally long progress bar. > > Fixes RHBZ#671083ACK. I'm a bit surprised that the progress bar is sent to STDERR by default (the default in Term::ProgressBar). For reference, similar code is used in guestfish except we check if stdin is a tty not stderr, and we output to stdout: http://git.annexia.org/?p=libguestfs.git;a=blob;f=fish/fish.c;h=10f645f5ddea81954b361d2ece62c20f2fd8eea1;hb=HEAD#l490 http://git.annexia.org/?p=libguestfs.git;a=blob;f=fish/progress.c;hb=HEAD Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Reasonably Related Threads
- [PREVIEW ONLY] Refactor data transfer code
- [PATCH v2v] Add support for performing automated builds
- activerecord generating wrong syntax with postgresql
- Version two of progressbar for scp/sftp
- [PATCH] Add LocalCopy transfer method to transfer local files to a target