Make use of Unix.isatty instead of our TTY.isatty_stdout, as the supported OCaml provides the former already. --- mllib/common_utils.ml | 10 +++++----- mllib/progress.ml | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index 295981c..9fcd8dd 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -207,15 +207,15 @@ let rec combine3 xs ys zs (* ANSI terminal colours. *) let ansi_green ?(chan = stdout) () - if TTY.isatty_stdout () then output_string chan "\x1b[0;32m" + if Unix.isatty Unix.stdout then output_string chan "\x1b[0;32m" let ansi_red ?(chan = stdout) () - if TTY.isatty_stdout () then output_string chan "\x1b[1;31m" + if Unix.isatty Unix.stdout then output_string chan "\x1b[1;31m" let ansi_blue ?(chan = stdout) () - if TTY.isatty_stdout () then output_string chan "\x1b[1;34m" + if Unix.isatty Unix.stdout then output_string chan "\x1b[1;34m" let ansi_magenta ?(chan = stdout) () - if TTY.isatty_stdout () then output_string chan "\x1b[1;35m" + if Unix.isatty Unix.stdout then output_string chan "\x1b[1;35m" let ansi_restore ?(chan = stdout) () - if TTY.isatty_stdout () then output_string chan "\x1b[0m" + if Unix.isatty Unix.stdout then output_string chan "\x1b[0m" (* Timestamped progress messages, used for ordinary messages when not * --quiet. diff --git a/mllib/progress.ml b/mllib/progress.ml index 194120e..8cf5875 100644 --- a/mllib/progress.ml +++ b/mllib/progress.ml @@ -33,7 +33,7 @@ let set_up_progress_bar ?(machine_readable = false) (g : Guestfs.guestfs) (* Only display progress bars if the machine_readable flag is set or * the output is a tty. *) - if machine_readable || TTY.isatty_stdout () then ( + if machine_readable || Unix.isatty Unix.stdout then ( (* Initialize the C mini library. *) let bar = progress_bar_init ~machine_readable in -- 1.9.3
The only function provided is isatty_stdout, which is no more used now in favour of Unix.isatty. --- builder/Makefile.am | 2 -- customize/Makefile.am | 2 -- mllib/Makefile.am | 9 ++------- mllib/tTY.ml | 19 ------------------- mllib/tTY.mli | 23 ----------------------- mllib/tty-c.c | 40 ---------------------------------------- po/POTFILES | 1 - po/POTFILES-ml | 1 - resize/Makefile.am | 2 -- sparsify/Makefile.am | 2 -- sysprep/Makefile.am | 2 -- v2v/Makefile.am | 2 -- 12 files changed, 2 insertions(+), 103 deletions(-) delete mode 100644 mllib/tTY.ml delete mode 100644 mllib/tTY.mli delete mode 100644 mllib/tty-c.c diff --git a/builder/Makefile.am b/builder/Makefile.am index 68c5556..7334031 100644 --- a/builder/Makefile.am +++ b/builder/Makefile.am @@ -89,8 +89,6 @@ 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 51ec72a..a95025d 100644 --- a/customize/Makefile.am +++ b/customize/Makefile.am @@ -68,8 +68,6 @@ 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 c4e64d2..b2b3271 100644 --- a/mllib/Makefile.am +++ b/mllib/Makefile.am @@ -44,9 +44,6 @@ SOURCES = \ progress.ml \ regedit.mli \ regedit.ml \ - tty-c.c \ - tTY.mli \ - tTY.ml \ uri-c.c \ uRI.mli \ uRI.ml @@ -60,7 +57,6 @@ if HAVE_OCAML ocaml_modules = config \ libdir \ common_gettext \ - tTY \ common_utils \ fsync \ progress \ @@ -73,7 +69,6 @@ ocaml_modules = config \ OBJECTS = \ $(top_builddir)/fish/guestfish-progress.o \ $(top_builddir)/fish/guestfish-uri.o \ - tty-c.o \ fsync-c.o \ progress-c.o \ uri-c.o \ @@ -152,11 +147,11 @@ DEFAULT_INCLUDES = \ check_SCRIPTS = common_utils_tests if HAVE_OCAMLOPT -common_utils_tests: common_gettext.cmx tty-c.o tTY.cmx common_utils.cmx common_utils_tests.cmx +common_utils_tests: common_gettext.cmx common_utils.cmx common_utils_tests.cmx $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \ mlguestfs.cmxa -linkpkg $^ -cclib '$(LIBTINFO_LIBS)' -o $@ else -common_utils_tests: common_gettext.cmo tty-c.o tTY.cmo common_utils.cmo common_utils_tests.cmo +common_utils_tests: common_gettext.cmo common_utils.cmo common_utils_tests.cmo $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) \ mlguestfs.cma -linkpkg $^ -cclib '$(LIBTINFO_LIBS)' -custom -o $@ endif diff --git a/mllib/tTY.ml b/mllib/tTY.ml deleted file mode 100644 index 80f0e1e..0000000 --- a/mllib/tTY.ml +++ /dev/null @@ -1,19 +0,0 @@ -(* virt-resize - * Copyright (C) 2013 Red Hat Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *) - -external isatty_stdout : unit -> bool = "virt_resize_isatty_stdout" "noalloc" diff --git a/mllib/tTY.mli b/mllib/tTY.mli deleted file mode 100644 index 69441f1..0000000 --- a/mllib/tTY.mli +++ /dev/null @@ -1,23 +0,0 @@ -(* virt-resize - * Copyright (C) 2013 Red Hat Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *) - -(** TTY utilities. *) - -val isatty_stdout : unit -> bool -(** RHEL 5-era ocaml didn't have Unix.isatty. This is not needed in - recent OCaml. *) diff --git a/mllib/tty-c.c b/mllib/tty-c.c deleted file mode 100644 index 369b647..0000000 --- a/mllib/tty-c.c +++ /dev/null @@ -1,40 +0,0 @@ -/* virt-resize - interface to isatty - * Copyright (C) 2013 Red Hat Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include <config.h> - -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> - -#include <caml/memory.h> -#include <caml/mlvalues.h> - -#pragma GCC diagnostic ignored "-Wmissing-prototypes" - -/* RHEL 5-era ocaml didn't have Unix.isatty. - * - * Note this function is marked as "noalloc" so it must not call any - * OCaml allocation functions: - * http://camltastic.blogspot.co.uk/2008/08/tip-calling-c-functions-directly-with.html - */ -value -virt_resize_isatty_stdout (value unitv) -{ - return isatty (1) ? Val_true : Val_false; -} diff --git a/po/POTFILES b/po/POTFILES index 1a088f5..9333ebe 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -252,7 +252,6 @@ make-fs/make-fs.c mllib/fsync-c.c mllib/mkdtemp-c.c mllib/progress-c.c -mllib/tty-c.c mllib/uri-c.c ocaml/guestfs-c-actions.c ocaml/guestfs-c.c diff --git a/po/POTFILES-ml b/po/POTFILES-ml index b6d88b0..4ebcc09 100644 --- a/po/POTFILES-ml +++ b/po/POTFILES-ml @@ -39,7 +39,6 @@ mllib/mkdtemp.ml mllib/planner.ml mllib/progress.ml mllib/regedit.ml -mllib/tTY.ml mllib/uRI.ml resize/resize.ml sparsify/cmdline.ml diff --git a/resize/Makefile.am b/resize/Makefile.am index a765587..234b904 100644 --- a/resize/Makefile.am +++ b/resize/Makefile.am @@ -32,8 +32,6 @@ if HAVE_OCAML # Note this list must be in dependency order. deps = \ - $(top_builddir)/mllib/tty-c.o \ - $(top_builddir)/mllib/tTY.cmx \ $(top_builddir)/mllib/fsync-c.o \ $(top_builddir)/mllib/fsync.cmx \ $(top_builddir)/fish/guestfish-progress.o \ diff --git a/sparsify/Makefile.am b/sparsify/Makefile.am index 402ccc8..32552e5 100644 --- a/sparsify/Makefile.am +++ b/sparsify/Makefile.am @@ -34,7 +34,6 @@ SOURCES_ML = \ SOURCES_C = \ $(top_builddir)/fish/progress.c \ - $(top_builddir)/mllib/tty-c.c \ $(top_builddir)/mllib/progress-c.c \ statvfs-c.c @@ -54,7 +53,6 @@ virt_sparsify_CFLAGS = \ BOBJECTS = \ $(top_builddir)/mllib/common_gettext.cmo \ - $(top_builddir)/mllib/tTY.cmo \ $(top_builddir)/mllib/common_utils.cmo \ $(top_builddir)/mllib/progress.cmo \ $(top_builddir)/mllib/config.cmo \ diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am index f7ef265..c00f53c 100644 --- a/sysprep/Makefile.am +++ b/sysprep/Makefile.am @@ -82,8 +82,6 @@ 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 d1c9169..b5a4dc4 100644 --- a/v2v/Makefile.am +++ b/v2v/Makefile.am @@ -95,7 +95,6 @@ SOURCES_ML = \ SOURCES_C = \ $(top_builddir)/fish/progress.c \ $(top_builddir)/fish/file-edit.c \ - $(top_builddir)/mllib/tty-c.c \ $(top_builddir)/mllib/progress-c.c \ $(top_builddir)/mllib/mkdtemp-c.c \ $(top_builddir)/customize/crypt-c.c \ @@ -123,7 +122,6 @@ 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/progress.cmo \ -- 1.9.3
Pino Toscano
2014-Nov-25 10:59 UTC
[Libguestfs] [PATCH] mllib: check the right out channel for tty
Make sure to check whether the given output channel is a tty before sending ANSI terminal codes to it, instead of always check what stdout is. --- mllib/common_utils.ml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index 9fcd8dd..83ebd3a 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -205,17 +205,20 @@ let rec combine3 xs ys zs | x::xs, y::ys, z::zs -> (x, y, z) :: combine3 xs ys zs | _ -> invalid_arg "combine3" +let istty chan + Unix.isatty (Unix.descr_of_out_channel chan) + (* ANSI terminal colours. *) let ansi_green ?(chan = stdout) () - if Unix.isatty Unix.stdout then output_string chan "\x1b[0;32m" + if istty chan then output_string chan "\x1b[0;32m" let ansi_red ?(chan = stdout) () - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;31m" + if istty chan then output_string chan "\x1b[1;31m" let ansi_blue ?(chan = stdout) () - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;34m" + if istty chan then output_string chan "\x1b[1;34m" let ansi_magenta ?(chan = stdout) () - if Unix.isatty Unix.stdout then output_string chan "\x1b[1;35m" + if istty chan then output_string chan "\x1b[1;35m" let ansi_restore ?(chan = stdout) () - if Unix.isatty Unix.stdout then output_string chan "\x1b[0m" + if istty chan then output_string chan "\x1b[0m" (* Timestamped progress messages, used for ordinary messages when not * --quiet. -- 1.9.3
On Tue, Nov 25, 2014 at 11:59:06AM +0100, Pino Toscano wrote:> Make use of Unix.isatty instead of our TTY.isatty_stdout, as the > supported OCaml provides the former already.I was meaning to do this. ACK series. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Possibly Parallel Threads
- [PATCH 0/7] p2v: Multiple improvements to the look of virt-p2v.
- [PATCH v2 0/4] OCaml tools: output messages as JSON machine
- [PATCH 0/3] Move Curl wrapper to mllib and use it for virt-builder.
- [PATCH 1/2] configure: look for the oUnit OCaml module
- [PATCH 0/2] Build mllib and customize into libraries.