From 8240144580c863a84a7427a146bfa59cc492c5ce Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 5 Apr 2022 19:55:40 +0200 Subject: [PATCH] console: argtable3: re-introduce arg_print_formatted function --- components/console/argtable3/argtable3.c | 65 +++++++++++++++--------- components/console/argtable3/argtable3.h | 1 + 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/components/console/argtable3/argtable3.c b/components/console/argtable3/argtable3.c index d3d61e5658..9e34c2bb5a 100644 --- a/components/console/argtable3/argtable3.c +++ b/components/console/argtable3/argtable3.c @@ -869,31 +869,7 @@ void arg_print_glossary(FILE* fp, void** argtable, const char* format) { * to right margin. The function does not indent the first line, but * only the following ones. * - * Example: - * arg_print_formatted( fp, 0, 5, "Some text that doesn't fit." ) - * will result in the following output: - * - * Some - * text - * that - * doesn' - * t fit. - * - * Too long lines will be wrapped in the middle of a word. - * - * arg_print_formatted( fp, 2, 7, "Some text that doesn't fit." ) - * will result in the following output: - * - * Some - * text - * that - * doesn' - * t fit. - * - * As you see, the first line is not indented. This enables output of - * lines, which start in a line where output already happened. - * - * Author: Uli Fouquet + * See description of arg_print_formatted below. */ static void arg_print_formatted_ds(arg_dstr_t ds, const unsigned lmargin, const unsigned rmargin, const char* text) { const unsigned int textlen = (unsigned int)strlen(text); @@ -954,6 +930,45 @@ static void arg_print_formatted_ds(arg_dstr_t ds, const unsigned lmargin, const } /* lines of text */ } +/** + * Print a piece of text formatted, which means in a column with a + * left and a right margin. The lines are wrapped at whitspaces next + * to right margin. The function does not indent the first line, but + * only the following ones. + * + * Example: + * arg_print_formatted( fp, 0, 5, "Some text that doesn't fit." ) + * will result in the following output: + * + * Some + * text + * that + * doesn' + * t fit. + * + * Too long lines will be wrapped in the middle of a word. + * + * arg_print_formatted( fp, 2, 7, "Some text that doesn't fit." ) + * will result in the following output: + * + * Some + * text + * that + * doesn' + * t fit. + * + * As you see, the first line is not indented. This enables output of + * lines, which start in a line where output already happened. + * + * Author: Uli Fouquet + */ +void arg_print_formatted(FILE* fp, const unsigned lmargin, const unsigned rmargin, const char* text) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_formatted_ds(ds, lmargin, rmargin, text); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + /** * Prints the glossary in strict GNU format. * Differences to arg_print_glossary() are: diff --git a/components/console/argtable3/argtable3.h b/components/console/argtable3/argtable3.h index 896f99a1c3..cbf15ec8f4 100644 --- a/components/console/argtable3/argtable3.h +++ b/components/console/argtable3/argtable3.h @@ -229,6 +229,7 @@ ARG_EXTERN void arg_print_syntax(FILE* fp, void** argtable, const char* suffix); ARG_EXTERN void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix); ARG_EXTERN void arg_print_glossary(FILE* fp, void** argtable, const char* format); ARG_EXTERN void arg_print_glossary_gnu(FILE* fp, void** argtable); +ARG_EXTERN void arg_print_formatted(FILE *fp, const unsigned lmargin, const unsigned rmargin, const char *text); ARG_EXTERN void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname); ARG_EXTERN void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix); ARG_EXTERN void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix);