common.sh: limit pv progress bar initial width to 80 characters

By default, pv automatically infers and uses the full width of the
terminal. This generally makes sense for a console application, but in
the case of pv it just causes the progress bar to be ridiculously wide
when run on wide terminals, to the point it's hard to read. This CL is
setting it to 80 characters, the widely accepted standard width for
a terminal, in cases where the terminal appears to be larger than
80 columns. Note that:

* Even with -w, pv appears to be resizing the progress bar as the
  terminal width changes midway through the run. This means that if
  a user widens the window, then the progress bar will go wide again and
  there's nothing to be done about it.

* Theoretically, in very rare cases this may lead to a progress bar the
  exceeds the width of the terminal (i.e. set to 80 columns on
  a terminal that has just shrunk to fewer columns). The odds for such
  timing are close to nil and even then the damage is minimal.

* This will work for non-terminal runs, or otherwise runs where stty
  does not produce any output.

* To avoid the initialization overhead for all common.sh inclusion,
  replacing the variable with a function that prints the pv/cat command.

BUG=None
TEST=Ran ./image_to_usb on wide and narrow terminal windows, it works.

Change-Id: I549df1dd29e93909ea646ae9b9e09d9a588ad382
Reviewed-on: https://gerrit.chromium.org/gerrit/40937
Commit-Queue: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
This commit is contained in:
Gilad Arnold 2013-01-09 09:19:51 -08:00 committed by ChromeBot
parent 435611e5ee
commit 8f861c05b6
3 changed files with 19 additions and 10 deletions

View File

@ -42,7 +42,7 @@ copy_image() {
local dst="${BUILD_DIR}/$2"
if should_build_image $1; then
echo "Creating $2 from $1..."
$COMMON_PV_CAT "${src}" >"${dst}" || die "Cannot copy $1 to $2"
$(pv_cat_cmd) "${src}" >"${dst}" || die "Cannot copy $1 to $2"
else
mv "${src}" "${dst}" || die "Cannot move $1 to $2"
fi

View File

@ -16,13 +16,22 @@ fi
# Ensure that any sub scripts we invoke get the max proc count.
export NUM_JOBS="${NUM_JOBS}"
# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience
COMMON_PV_OK=1
COMMON_PV_CAT=pv
pv -V >/dev/null 2>&1 || COMMON_PV_OK=0
if [ $COMMON_PV_OK -eq 0 ]; then
COMMON_PV_CAT=cat
fi
# Returns the pv command if it's available, otherwise plain-old cat. Note that
# this function echoes the command, rather than running it, so it can be used
# as an argument to other commands (like sudo).
pv_cat_cmd() {
if pv -V >/dev/null 2>&1; then
# Limit pv's output to 80 columns, for readability.
local term_cols=$(stty size 2>/dev/null | cut -d' ' -f2)
if [ ${term_cols:-0} -gt 80 ]; then
echo pv -w 80
else
echo pv
fi
else
echo cat
fi
}
# Make sure we have the location and name of the calling script, using
# the current value if it is already set.

View File

@ -350,7 +350,7 @@ if [ -b "${FLAGS_to}" ]; then
fi
if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then
sudo ${COMMON_PV_CAT} "${SRC_IMAGE}" |
sudo $(pv_cat_cmd) "${SRC_IMAGE}" |
sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer
sync
else
@ -369,7 +369,7 @@ else
# Output to a file, so just make a copy.
if [ "${SRC_IMAGE}" != "${FLAGS_to}" ]; then
echo "Copying image ${SRC_IMAGE} to file ${FLAGS_to}..."
${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}"
$(pv_cat_cmd) "${SRC_IMAGE}" >"${FLAGS_to}"
fi
info "To copy onto a USB/MMC drive /dev/sdX, use: "