Skip to content

Commit

Permalink
virt tools: Add common --colours option.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwmjones committed Jun 18, 2016
1 parent 5b13135 commit b000d6b
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 17 deletions.
9 changes: 9 additions & 0 deletions builder/virt-builder.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions customize/virt-customize.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions dib/virt-dib.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions get-kernel/virt-get-kernel.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 25 additions & 17 deletions mllib/common_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions resize/virt-resize.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
9 changes: 9 additions & 0 deletions sparsify/virt-sparsify.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions sysprep/virt-sysprep.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions v2v/cmdline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand Down
9 changes: 9 additions & 0 deletions v2v/virt-v2v.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b000d6b

Please sign in to comment.