cmd: use term formatting codes for command help text

This commit is contained in:
2024-11-14 22:03:39 +00:00
parent b2c9c7cab9
commit 64a246e6f7
4 changed files with 38 additions and 24 deletions

View File

@@ -10,7 +10,7 @@
#define OUTPUT_STREAM stderr
static struct b_btree command_list = {};
static struct b_btree command_list = {0};
B_BTREE_DEFINE_SIMPLE_GET(struct b_command, unsigned int, b_node, b_id, get_command)
B_BTREE_DEFINE_SIMPLE_INSERT(struct b_command, b_node, b_id, put_command)
@@ -545,8 +545,9 @@ static void print_options_list(struct b_command *cmd)
z__b_get_option_description(opt, desc_str);
size_t opt_len
= b_string_get_size(opt_str, B_STRLEN_IGNORE_ESC) + 4;
size_t desc_len = b_string_get_size(desc_str, B_STRLEN_IGNORE_ESC);
= b_string_get_size(opt_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + 4;
size_t desc_len = b_string_get_size(
desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
if (description_on_separate_line(opt_len, desc_len)) {
continue;
@@ -557,7 +558,7 @@ static void print_options_list(struct b_command *cmd)
}
}
b_paragraph_format format = {};
b_paragraph_format format = {0};
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE;
format.p_left_margin = desb_margin + 4;
format.p_right_margin = 4;
@@ -575,9 +576,12 @@ static void print_options_list(struct b_command *cmd)
z__b_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__b_get_option_description(opt, desc_str);
size_t opt_len
= b_string_get_size(opt_str, B_STRLEN_IGNORE_ESC) + 4;
size_t desc_len = b_string_get_size(desc_str, B_STRLEN_IGNORE_ESC);
size_t opt_len = b_string_get_size(
opt_str, B_STRLEN_IGNORE_ESC
| B_STRLEN_IGNORE_MOD)
+ 4;
size_t desc_len = b_string_get_size(
desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
bool new_paragraph
= description_on_separate_line(opt_len, desc_len);
@@ -631,14 +635,16 @@ static void print_args_list(struct b_command *cmd)
b_string_clear(str);
z__b_get_arg_usage_string(arg, true, 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 | B_STRLEN_IGNORE_MOD)
+ 4;
if (len > desb_margin) {
desb_margin = len;
}
}
b_paragraph_format format = {};
b_paragraph_format format = {0};
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE;
format.p_left_margin = desb_margin + 4;
format.p_right_margin = 4;
@@ -653,7 +659,10 @@ static void print_args_list(struct b_command *cmd)
fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(str), OUTPUT_STREAM);
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 | B_STRLEN_IGNORE_MOD)
+ 4;
while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM);
len++;
@@ -682,14 +691,16 @@ static void print_commands_list(struct b_command *cmd)
b_string_clear(str);
get_command_string(sub, 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 | B_STRLEN_IGNORE_MOD)
+ 4;
if (len > desb_margin) {
desb_margin = len;
}
}
b_paragraph_format format = {};
b_paragraph_format format = {0};
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE;
format.p_left_margin = desb_margin + 4;
format.p_right_margin = 4;
@@ -704,7 +715,10 @@ static void print_commands_list(struct b_command *cmd)
fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(str), OUTPUT_STREAM);
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 | B_STRLEN_IGNORE_MOD)
+ 4;
while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM);
len++;
@@ -816,7 +830,7 @@ struct b_command_option *b_command_get_option_with_short_name(
static void print_usage(struct b_command *cmd)
{
b_paragraph_format format = {};
b_paragraph_format format = {0};
format.p_left_margin = format.p_right_margin = 4;
b_fprintf(OUTPUT_STREAM, F_YELLOW "USAGE:" F_RESET "\n");
@@ -847,7 +861,7 @@ static void print_usage(struct b_command *cmd)
static void print_help(struct b_command *cmd)
{
b_paragraph_format format = {};
b_paragraph_format format = {0};
if (!cmd->b_parent) {
b_fprintf(OUTPUT_STREAM, F_GREEN "%s" F_RESET "\n", cmd->b_name);