[Libguestfs] [PATCH 2/7] virt tools: Add common --colours option.

Richard W.M. Jones rjones at redhat.com
Sat Jun 18 15:09:12 UTC 2016


This option (alternately spelled: --color, --colour, --colors, or
--colours) enables ANSI colour sequences output even if that would be
disabled becaues the output is not a TTY.
---
 builder/virt-builder.pod       |  9 +++++++++
 customize/virt-customize.pod   |  9 +++++++++
 dib/virt-dib.pod               |  9 +++++++++
 get-kernel/virt-get-kernel.pod |  9 +++++++++
 mllib/common_utils.ml          | 42 +++++++++++++++++++++++++-----------------
 resize/virt-resize.pod         |  9 +++++++++
 sparsify/virt-sparsify.pod     |  9 +++++++++
 sysprep/virt-sysprep.pod       |  9 +++++++++
 v2v/cmdline.ml                 |  1 +
 v2v/virt-v2v.pod               |  9 +++++++++
 10 files changed, 98 insertions(+), 17 deletions(-)

diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod
index cd3be9d..91c1114 100644
--- a/builder/virt-builder.pod
+++ b/builder/virt-builder.pod
@@ -241,6 +241,15 @@ Using I<--no-check-signature> bypasses this check.
 
 See also I<--fingerprint>.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<--curl> CURL
 
 Specify an alternate L<curl(1)> binary.  You can also use this to add
diff --git a/customize/virt-customize.pod b/customize/virt-customize.pod
index 7654fee..e594f61 100644
--- a/customize/virt-customize.pod
+++ b/customize/virt-customize.pod
@@ -74,6 +74,15 @@ disk format (not just an ISO).
 Specify the disk format for the next I<--attach> option.  The
 C<FORMAT> is usually C<raw> or C<qcow2>.  Use C<raw> for ISOs.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<-c> URI
 
 =item B<--connect> URI
diff --git a/dib/virt-dib.pod b/dib/virt-dib.pod
index 9baae23..8ccb9f5 100644
--- a/dib/virt-dib.pod
+++ b/dib/virt-dib.pod
@@ -72,6 +72,15 @@ Right now this option does nothing more than setting the C<ARCH>
 environment variable for the elements, and it's up to them to
 produce an image for the requested architecture.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<--debug> LEVEL
 
 Set the debug level to C<LEVEL>, which is a non-negative integer
diff --git a/get-kernel/virt-get-kernel.pod b/get-kernel/virt-get-kernel.pod
index 92d6cb6..97a159c 100644
--- a/get-kernel/virt-get-kernel.pod
+++ b/get-kernel/virt-get-kernel.pod
@@ -44,6 +44,15 @@ force a particular format use the I<--format> option.
 Add a remote disk.  The URI format is compatible with guestfish.
 See L<guestfish(1)/ADDING REMOTE STORAGE>.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<-c> URI
 
 =item B<--connect> URI
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index e54272c..a663027 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -295,27 +295,16 @@ let protect ~f ~finally =
   finally ();
   match r with Either ret -> ret | Or exn -> raise exn
 
-let istty chan =
-  Unix.isatty (Unix.descr_of_out_channel chan)
-
-(* ANSI terminal colours. *)
-let ansi_green ?(chan = stdout) () =
-  if istty chan then output_string chan "\x1b[0;32m"
-let ansi_red ?(chan = stdout) () =
-  if istty chan then output_string chan "\x1b[1;31m"
-let ansi_blue ?(chan = stdout) () =
-  if istty chan then output_string chan "\x1b[1;34m"
-let ansi_magenta ?(chan = stdout) () =
-  if istty chan then output_string chan "\x1b[1;35m"
-let ansi_restore ?(chan = stdout) () =
-  if istty chan then output_string chan "\x1b[0m"
-
 (* Program name. *)
 let prog = Filename.basename Sys.executable_name
 
-(* Stores the quiet (--quiet), trace (-x) and verbose (-v) flags in a
- * global variable.
+(* Stores the colours (--colours), quiet (--quiet), trace (-x) and
+ * verbose (-v) flags in a global variable.
  *)
+let colours = ref false
+let set_colours () = colours := true
+let colours () = !colours
+
 let quiet = ref false
 let set_quiet () = quiet := true
 let quiet () = !quiet
@@ -328,6 +317,21 @@ let verbose = ref false
 let set_verbose () = verbose := true
 let verbose () = !verbose
 
+(* ANSI terminal colours. *)
+let istty chan =
+  Unix.isatty (Unix.descr_of_out_channel chan)
+
+let ansi_green ?(chan = stdout) () =
+  if colours () || istty chan then output_string chan "\x1b[0;32m"
+let ansi_red ?(chan = stdout) () =
+  if colours () || istty chan then output_string chan "\x1b[1;31m"
+let ansi_blue ?(chan = stdout) () =
+  if colours () || istty chan then output_string chan "\x1b[1;34m"
+let ansi_magenta ?(chan = stdout) () =
+  if colours () || istty chan then output_string chan "\x1b[1;35m"
+let ansi_restore ?(chan = stdout) () =
+  if colours () || istty chan then output_string chan "\x1b[0m"
+
 (* Timestamped progress messages, used for ordinary messages when not
  * --quiet.
  *)
@@ -592,6 +596,10 @@ let set_standard_options argspec =
     "--debug-gc",   Arg.Unit set_debug_gc,     " " ^ s_"Debug GC and memory allocations (internal)";
     "-q",           Arg.Unit set_quiet,        " " ^ s_"Don't print progress messages";
     "--quiet",      Arg.Unit set_quiet,        " " ^ s_"Don't print progress messages";
+    "--color",      Arg.Unit set_colours,      " " ^ s_"Use ANSI colour sequences even if not tty";
+    "--colors",     Arg.Unit set_colours,      " " ^ s_"Use ANSI colour sequences even if not tty";
+    "--colour",     Arg.Unit set_colours,      " " ^ s_"Use ANSI colour sequences even if not tty";
+    "--colours",    Arg.Unit set_colours,      " " ^ s_"Use ANSI colour sequences even if not tty";
   ] @ argspec in
   let argspec =
     let cmp (arg1, _, _) (arg2, _, _) = compare_command_line_args arg1 arg2 in
diff --git a/resize/virt-resize.pod b/resize/virt-resize.pod
index a0ab459..2b2a485 100644
--- a/resize/virt-resize.pod
+++ b/resize/virt-resize.pod
@@ -327,6 +327,15 @@ since around 2008.
 
 =back
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<-d>
 
 =item B<--debug>
diff --git a/sparsify/virt-sparsify.pod b/sparsify/virt-sparsify.pod
index b841251..44f7d4e 100644
--- a/sparsify/virt-sparsify.pod
+++ b/sparsify/virt-sparsify.pod
@@ -155,6 +155,15 @@ B<fail> and exit.
 
 You cannot use this option and I<--in-place> together.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<--compress>
 
 Compress the output file.  This I<only> works if the output format is
diff --git a/sysprep/virt-sysprep.pod b/sysprep/virt-sysprep.pod
index 0c1c7c6..bdb4580 100644
--- a/sysprep/virt-sysprep.pod
+++ b/sysprep/virt-sysprep.pod
@@ -59,6 +59,15 @@ force a particular format use the I<--format> option.
 Add a remote disk.  The URI format is compatible with guestfish.
 See L<guestfish(1)/ADDING REMOTE STORAGE>.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<-c> URI
 
 =item B<--connect> URI
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index c05dbe3..1064987 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -278,6 +278,7 @@ read the man page virt-v2v(1).
   if args = [] && machine_readable then (
     printf "virt-v2v\n";
     printf "libguestfs-rewrite\n";
+    printf "colours-option\n";
     List.iter (printf "input:%s\n") (Modules_list.input_modules ());
     List.iter (printf "output:%s\n") (Modules_list.output_modules ());
     List.iter (printf "convert:%s\n") (Modules_list.convert_modules ());
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
index 292c5a3..a781eb3 100644
--- a/v2v/virt-v2v.pod
+++ b/v2v/virt-v2v.pod
@@ -257,6 +257,15 @@ Display help.
 
 See I<--network> below.
 
+=item B<--colors>
+
+=item B<--colours>
+
+Use ANSI colour sequences to colourize messages.  This is the default
+when the output is a tty.  If the output of the program is redirected
+to a file, ANSI colour sequences are disabled unless you use this
+option.
+
 =item B<--compressed>
 
 Write a compressed output file.  This is only allowed if the output
-- 
2.7.4




More information about the Libguestfs mailing list