307 lines
12 KiB
Diff
307 lines
12 KiB
Diff
From 2c8837069cfd00a0a440d229f2086899cf43cd62 Mon Sep 17 00:00:00 2001
|
|
From: Daniel Schaal <farbing@web.de>
|
|
Date: Fri, 2 Aug 2013 07:59:02 +0200
|
|
Subject: [PATCH] systemd-delta: Only print colors when on a tty
|
|
|
|
This make systemd-delta follow the behaviour of systemctl
|
|
and journalctl.
|
|
|
|
https://bugs.freedesktop.org/show_bug.cgi?id=67656
|
|
|
|
[zj: unify color query methods between those three programs.]
|
|
---
|
|
src/delta/delta.c | 15 +++++---
|
|
src/journal/journalctl.c | 5 ++-
|
|
src/shared/util.h | 16 +++++++++
|
|
src/systemctl/systemctl.c | 88 +++++++++++++++++------------------------------
|
|
4 files changed, 60 insertions(+), 64 deletions(-)
|
|
|
|
diff --git a/src/delta/delta.c b/src/delta/delta.c
|
|
index 49c2fc3..b3272d9 100644
|
|
--- a/src/delta/delta.c
|
|
+++ b/src/delta/delta.c
|
|
@@ -66,7 +66,8 @@ static int notify_override_masked(const char *top, const char *bottom) {
|
|
if (!(arg_flags & SHOW_MASKED))
|
|
return 0;
|
|
|
|
- printf(ANSI_HIGHLIGHT_RED_ON "[MASKED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
|
|
+ printf("%s%s%s %s → %s\n",
|
|
+ ansi_highlight_red(), "[MASKED]", ansi_highlight_off(), top, bottom);
|
|
return 1;
|
|
}
|
|
|
|
@@ -74,7 +75,8 @@ static int notify_override_equivalent(const char *top, const char *bottom) {
|
|
if (!(arg_flags & SHOW_EQUIVALENT))
|
|
return 0;
|
|
|
|
- printf(ANSI_HIGHLIGHT_GREEN_ON "[EQUIVALENT]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
|
|
+ printf("%s%s%s %s → %s\n",
|
|
+ ansi_highlight_green(), "[EQUIVALENT]", ansi_highlight(), top, bottom);
|
|
return 1;
|
|
}
|
|
|
|
@@ -82,7 +84,8 @@ static int notify_override_redirected(const char *top, const char *bottom) {
|
|
if (!(arg_flags & SHOW_REDIRECTED))
|
|
return 0;
|
|
|
|
- printf(ANSI_HIGHLIGHT_ON "[REDIRECTED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
|
|
+ printf("%s%s%s %s → %s\n",
|
|
+ ansi_highlight(), "[REDIRECTED]", ansi_highlight_off(), top, bottom);
|
|
return 1;
|
|
}
|
|
|
|
@@ -90,7 +93,8 @@ static int notify_override_overridden(const char *top, const char *bottom) {
|
|
if (!(arg_flags & SHOW_OVERRIDDEN))
|
|
return 0;
|
|
|
|
- printf(ANSI_HIGHLIGHT_ON "[OVERRIDDEN]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
|
|
+ printf("%s%s%s %s → %s\n",
|
|
+ ansi_highlight(), "[OVERRIDDEN]", ansi_highlight_off(), top, bottom);
|
|
return 1;
|
|
}
|
|
|
|
@@ -98,7 +102,8 @@ static int notify_override_extended(const char *top, const char *bottom) {
|
|
if (!(arg_flags & SHOW_EXTENDED))
|
|
return 0;
|
|
|
|
- printf(ANSI_HIGHLIGHT_ON "[EXTENDED]" ANSI_HIGHLIGHT_OFF " %s → %s\n", top, bottom);
|
|
+ printf("%s%s%s %s → %s\n",
|
|
+ ansi_highlight(), "[EXTENDED]", ansi_highlight_off(), top, bottom);
|
|
return 1;
|
|
}
|
|
|
|
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
|
|
index dde2ed7..feea6bf 100644
|
|
--- a/src/journal/journalctl.c
|
|
+++ b/src/journal/journalctl.c
|
|
@@ -1605,14 +1605,13 @@ int main(int argc, char *argv[]) {
|
|
|
|
if (!arg_merge) {
|
|
sd_id128_t boot_id;
|
|
- const char *color_on = on_tty() ? ANSI_HIGHLIGHT_ON : "",
|
|
- *color_off = on_tty() ? ANSI_HIGHLIGHT_OFF : "";
|
|
|
|
r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
|
|
if (r >= 0) {
|
|
if (previous_boot_id_valid &&
|
|
!sd_id128_equal(boot_id, previous_boot_id))
|
|
- printf("%s-- Reboot --%s\n", color_on, color_off);
|
|
+ printf("%s-- Reboot --%s\n",
|
|
+ ansi_highlight(), ansi_highlight_off());
|
|
|
|
previous_boot_id = boot_id;
|
|
previous_boot_id_valid = true;
|
|
diff --git a/src/shared/util.h b/src/shared/util.h
|
|
index ac999c6..3be692e 100644
|
|
--- a/src/shared/util.h
|
|
+++ b/src/shared/util.h
|
|
@@ -374,6 +374,22 @@ void columns_lines_cache_reset(int _unused_ signum);
|
|
|
|
bool on_tty(void);
|
|
|
|
+static inline const char *ansi_highlight(void) {
|
|
+ return on_tty() ? ANSI_HIGHLIGHT_ON : "";
|
|
+}
|
|
+
|
|
+static inline const char *ansi_highlight_red(void) {
|
|
+ return on_tty() ? ANSI_HIGHLIGHT_RED_ON : "";
|
|
+}
|
|
+
|
|
+static inline const char *ansi_highlight_green(void) {
|
|
+ return on_tty() ? ANSI_HIGHLIGHT_GREEN_ON : "";
|
|
+}
|
|
+
|
|
+static inline const char *ansi_highlight_off(void) {
|
|
+ return on_tty() ? ANSI_HIGHLIGHT_OFF : "";
|
|
+}
|
|
+
|
|
int running_in_chroot(void);
|
|
|
|
char *ellipsize(const char *s, size_t length, unsigned percent);
|
|
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
|
|
index 9f47b2c..aca033b 100644
|
|
--- a/src/systemctl/systemctl.c
|
|
+++ b/src/systemctl/systemctl.c
|
|
@@ -175,30 +175,6 @@ static void polkit_agent_open_if_enabled(void) {
|
|
}
|
|
#endif
|
|
|
|
-static const char *ansi_highlight(bool b) {
|
|
-
|
|
- if (!on_tty())
|
|
- return "";
|
|
-
|
|
- return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
|
|
-}
|
|
-
|
|
-static const char *ansi_highlight_red(bool b) {
|
|
-
|
|
- if (!on_tty())
|
|
- return "";
|
|
-
|
|
- return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
|
|
-}
|
|
-
|
|
-static const char *ansi_highlight_green(bool b) {
|
|
-
|
|
- if (!on_tty())
|
|
- return "";
|
|
-
|
|
- return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
|
|
-}
|
|
-
|
|
static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
|
|
assert(error);
|
|
|
|
@@ -381,14 +357,14 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
|
|
|
|
if (streq(u->load_state, "error") ||
|
|
streq(u->load_state, "not-found")) {
|
|
- on_loaded = on = ansi_highlight_red(true);
|
|
- off_loaded = off = ansi_highlight_red(false);
|
|
+ on_loaded = on = ansi_highlight_red();
|
|
+ off_loaded = off = ansi_highlight_off();
|
|
} else
|
|
on_loaded = off_loaded = "";
|
|
|
|
if (streq(u->active_state, "failed")) {
|
|
- on_active = on = ansi_highlight_red(true);
|
|
- off_active = off = ansi_highlight_red(false);
|
|
+ on_active = on = ansi_highlight_red();
|
|
+ off_active = off = ansi_highlight_off();
|
|
} else
|
|
on_active = off_active = "";
|
|
|
|
@@ -416,11 +392,11 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
|
|
if (job_count)
|
|
printf("JOB = Pending job for the unit.\n");
|
|
puts("");
|
|
- on = ansi_highlight(true);
|
|
- off = ansi_highlight(false);
|
|
+ on = ansi_highlight();
|
|
+ off = ansi_highlight_off();
|
|
} else {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
}
|
|
|
|
if (arg_all)
|
|
@@ -683,13 +659,13 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
|
|
printf("\n");
|
|
}
|
|
|
|
- on = ansi_highlight(true);
|
|
- off = ansi_highlight(false);
|
|
+ on = ansi_highlight();
|
|
+ off = ansi_highlight_off();
|
|
if (!arg_no_legend)
|
|
printf("\n");
|
|
} else {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
}
|
|
|
|
if (!arg_no_legend) {
|
|
@@ -838,11 +814,11 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
|
|
u->state == UNIT_FILE_MASKED_RUNTIME ||
|
|
u->state == UNIT_FILE_DISABLED ||
|
|
u->state == UNIT_FILE_INVALID) {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
} else if (u->state == UNIT_FILE_ENABLED) {
|
|
- on = ansi_highlight_green(true);
|
|
- off = ansi_highlight_green(false);
|
|
+ on = ansi_highlight_green();
|
|
+ off = ansi_highlight_off();
|
|
} else
|
|
on = off = "";
|
|
|
|
@@ -1250,8 +1226,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
|
|
assert(n == 0 || jobs);
|
|
|
|
if (n == 0) {
|
|
- on = ansi_highlight_green(true);
|
|
- off = ansi_highlight_green(false);
|
|
+ on = ansi_highlight_green();
|
|
+ off = ansi_highlight_off();
|
|
|
|
printf("%sNo jobs running.%s\n", on, off);
|
|
return;
|
|
@@ -1287,8 +1263,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
|
|
_cleanup_free_ char *e = NULL;
|
|
|
|
if (streq(j->state, "running")) {
|
|
- on = ansi_highlight(true);
|
|
- off = ansi_highlight(false);
|
|
+ on = ansi_highlight();
|
|
+ off = ansi_highlight_off();
|
|
} else
|
|
on = off = "";
|
|
|
|
@@ -1301,8 +1277,8 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
|
|
}
|
|
}
|
|
|
|
- on = ansi_highlight(true);
|
|
- off = ansi_highlight(false);
|
|
+ on = ansi_highlight();
|
|
+ off = ansi_highlight_off();
|
|
|
|
if (on_tty())
|
|
printf("\n%s%zu jobs listed%s.\n", on, n, off);
|
|
@@ -2588,8 +2564,8 @@ static void print_status_info(UnitStatusInfo *i) {
|
|
printf(" Follow: unit currently follows state of %s\n", i->following);
|
|
|
|
if (streq_ptr(i->load_state, "error")) {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
} else
|
|
on = off = "";
|
|
|
|
@@ -2639,11 +2615,11 @@ static void print_status_info(UnitStatusInfo *i) {
|
|
ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
|
|
|
|
if (streq_ptr(i->active_state, "failed")) {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
} else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
|
|
- on = ansi_highlight_green(true);
|
|
- off = ansi_highlight_green(false);
|
|
+ on = ansi_highlight_green();
|
|
+ off = ansi_highlight_off();
|
|
} else
|
|
on = off = "";
|
|
|
|
@@ -2718,8 +2694,8 @@ static void print_status_info(UnitStatusInfo *i) {
|
|
|
|
good = is_clean_exit_lsb(p->code, p->status, NULL);
|
|
if (!good) {
|
|
- on = ansi_highlight_red(true);
|
|
- off = ansi_highlight_red(false);
|
|
+ on = ansi_highlight_red();
|
|
+ off = ansi_highlight_off();
|
|
} else
|
|
on = off = "";
|
|
|
|
@@ -2838,8 +2814,8 @@ static void print_status_info(UnitStatusInfo *i) {
|
|
|
|
if (i->need_daemon_reload)
|
|
printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %sdaemon-reload' recommended.\n",
|
|
- ansi_highlight_red(true),
|
|
- ansi_highlight_red(false),
|
|
+ ansi_highlight_red(),
|
|
+ ansi_highlight_off(),
|
|
arg_scope == UNIT_FILE_SYSTEM ? "" : "--user ");
|
|
}
|
|
|