From: Daan De Meyer Date: Tue, 17 Sep 2024 10:06:49 +0000 (+0200) Subject: Move show_menu() to terminal-util.h X-Git-Tag: v257-rc1~337^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ec75a254d29aa0beec6ab8c3cf7745bc38ea8ded;p=thirdparty%2Fsystemd.git Move show_menu() to terminal-util.h --- diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index cff1d50e42a..d5593bd01d4 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -29,6 +29,7 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "glyph-util.h" #include "hexdecoct.h" #include "inotify-util.h" #include "io-util.h" @@ -274,6 +275,52 @@ bool any_key_to_proceed(void) { return key != 'q'; } +int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { + unsigned break_lines, break_modulo; + size_t n, per_column, i, j; + + assert(n_columns > 0); + + n = strv_length(x); + per_column = DIV_ROUND_UP(n, n_columns); + + break_lines = lines(); + if (break_lines > 2) + break_lines--; + + /* The first page gets two extra lines, since we want to show + * a title */ + break_modulo = break_lines; + if (break_modulo > 3) + break_modulo -= 3; + + for (i = 0; i < per_column; i++) { + + for (j = 0; j < n_columns; j++) { + _cleanup_free_ char *e = NULL; + + if (j * per_column + i >= n) + break; + + e = ellipsize(x[j * per_column + i], width, percentage); + if (!e) + return log_oom(); + + printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e); + } + + putchar('\n'); + + /* on the first screen we reserve 2 extra lines for the title */ + if (i % break_lines == break_modulo) { + if (!any_key_to_proceed()) + return 0; + } + } + + return 0; +} + int open_terminal(const char *name, int mode) { _cleanup_close_ int fd = -EBADF; unsigned c = 0; diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 180ca24ef40..b5c45c464bc 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -79,6 +79,7 @@ int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl); int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4); int ask_string(char **ret, const char *text, ...) _printf_(2, 3); bool any_key_to_proceed(void); +int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage); int vt_disallocate(const char *name); diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 236c64634c9..3bf11e7f214 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -132,52 +132,6 @@ static void print_welcome(int rfd) { done = true; } -static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { - unsigned break_lines, break_modulo; - size_t n, per_column, i, j; - - assert(n_columns > 0); - - n = strv_length(x); - per_column = DIV_ROUND_UP(n, n_columns); - - break_lines = lines(); - if (break_lines > 2) - break_lines--; - - /* The first page gets two extra lines, since we want to show - * a title */ - break_modulo = break_lines; - if (break_modulo > 3) - break_modulo -= 3; - - for (i = 0; i < per_column; i++) { - - for (j = 0; j < n_columns; j++) { - _cleanup_free_ char *e = NULL; - - if (j * per_column + i >= n) - break; - - e = ellipsize(x[j * per_column + i], width, percentage); - if (!e) - return log_oom(); - - printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e); - } - - putchar('\n'); - - /* on the first screen we reserve 2 extra lines for the title */ - if (i % break_lines == break_modulo) { - if (!any_key_to_proceed()) - return 0; - } - } - - return 0; -} - static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) { int r;