summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Schaal <farbing@web.de>2013-08-02 07:59:02 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-08-04 08:33:02 -0400
commit0b5a519c89b5fb48ec7a3cfbd9c02283ae0f23ac (patch)
treef97494aceb6e7feb68c2ef554f4fa59485f6e7f1 /src
parent92d700dea651e2a09ee75c7713b8387f11435cec (diff)
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.]
Diffstat (limited to 'src')
-rw-r--r--src/delta/delta.c15
-rw-r--r--src/journal/journalctl.c5
-rw-r--r--src/shared/util.h16
-rw-r--r--src/systemctl/systemctl.c88
4 files changed, 60 insertions, 64 deletions
diff --git a/src/delta/delta.c b/src/delta/delta.c
index 49c2fc323a..b3272d916e 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 dde2ed7e37..feea6bf19e 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 ac999c624c..3be692ec33 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 5a4d80c4d6..8ec1824f3b 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);
@@ -2558,8 +2534,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 = "";
@@ -2609,11 +2585,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 = "";
@@ -2688,8 +2664,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 = "";
@@ -2808,8 +2784,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 ");
}