cmd: print extra-long option descriptions on the following line

This commit is contained in:
2024-10-27 14:24:23 +00:00
parent ef9cc13174
commit afed1b111d

View File

@@ -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) 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"); b_fprintf(OUTPUT_STREAM, "\n" F_YELLOW "OPTIONS:" F_RESET "\n");
size_t desb_margin = 0; 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); z__b_get_option_usage_string(opt, CMD_STR_COLOUR, str);
size_t len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4; size_t len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4;
if (len >= newline_threshold) {
continue;
}
if (len > desb_margin) { if (len > desb_margin) {
desb_margin = len; desb_margin = len;
} }
@@ -375,9 +388,26 @@ static void print_options_list(struct b_command *cmd)
b_string_clear(str); b_string_clear(str);
z__b_get_option_usage_string(opt, CMD_STR_COLOUR, 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); fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(str), 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; unsigned int len = b_string_get_size(str, B_STRLEN_IGNORE_ESC) + 4;
while (len < format.p_left_margin) { while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM); fputc(' ', OUTPUT_STREAM);
@@ -388,6 +418,10 @@ static void print_options_list(struct b_command *cmd)
z__b_get_option_description(opt, str); z__b_get_option_description(opt, str);
b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format); b_print_paragraph(b_string_ptr(str), OUTPUT_STREAM, &format);
if (opt_len >= newline_threshold) {
fputc('\n', OUTPUT_STREAM);
}
} }
b_string_release(str); b_string_release(str);