1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-08 11:27:32 +03:00

util: rework ANSI escape code macros

This commit is contained in:
Lennart Poettering 2012-01-13 21:56:09 +01:00
parent 9ddc4a26e5
commit c1072ea0da
6 changed files with 27 additions and 26 deletions

View File

@ -89,10 +89,10 @@ int ask_password_tty(
goto finish; goto finish;
} }
loop_write(ttyfd, "\x1B[1m", 4, false); loop_write(ttyfd, ANSI_HIGHLIGHT_ON, sizeof(ANSI_HIGHLIGHT_ON)-1, false);
loop_write(ttyfd, message, strlen(message), false); loop_write(ttyfd, message, strlen(message), false);
loop_write(ttyfd, " ", 1, false); loop_write(ttyfd, " ", 1, false);
loop_write(ttyfd, "\x1B[0m", 4, false); loop_write(ttyfd, ANSI_HIGHLIGHT_OFF, sizeof(ANSI_HIGHLIGHT_OFF)-1, false);
new_termios = old_termios; new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON|ECHO); new_termios.c_lflag &= ~(ICANON|ECHO);

View File

@ -488,16 +488,16 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) {
break; break;
case JOB_FAILED: case JOB_FAILED:
unit_status_printf(u, ANSI_HIGHLIGHT_ON "FAILED" ANSI_HIGHLIGHT_OFF, "Failed to start %s", unit_description(u)); unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON "FAILED" ANSI_HIGHLIGHT_OFF, "Failed to start %s", unit_description(u));
unit_status_printf(u, NULL, "See 'systemctl status %s' for details.", u->meta.id); unit_status_printf(u, NULL, "See 'systemctl status %s' for details.", u->meta.id);
break; break;
case JOB_DEPENDENCY: case JOB_DEPENDENCY:
unit_status_printf(u, ANSI_HIGHLIGHT_ON " ABORT" ANSI_HIGHLIGHT_OFF, "Dependency failed. Aborted start of %s", unit_description(u)); unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " ABORT" ANSI_HIGHLIGHT_OFF, "Dependency failed. Aborted start of %s", unit_description(u));
break; break;
case JOB_TIMEOUT: case JOB_TIMEOUT:
unit_status_printf(u, ANSI_HIGHLIGHT_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out starting %s", unit_description(u)); unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out starting %s", unit_description(u));
break; break;
default: default:
@ -509,7 +509,7 @@ static void job_print_status_message(Unit *u, JobType t, JobResult result) {
switch (result) { switch (result) {
case JOB_TIMEOUT: case JOB_TIMEOUT:
unit_status_printf(u, ANSI_HIGHLIGHT_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out stopping %s", unit_description(u)); unit_status_printf(u, ANSI_HIGHLIGHT_RED_ON " TIME " ANSI_HIGHLIGHT_OFF, "Timed out stopping %s", unit_description(u));
break; break;
case JOB_DONE: case JOB_DONE:

View File

@ -344,7 +344,7 @@ static int write_to_console(
} }
if (highlight) if (highlight)
IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON); IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
IOVEC_SET_STRING(iovec[n++], buffer); IOVEC_SET_STRING(iovec[n++], buffer);
if (highlight) if (highlight)
IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF); IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);

View File

@ -166,12 +166,12 @@ static void agent_open_if_enabled(void) {
agent_open(); agent_open();
} }
static const char *ansi_highlight(bool b) { static const char *ansi_highlight_red(bool b) {
if (!on_tty()) if (!on_tty())
return ""; return "";
return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF; return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
} }
static const char *ansi_highlight_green(bool b) { static const char *ansi_highlight_green(bool b) {
@ -383,14 +383,14 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
n_shown++; n_shown++;
if (streq(u->load_state, "error")) { if (streq(u->load_state, "error")) {
on_loaded = ansi_highlight(true); on_loaded = ansi_highlight_red(true);
off_loaded = ansi_highlight(false); off_loaded = ansi_highlight_red(false);
} else } else
on_loaded = off_loaded = ""; on_loaded = off_loaded = "";
if (streq(u->active_state, "failed")) { if (streq(u->active_state, "failed")) {
on_active = ansi_highlight(true); on_active = ansi_highlight_red(true);
off_active = ansi_highlight(false); off_active = ansi_highlight_red(false);
} else } else
on_active = off_active = ""; on_active = off_active = "";
@ -593,8 +593,8 @@ static void output_unit_file_list(const UnitFileList *units, unsigned c) {
if (u->state == UNIT_FILE_MASKED || if (u->state == UNIT_FILE_MASKED ||
u->state == UNIT_FILE_MASKED_RUNTIME || u->state == UNIT_FILE_MASKED_RUNTIME ||
u->state == UNIT_FILE_DISABLED) { u->state == UNIT_FILE_DISABLED) {
on = ansi_highlight(true); on = ansi_highlight_red(true);
off = ansi_highlight(false); off = ansi_highlight_red(false);
} else if (u->state == UNIT_FILE_ENABLED) { } else if (u->state == UNIT_FILE_ENABLED) {
on = ansi_highlight_green(true); on = ansi_highlight_green(true);
off = ansi_highlight_green(false); off = ansi_highlight_green(false);
@ -2069,8 +2069,8 @@ static void print_status_info(UnitStatusInfo *i) {
printf("\t Follow: unit currently follows state of %s\n", i->following); printf("\t Follow: unit currently follows state of %s\n", i->following);
if (streq_ptr(i->load_state, "error")) { if (streq_ptr(i->load_state, "error")) {
on = ansi_highlight(true); on = ansi_highlight_red(true);
off = ansi_highlight(false); off = ansi_highlight_red(false);
} else } else
on = off = ""; on = off = "";
@ -2086,8 +2086,8 @@ static void print_status_info(UnitStatusInfo *i) {
ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
if (streq_ptr(i->active_state, "failed")) { if (streq_ptr(i->active_state, "failed")) {
on = ansi_highlight(true); on = ansi_highlight_red(true);
off = ansi_highlight(false); off = ansi_highlight_red(false);
} else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) { } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
on = ansi_highlight_green(true); on = ansi_highlight_green(true);
off = ansi_highlight_green(false); off = ansi_highlight_green(false);
@ -2163,8 +2163,8 @@ static void print_status_info(UnitStatusInfo *i) {
good = is_clean_exit(p->code, p->status); good = is_clean_exit(p->code, p->status);
if (!good) { if (!good) {
on = ansi_highlight(true); on = ansi_highlight_red(true);
off = ansi_highlight(false); off = ansi_highlight_red(false);
} else } else
on = off = ""; on = off = "";
@ -2274,8 +2274,8 @@ static void print_status_info(UnitStatusInfo *i) {
if (i->need_daemon_reload) if (i->need_daemon_reload)
printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n", printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
ansi_highlight(true), ansi_highlight_red(true),
ansi_highlight(false), ansi_highlight_red(false),
arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user"); arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
} }

View File

@ -2467,14 +2467,14 @@ int ask(char *ret, const char *replies, const char *text, ...) {
bool need_nl = true; bool need_nl = true;
if (on_tty) if (on_tty)
fputs("\x1B[1m", stdout); fputs(ANSI_HIGHLIGHT_ON, stdout);
va_start(ap, text); va_start(ap, text);
vprintf(text, ap); vprintf(text, ap);
va_end(ap); va_end(ap);
if (on_tty) if (on_tty)
fputs("\x1B[0m", stdout); fputs(ANSI_HIGHLIGHT_OFF, stdout);
fflush(stdout); fflush(stdout);

View File

@ -68,7 +68,8 @@ typedef struct dual_timestamp {
#define FORMAT_TIMESTAMP_PRETTY_MAX 256 #define FORMAT_TIMESTAMP_PRETTY_MAX 256
#define FORMAT_TIMESPAN_MAX 64 #define FORMAT_TIMESPAN_MAX 64
#define ANSI_HIGHLIGHT_ON "\x1B[1;31m" #define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
#define ANSI_HIGHLIGHT_RED_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m" #define ANSI_HIGHLIGHT_GREEN_ON "\x1B[1;32m"
#define ANSI_HIGHLIGHT_OFF "\x1B[0m" #define ANSI_HIGHLIGHT_OFF "\x1B[0m"