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

Laszlo Ersek lersek at redhat.com
Fri Feb 11 15:32:24 UTC 2022


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 (
-- 
2.19.1.3.g30247aa5d201




More information about the Libguestfs mailing list