[Pkg-libvirt-commits] [libguestfs] 03/179: mllib: Coloured messages, errors, warnings.
Hilko Bengen
bengen at moszumanska.debian.org
Fri Oct 31 19:07:52 UTC 2014
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to branch experimental
in repository libguestfs.
commit 9018a23828b5c65b677a4ab1d21b776c6d225c73
Author: Richard W.M. Jones <rjones at redhat.com>
Date: Thu Oct 2 13:19:03 2014 +0100
mllib: Coloured messages, errors, warnings.
Uses ANSI terminal codes to colour the output.
---
builder/Makefile.am | 2 ++
customize/Makefile.am | 2 ++
mllib/Makefile.am | 6 +++---
mllib/common_utils.ml | 28 ++++++++++++++++++++++++----
sparsify/Makefile.am | 2 +-
sysprep/Makefile.am | 2 ++
v2v/Makefile.am | 2 +-
7 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/builder/Makefile.am b/builder/Makefile.am
index eb6295a..4b1e1b8 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -87,6 +87,8 @@ deps = \
$(top_builddir)/mllib/libdir.cmx \
$(top_builddir)/mllib/config.cmx \
$(top_builddir)/mllib/common_gettext.cmx \
+ $(top_builddir)/mllib/tty-c.o \
+ $(top_builddir)/mllib/tTY.cmx \
$(top_builddir)/mllib/common_utils.cmx \
$(top_builddir)/mllib/fsync-c.o \
$(top_builddir)/mllib/fsync.cmx \
diff --git a/customize/Makefile.am b/customize/Makefile.am
index 746375d..0760476 100644
--- a/customize/Makefile.am
+++ b/customize/Makefile.am
@@ -65,6 +65,8 @@ deps = \
$(top_builddir)/fish/guestfish-uri.o \
$(top_builddir)/fish/guestfish-file-edit.o \
$(top_builddir)/mllib/common_gettext.cmx \
+ $(top_builddir)/mllib/tty-c.o \
+ $(top_builddir)/mllib/tTY.cmx \
$(top_builddir)/mllib/common_utils.cmx \
$(top_builddir)/mllib/config.cmx \
$(top_builddir)/mllib/regedit.cmx \
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 210fc64..ac953ac 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -58,8 +58,8 @@ if HAVE_OCAML
ocaml_modules = config \
libdir \
common_gettext \
- common_utils \
tTY \
+ common_utils \
fsync \
progress \
uRI \
@@ -148,11 +148,11 @@ DEFAULT_INCLUDES = \
check_SCRIPTS = common_utils_tests
if HAVE_OCAMLOPT
-common_utils_tests: common_gettext.cmx common_utils.cmx common_utils_tests.cmx
+common_utils_tests: common_gettext.cmx tty-c.o tTY.cmx common_utils.cmx common_utils_tests.cmx
$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
mlguestfs.cmxa -linkpkg $^ -cclib -lncurses -o $@
else
-common_utils_tests: common_gettext.cmo common_utils.cmo common_utils_tests.cmo
+common_utils_tests: common_gettext.cmo tty-c.o tTY.cmo common_utils.cmo common_utils_tests.cmo
$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) \
mlguestfs.cma -linkpkg $^ -cclib -lncurses -custom -o $@
endif
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 62d0782..d3fec06 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -199,6 +199,16 @@ let rec mapi i f =
r :: mapi (i + 1) f l
let mapi f l = mapi 0 f l
+(* ANSI terminal colours. *)
+let ansi_green ?(chan = stdout) () =
+ if TTY.isatty_stdout () then output_string chan "\x1b[0;32m"
+let ansi_red ?(chan = stdout) () =
+ if TTY.isatty_stdout () then output_string chan "\x1b[1;31m"
+let ansi_blue ?(chan = stdout) () =
+ if TTY.isatty_stdout () then output_string chan "\x1b[1;34m"
+let ansi_restore ?(chan = stdout) () =
+ if TTY.isatty_stdout () then output_string chan "\x1b[0m"
+
(* Timestamped progress messages, used for ordinary messages when not
* --quiet.
*)
@@ -207,19 +217,26 @@ let make_message_function ~quiet fs =
let p str =
if not quiet then (
let t = sprintf "%.1f" (Unix.time () -. start_t) in
- printf "[%6s] %s\n%!" t str
+ printf "[%6s] " t;
+ ansi_green ();
+ printf "%s" str;
+ ansi_restore ();
+ print_newline ()
)
in
ksprintf p fs
let error ~prog ?(exit_code = 1) fs =
let display str =
- wrap ~chan:stderr (sprintf (f_"%s: error: %s") prog str);
+ let chan = stderr in
+ ansi_red ~chan ();
+ wrap ~chan (sprintf (f_"%s: error: %s") prog str);
prerr_newline ();
prerr_newline ();
- wrap ~chan:stderr
+ wrap ~chan
(sprintf (f_"If reporting bugs, run %s with debugging enabled and include the complete output:\n\n %s -v -x [...]")
prog prog);
+ ansi_restore ~chan ();
prerr_newline ();
exit exit_code
in
@@ -227,7 +244,10 @@ let error ~prog ?(exit_code = 1) fs =
let warning ~prog fs =
let display str =
- wrap ~chan:stderr (sprintf (f_"%s: warning: %s") prog str);
+ let chan = stderr in
+ ansi_blue ~chan ();
+ wrap ~chan (sprintf (f_"%s: warning: %s") prog str);
+ ansi_restore ~chan ();
prerr_newline ();
in
ksprintf display fs
diff --git a/sparsify/Makefile.am b/sparsify/Makefile.am
index fc2777d..48cd5f0 100644
--- a/sparsify/Makefile.am
+++ b/sparsify/Makefile.am
@@ -53,8 +53,8 @@ virt_sparsify_CFLAGS = \
BOBJECTS = \
$(top_builddir)/mllib/common_gettext.cmo \
- $(top_builddir)/mllib/common_utils.cmo \
$(top_builddir)/mllib/tTY.cmo \
+ $(top_builddir)/mllib/common_utils.cmo \
$(top_builddir)/mllib/progress.cmo \
$(top_builddir)/mllib/config.cmo \
$(SOURCES_ML:.ml=.cmo)
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index d6dae86..b1cebc2 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -82,6 +82,8 @@ if HAVE_OCAML
# Note this list must be in dependency order.
deps = \
$(top_builddir)/mllib/common_gettext.cmx \
+ $(top_builddir)/mllib/tty-c.o \
+ $(top_builddir)/mllib/tTY.cmx \
$(top_builddir)/mllib/common_utils.cmx \
$(top_builddir)/mllib/uri-c.o \
$(top_builddir)/mllib/uRI.cmx \
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index db96b64..39cf392 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -116,9 +116,9 @@ virt_v2v_CFLAGS = \
BOBJECTS = \
$(top_builddir)/mllib/common_gettext.cmo \
+ $(top_builddir)/mllib/tTY.cmo \
$(top_builddir)/mllib/common_utils.cmo \
$(top_builddir)/mllib/regedit.cmo \
- $(top_builddir)/mllib/tTY.cmo \
$(top_builddir)/mllib/progress.cmo \
$(top_builddir)/mllib/config.cmo \
$(top_builddir)/mllib/mkdtemp.cmo \
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git
More information about the Pkg-libvirt-commits
mailing list