[Libguestfs] [libguestfs-common PATCH 2/3] Tools_utils.wrap: only wrap text for TTYs

Richard W.M. Jones rjones at redhat.com
Sat Feb 12 18:13:11 UTC 2022


On Fri, Feb 11, 2022 at 04:32:24PM +0100, Laszlo Ersek wrote:
> Whenever stdout and/or stderr is redirected (usually to a regular file),
> users mostly do that to capture logs, for later searching. Wrapping
> interferes with grep though, so make "wrap" just print the string when the
> output channel is not a TTY. The requested indentation is honored though.
> 
> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1820221
> Signed-off-by: Laszlo Ersek <lersek at redhat.com>
> ---
> 
> Notes:
>     Easily testable e.g. with virt-resize:
>     
>     $ guestfish -N fs:ext4 exit
>     $ truncate -s 2G test1.resized.img
>     $ virt-resize --expand /dev/sda1 test1.img test1.resized.img
>     $ virt-resize --expand /dev/sda1 test1.img test1.resized.img > log
>     $ cat log
> 
>  mltools/tools_utils.ml | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/mltools/tools_utils.ml b/mltools/tools_utils.ml
> index 06ba2f7ab295..4c5188988e03 100644
> --- a/mltools/tools_utils.ml
> +++ b/mltools/tools_utils.ml
> @@ -126,8 +126,13 @@ let message fs =
>  type wrap_break_t = WrapEOS | WrapSpace | WrapNL
>  
>  let rec wrap ?(chan = stdout) ?(indent = 0) str =
> -  let len = String.length str in
> -  _wrap chan indent 0 0 len str
> +  if istty chan then
> +    let len = String.length str in
> +    _wrap chan indent 0 0 len str
> +  else (
> +    output_spaces chan indent;
> +    output_string chan str
> +  )
>  
>  and _wrap chan indent column i len str =
>    if i < len then (

Reviewed-by: Richard W.M. Jones <rjones at redhat.com>

(Now it's irritating me that the function is called istty :-)

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list