From 778a78bb9f5136d328f2692cf25975317d63665f Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Wed, 10 Jan 2018 20:58:47 +0000 Subject: [PATCH] app/status: factor out escape chars logic Make use of the new `glnx_stdout_is_console ()` rather than caching our own result of `isatty()`. Add helper functions in `libbuiltin.h` to retrieve escape characters. Prep for using them from other files. Update submodule: libglnx Closes: #1196 Approved by: cgwalters --- libglnx | 2 +- src/app/rpmostree-builtin-status.c | 26 +++++++++++--------------- src/app/rpmostree-libbuiltin.h | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/libglnx b/libglnx index 96b1fd95..6f1ee5db 160000 --- a/libglnx +++ b/libglnx @@ -1 +1 @@ -Subproject commit 96b1fd9578b3d6ff2d8e0707068f5ef450eba98c +Subproject commit 6f1ee5db1400b13a9a0fa0b2274ae34e8710c1aa diff --git a/src/app/rpmostree-builtin-status.c b/src/app/rpmostree-builtin-status.c index 880746ab..4b8e36b8 100644 --- a/src/app/rpmostree-builtin-status.c +++ b/src/app/rpmostree-builtin-status.c @@ -28,6 +28,7 @@ #include #include "rpmostree-builtins.h" +#include "rpmostree-libbuiltin.h" #include "rpmostree-dbus-helpers.h" #include "rpmostree-util.h" #include "rpmostree-rpm-util.h" @@ -206,11 +207,6 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy, { GVariantIter iter; gboolean first = TRUE; - const int is_tty = isatty (1); - const char *bold_prefix = is_tty ? "\x1b[1m" : ""; - const char *bold_suffix = is_tty ? "\x1b[0m" : ""; - const char *red_prefix = is_tty ? "\x1b[31m" : ""; - const char *red_suffix = is_tty ? "\x1b[22m" : ""; /* First, gather global state */ gboolean have_any_live_overlay = FALSE; @@ -380,8 +376,8 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy, if (version_string) { g_autofree char *version_time - = g_strdup_printf ("%s%s%s (%s)", bold_prefix, version_string, - bold_suffix, timestamp_string); + = g_strdup_printf ("%s%s%s (%s)", get_bold_start (), version_string, + get_bold_end (), timestamp_string); print_kv ("Version", max_key_len, version_time); } else @@ -415,18 +411,18 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy, if (live_inprogress) { if (is_booted) - g_print ("%s%s", red_prefix, bold_prefix); + g_print ("%s%s", get_red_start (), get_bold_start ()); print_kv ("InterruptedLiveCommit", max_key_len, live_inprogress); if (is_booted) - g_print ("%s%s", bold_suffix, red_suffix); + g_print ("%s%s", get_bold_end (), get_red_end ()); } if (live_replaced) { if (is_booted) - g_print ("%s%s", red_prefix, bold_prefix); + g_print ("%s%s", get_red_start (), get_bold_start ()); print_kv ("LiveCommit", max_key_len, live_replaced); if (is_booted) - g_print ("%s%s", bold_suffix, red_suffix); + g_print ("%s%s", get_bold_end (), get_red_end ()); } /* Show any difference between the baseref vs head, but only for the @@ -635,9 +631,9 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy, if (unlocked && g_strcmp0 (unlocked, "none") != 0) { - g_print ("%s%s", red_prefix, bold_prefix); + g_print ("%s%s", get_red_start (), get_bold_start ()); print_kv ("Unlocked", max_key_len, unlocked); - g_print ("%s%s", bold_suffix, red_suffix); + g_print ("%s%s", get_bold_end (), get_red_end ()); } const char *end_of_life_string = NULL; /* look for endoflife attribute in the deployment */ @@ -645,9 +641,9 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy, if (end_of_life_string) { - g_print ("%s%s", red_prefix, bold_prefix); + g_print ("%s%s", get_red_start (), get_bold_start ()); print_kv ("EndOfLife", max_key_len, end_of_life_string); - g_print ("%s%s", bold_suffix, red_suffix); + g_print ("%s%s", get_bold_end (), get_red_end ()); } } diff --git a/src/app/rpmostree-libbuiltin.h b/src/app/rpmostree-libbuiltin.h index b96528ae..87206ea5 100644 --- a/src/app/rpmostree-libbuiltin.h +++ b/src/app/rpmostree-libbuiltin.h @@ -22,10 +22,26 @@ #include +#include "libglnx.h" + #include "rpm-ostreed-generated.h" G_BEGIN_DECLS +#define TERM_ESCAPE_SEQUENCE(type,seq) \ + static inline const char* get_##type (void) { \ + if (glnx_stdout_is_tty ()) \ + return seq; \ + return ""; \ + } + +TERM_ESCAPE_SEQUENCE(red_start, "\x1b[31m") +TERM_ESCAPE_SEQUENCE(red_end, "\x1b[22m") +TERM_ESCAPE_SEQUENCE(bold_start, "\x1b[1m") +TERM_ESCAPE_SEQUENCE(bold_end, "\x1b[0m") + +#undef TERM_ESCAPE_SEQUENCE + void rpmostree_usage_error (GOptionContext *context, const char *message,