diff --git a/src/core/manager.c b/src/core/manager.c index fcb7739b136..60a96611604 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -71,6 +71,7 @@ #include "path-lookup.h" #include "path-util.h" #include "plymouth-util.h" +#include "pretty-print.h" #include "process-util.h" #include "psi-util.h" #include "ratelimit.h" @@ -182,42 +183,6 @@ static void manager_watch_jobs_in_progress(Manager *m) { (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); } -#define CYLON_BUFFER_EXTRA (2*STRLEN(ANSI_RED) + STRLEN(ANSI_HIGHLIGHT_RED) + 2*STRLEN(ANSI_NORMAL)) - -static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { - char *p = buffer; - - assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); - assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ - - if (pos > 1) { - if (pos > 2) - p = mempset(p, ' ', pos-2); - if (log_get_show_color()) - p = stpcpy(p, ANSI_RED); - *p++ = '*'; - } - - if (pos > 0 && pos <= width) { - if (log_get_show_color()) - p = stpcpy(p, ANSI_HIGHLIGHT_RED); - *p++ = '*'; - } - - if (log_get_show_color()) - p = stpcpy(p, ANSI_NORMAL); - - if (pos < width) { - if (log_get_show_color()) - p = stpcpy(p, ANSI_RED); - *p++ = '*'; - if (pos < width-1) - p = mempset(p, ' ', width-1-pos); - if (log_get_show_color()) - strcpy(p, ANSI_NORMAL); - } -} - static void manager_flip_auto_status(Manager *m, bool enable, const char *reason) { assert(m); diff --git a/src/core/show-status.c b/src/core/show-status.c index d5eb7241596..1d47c0af4d2 100644 --- a/src/core/show-status.c +++ b/src/core/show-status.c @@ -98,7 +98,7 @@ int status_vprintf(const char *status, ShowStatusFlags flags, const char *format if (prev_ephemeral && !FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL)) iovec[n++] = IOVEC_MAKE_STRING(ANSI_ERASE_TO_END_OF_LINE); - prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL) ; + prev_ephemeral = FLAGS_SET(flags, SHOW_STATUS_EPHEMERAL); if (writev(fd, iovec, n) < 0) return -errno; diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c index e31edb88b02..2833063d4c1 100644 --- a/src/shared/pretty-print.c +++ b/src/shared/pretty-print.c @@ -17,6 +17,42 @@ #include "strv.h" #include "terminal-util.h" +void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { + char *p = buffer; + + assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); + assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ + + if (pos > 1) { + if (pos > 2) + p = mempset(p, ' ', pos-2); + if (log_get_show_color()) + p = stpcpy(p, ANSI_RED); + *p++ = '*'; + } + + if (pos > 0 && pos <= width) { + if (log_get_show_color()) + p = stpcpy(p, ANSI_HIGHLIGHT_RED); + *p++ = '*'; + } + + if (log_get_show_color()) + p = stpcpy(p, ANSI_NORMAL); + + if (pos < width) { + if (log_get_show_color()) + p = stpcpy(p, ANSI_RED); + *p++ = '*'; + if (pos < width-1) + p = mempset(p, ' ', width-1-pos); + if (log_get_show_color()) + p = stpcpy(p, ANSI_NORMAL); + } + + *p = '\0'; +} + bool urlify_enabled(void) { #if ENABLE_URLIFY static int cached_urlify_enabled = -1; diff --git a/src/shared/pretty-print.h b/src/shared/pretty-print.h index b25684aaa65..c17e976580d 100644 --- a/src/shared/pretty-print.h +++ b/src/shared/pretty-print.h @@ -4,6 +4,10 @@ #include "glyph-util.h" #include "terminal-util.h" +#define CYLON_BUFFER_EXTRA (2*STRLEN(ANSI_RED) + STRLEN(ANSI_HIGHLIGHT_RED) + 2*STRLEN(ANSI_NORMAL)) + +void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos); + void print_separator(void); int file_url_from_path(const char *path, char **ret); diff --git a/src/test/test-pretty-print.c b/src/test/test-pretty-print.c index ece1a161e45..52b2bc861e8 100644 --- a/src/test/test-pretty-print.c +++ b/src/test/test-pretty-print.c @@ -11,6 +11,34 @@ #include "strv.h" #include "tests.h" +#define CYLON_WIDTH 6 + +static void test_draw_cylon_one(unsigned pos) { + char buf[CYLON_WIDTH + CYLON_BUFFER_EXTRA + 1]; + + log_debug("/* %s(%u) */", __func__, pos); + + assert(pos <= CYLON_WIDTH + 1); + + memset(buf, 0xff, sizeof(buf)); + draw_cylon(buf, sizeof(buf), CYLON_WIDTH, pos); + assert_se(strlen(buf) < sizeof(buf)); +} + +TEST(draw_cylon) { + bool saved = log_get_show_color(); + + log_show_color(false); + for (unsigned i = 0; i <= CYLON_WIDTH + 1; i++) + test_draw_cylon_one(i); + + log_show_color(true); + for (unsigned i = 0; i <= CYLON_WIDTH + 1; i++) + test_draw_cylon_one(i); + + log_show_color(saved); +} + TEST(terminal_urlify) { _cleanup_free_ char *formatted = NULL;