-h: Refactor out colored help printing code (#5421)

This commit is contained in:
Khairul Azhar Kasmiran 2025-10-01 06:02:15 +08:00 committed by GitHub
parent 3a4134280f
commit ff79a14c04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 36 additions and 98 deletions

View file

@ -88,12 +88,7 @@ static int help(bool verbose) {
"-x", "[num]", "Number of expected failed tests",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
printf("Supported test types: @json @unit @fuzz @cmds\n"
"OS/Arch for archos tests: " RZ_TEST_ARCH_OS "\n");
}

View file

@ -234,12 +234,7 @@ RZ_API RZ_OWN char *rz_print_json_indent(RZ_NULLABLE const char *s, bool color,
RZ_API char *rz_print_json_human(const char *s);
RZ_API RZ_OWN RzStrBuf *rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const RzAsmTokenString *toks);
RZ_API size_t rz_print_options_get_max_len(const char **options, size_t options_len,
RZ_NULLABLE size_t *maxDescLength);
RZ_API void rz_print_colored_help_option(const char *flag, const char *arg, const char *desc,
size_t maxFlagAndArgLength);
RZ_API void rz_print_colored_help_option_example(RZ_NULLABLE const char *flag, RZ_NULLABLE const char *arg,
const char *desc, size_t maxFlagAndArgLength, RZ_NULLABLE const char *example, size_t maxDescLength);
RZ_API void rz_print_colored_help(const char **options, size_t options_len, bool have_examples);
#endif
#ifdef __cplusplus

View file

@ -139,12 +139,7 @@ static int main_help(RZ_BORROW RZ_NONNULL RzCore *core, int line) {
"-z, -zz", "", "Do not load strings or load them even in raw",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
if (line == 2) {
char *datahome = rz_path_home_prefix(RZ_DATADIR);

View file

@ -207,12 +207,7 @@ static int rasm_show_help(int v) {
// clang-format on
};
if (v != 1) {
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
printf(" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n"

View file

@ -248,14 +248,7 @@ static int help(void) {
#undef CF
#undef CA
#undef CR
size_t maxDescLength = SIZE_MAX;
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), &maxDescLength);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 4) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option_example(options[i], options[i + 1], options[i + 2],
maxFlagAndArgLength, options[i + 3], maxDescLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), true);
return true;
}

View file

@ -218,12 +218,7 @@ static int rzbin_show_help(int v) {
"-Z", "", "Guess size of binary program",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
if (v) {
printf("Environment:\n"

View file

@ -243,13 +243,7 @@ static void rz_diff_show_help(bool usage_only) {
"", "", " symbols | compare symbols found in the files",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
printf(
"palette colors can be changed by adding the following lines\n"
"inside the $HOME/.rizinrc file\n"

View file

@ -223,12 +223,7 @@ static int show_help(const char *argv0, int line) {
"-Z", "", "Show string found on each search hit",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
return 0;
}

View file

@ -50,12 +50,7 @@ static int usage(int v) {
"-z", "" ,"Output in C string syntax",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
return 1;
}

View file

@ -109,12 +109,7 @@ static void rz_hash_show_help(bool usage_only) {
"", "", "If 's:' prefix is specified",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
static void rz_hash_show_algorithms(RzHashContext *ctx) {

View file

@ -37,12 +37,7 @@ static void rz_run_help(int v) {
"--", "[program] [args]", "Run commands",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
}
if (v == 1) {
// clang-format off

View file

@ -26,12 +26,7 @@ static void rz_sign_show_help(void) {
"-v", "", "Show version information",
// clang-format on
};
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
if (i + 1 < RZ_ARRAY_SIZE(options)) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);
}
}
rz_print_colored_help(options, RZ_ARRAY_SIZE(options), false);
printf("Examples:\n"
" rz-sign -d signature.sig\n"
" rz-sign -c new_signature.pat old_signature.sig\n"

View file

@ -1465,9 +1465,7 @@ RZ_API RZ_OWN RzStrBuf *rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const Rz
* assume that there are 4 strings per option.
* \return The maximum flag+arg length in \p options.
*/
RZ_API size_t rz_print_options_get_max_len(const char **options, size_t options_len,
RZ_NULLABLE size_t *maxDescLength) {
static size_t options_get_max_len(const char **options, size_t options_len, RZ_NULLABLE size_t *maxDescLength) {
int items_per_opt = maxDescLength ? 4 : 3;
size_t maxFlagAndArgLength = 0;
if (maxDescLength) {
@ -1490,22 +1488,6 @@ RZ_API size_t rz_print_options_get_max_len(const char **options, size_t options_
return maxFlagAndArgLength;
}
/**
* \brief Prints a help option with the option flag and arg strings colorized, and description aligned to a column.
*
* \param flag Option flag, will be colored green.
* \param arg Option arg, will be colored yellow.
* \param desc Option description, will be aligned in the third column.
* \param maxFlagAndArgLength Width of column containing \p flag and \p arg. It needs to be calculated beforehand
* as the max over all options, perhaps via rz_print_options_get_max_len().
*/
RZ_API void rz_print_colored_help_option(const char *flag, const char *arg, const char *desc,
size_t maxFlagAndArgLength) {
rz_return_if_fail(flag && arg && desc);
rz_print_colored_help_option_example(flag, arg, desc, maxFlagAndArgLength, NULL, 0);
}
/**
* \brief Prints a help option with example, with the option flag and arg strings colorized, and both description and
* example aligned to columns.
@ -1515,13 +1497,13 @@ RZ_API void rz_print_colored_help_option(const char *flag, const char *arg, cons
* \param desc Option description, will be aligned in the third column unless both \p flag and \p arg are NULL. If so,
* it will be aligned at the first column.
* \param maxFlagAndArgLength Width of column containing \p flag and \p arg. It needs to be calculated beforehand
* as the max over all options, perhaps via rz_print_options_get_max_len().
* as the max over all options, perhaps via options_get_max_len().
* \param example Optional option example, will be aligned in a column after the description and prefixed with "; ".
* \param maxDescLength Width of column containing \p desc. It needs to be calculated beforehand as the max over all
* descriptions, perhaps via rz_print_options_get_max_len().
* descriptions, perhaps via options_get_max_len().
*/
RZ_API void rz_print_colored_help_option_example(RZ_NULLABLE const char *flag, RZ_NULLABLE const char *arg,
const char *desc, size_t maxFlagAndArgLength, RZ_NULLABLE const char *example, size_t maxDescLength) {
static void print_colored_help_option(RZ_NULLABLE const char *flag, RZ_NULLABLE const char *arg, const char *desc,
size_t maxFlagAndArgLength, RZ_NULLABLE const char *example, size_t maxDescLength) {
rz_return_if_fail(desc);
bool desc_start = !flag && !arg;
@ -1553,3 +1535,22 @@ RZ_API void rz_print_colored_help_option_example(RZ_NULLABLE const char *flag, R
}
printf("\n");
}
/**
* \brief Print colored help from \p options.
*
* \param options Options string array, with 3 (default) or 4 strings per option depending on \p have_examples.
* \param options_len Length of \p options in strings, should be RZ_ARRAY_SIZE(options).
* \param have_examples If false, there are 3 strings per option: flag, argument and description. If true, there is a
* 4th string for each option that contains an example.
*/
RZ_API void rz_print_colored_help(const char **options, size_t options_len, bool have_examples) {
size_t maxDescLength = 0;
size_t maxFlagAndArgLength = options_get_max_len(options, options_len, have_examples ? &maxDescLength : NULL);
for (int i = 0; i < options_len; i += have_examples ? 4 : 3) {
if (i + 1 < options_len) {
print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength,
have_examples ? options[i + 3] : NULL, maxDescLength);
}
}
}