diff --git a/src/core/main.c b/src/core/main.c index 34794ccdd1..6d03b06684 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1371,12 +1371,12 @@ static int status_welcome(void) { "Failed to read os-release file, ignoring: %m"); if (log_get_show_color()) - return status_printf(NULL, false, false, + return status_printf(NULL, 0, "\nWelcome to \x1B[%sm%s\x1B[0m!\n", isempty(ansi_color) ? "1" : ansi_color, isempty(pretty_name) ? "Linux" : pretty_name); else - return status_printf(NULL, false, false, + return status_printf(NULL, 0, "\nWelcome to %s!\n", isempty(pretty_name) ? "Linux" : pretty_name); } diff --git a/src/core/manager.c b/src/core/manager.c index e2de1c84b9..5f11515b3d 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -4156,7 +4156,7 @@ void manager_status_printf(Manager *m, StatusType type, const char *status, cons return; va_start(ap, format); - status_vprintf(status, true, type == STATUS_TYPE_EPHEMERAL, format, ap); + status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap); va_end(ap); } diff --git a/src/core/show-status.c b/src/core/show-status.c index d8d2317d38..f748a82084 100644 --- a/src/core/show-status.c +++ b/src/core/show-status.c @@ -32,7 +32,7 @@ int parse_show_status(const char *v, ShowStatus *ret) { return 0; } -int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) { +int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) { static const char status_indent[] = " "; /* "[" STATUS "] " */ _cleanup_free_ char *s = NULL; _cleanup_close_ int fd = -1; @@ -57,7 +57,7 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char if (fd < 0) return fd; - if (ellipse) { + if (FLAGS_SET(flags, SHOW_STATUS_ELLIPSIZE)) { char *e; size_t emax, sl; int c; @@ -73,10 +73,8 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char emax = 3; e = ellipsize(s, emax, 50); - if (e) { - free(s); - s = e; - } + if (e) + free_and_replace(s, e); } if (prev_ephemeral) @@ -94,9 +92,9 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char iovec[n++] = IOVEC_MAKE_STRING(s); iovec[n++] = IOVEC_MAKE_STRING("\n"); - if (prev_ephemeral && !ephemeral) + if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL)) iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE); - prev_ephemeral = ephemeral; + prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ; if (writev(fd, iovec, n) < 0) return -errno; @@ -104,14 +102,14 @@ int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char return 0; } -int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) { +int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) { va_list ap; int r; assert(format); va_start(ap, format); - r = status_vprintf(status, ellipse, ephemeral, format, ap); + r = status_vprintf(status, flags, format, ap); va_end(ap); return r; diff --git a/src/core/show-status.h b/src/core/show-status.h index ac590303c1..f574d92d84 100644 --- a/src/core/show-status.h +++ b/src/core/show-status.h @@ -16,9 +16,14 @@ typedef enum ShowStatus { _SHOW_STATUS_INVALID = -1, } ShowStatus; +typedef enum ShowStatusFlags { + SHOW_STATUS_ELLIPSIZE = 1 << 0, + SHOW_STATUS_EPHEMERAL = 1 << 1, +} ShowStatusFlags; + ShowStatus show_status_from_string(const char *v) _const_; const char* show_status_to_string(ShowStatus s) _pure_; int parse_show_status(const char *v, ShowStatus *ret); -int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0); -int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5); +int status_vprintf(const char *status, ShowStatusFlags flags, const char *format, va_list ap) _printf_(3,0); +int status_printf(const char *status, ShowStatusFlags flags, const char *format, ...) _printf_(3,4); diff --git a/src/core/unit.c b/src/core/unit.c index df340548bb..73a625e7a1 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -46,6 +46,7 @@ #include "string-table.h" #include "string-util.h" #include "strv.h" +#include "terminal-util.h" #include "umask-util.h" #include "unit-name.h" #include "unit.h" @@ -1644,12 +1645,17 @@ static bool unit_assert_test(Unit *u) { } void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) { + const char *d; + + d = unit_description(u); + if (log_get_show_color()) + d = strjoina(ANSI_HIGHLIGHT, d, ANSI_NORMAL); + DISABLE_WARNING_FORMAT_NONLITERAL; - manager_status_printf(u->manager, STATUS_TYPE_NORMAL, status, unit_status_msg_format, unit_description(u)); + manager_status_printf(u->manager, STATUS_TYPE_NORMAL, status, unit_status_msg_format, d); REENABLE_WARNING; } - int unit_start_limit_test(Unit *u) { const char *reason;