From afed1b111db13622dce86740453dbecc3bedf99d Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 27 Oct 2024 14:24:23 +0000 Subject: [PATCH] cmd: print extra-long option descriptions on the following line --- cmd/command.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/command.c b/cmd/command.c index 4ad6b70..d0c000b 100644 --- a/cmd/command.c +++ b/cmd/command.c @@ -338,6 +338,15 @@ static void get_command_description(struct b_command *cmd, b_string *out) static void print_options_list(struct b_command *cmd) { + unsigned int term_width = 0; + b_term_get_dimensions(OUTPUT_STREAM, &term_width, NULL); + + unsigned int newline_threshold = 1000000; + + if (term_width) { + newline_threshold = term_width - 12 - 25; + } + b_fprintf(OUTPUT_STREAM, "\n" F_YELLOW "OPTIONS:" F_RESET "\n"); size_t desb_margin = 0; @@ -355,6 +364,10 @@ static void print_options_list(struct b_command *cmd) z__b_get_option_usage_string(opt, CMD_STR_COLOUR, str); size_t len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4; + if (len >= newline_threshold) { + continue; + } + if (len > desb_margin) { desb_margin = len; } @@ -375,9 +388,26 @@ static void print_options_list(struct b_command *cmd) b_string_clear(str); z__b_get_option_usage_string(opt, CMD_STR_COLOUR, str); + size_t opt_len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4; + + if (opt_len >= newline_threshold) { + fputc('\n', OUTPUT_STREAM); + } fputs(" ", OUTPUT_STREAM); b_fputs(b_string_ptr(str), OUTPUT_STREAM); + + if (opt_len >= newline_threshold) { + format.p_flags = 0; + format.p_left_margin = 8; + format.p_right_margin = 4; + fputc('\n', OUTPUT_STREAM); + } else { + format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE; + format.p_left_margin = desb_margin + 4; + format.p_right_margin = 4; + } + unsigned int len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4; while (len < format.p_left_margin) { fputc(' ', OUTPUT_STREAM); @@ -388,6 +418,10 @@ static void print_options_list(struct b_command *cmd) z__b_get_option_description(opt, str); b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); + + if (opt_len >= newline_threshold) { + fputc('\n', OUTPUT_STREAM); + } } b_string_release(str);