From: Kevin Day Date: Fri, 24 Apr 2020 02:46:14 +0000 (-0500) Subject: Feature: add color print2 functions X-Git-Tag: 0.5.0~344 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=938a0f7ac7d4fd1c41d6dd1b28c967724d3ac227;p=fll Feature: add color print2 functions It is very common to have colors that are bolded. Adding a new function to handle to color codes so that bold can be used makes sense to me. I do not like using numbers in function names, but in this exceptional case, it makes sense. --- diff --git a/level_1/fl_color/c/color.c b/level_1/fl_color/c/color.c index e71d91a..0fbc9ae 100644 --- a/level_1/fl_color/c/color.c +++ b/level_1/fl_color/c/color.c @@ -144,6 +144,41 @@ extern "C" { } #endif // _di_fl_color_print_ +#ifndef _di_fl_color_print2_ + f_return_status fl_color_print2(FILE *file, const f_string_dynamic start_color, const f_string_dynamic extra_color, const f_string_dynamic end_color, const int8_t *string, ...) { + #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); + if (string == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_1_parameter_checking_ + + if (start_color.used != 0) { + f_status status = f_print_string_dynamic(file, start_color); + + if (f_status_is_error(status)) return status; + + status = f_print_string_dynamic(file, extra_color); + + if (f_status_is_error(status)) return status; + } + + va_list ap; + + va_start(ap, string); + + vfprintf(file, string, ap); + + va_end(ap); + + if (end_color.used != 0) { + f_status status = f_print_string_dynamic(file, end_color); + + if (f_status_is_error(status)) return status; + } + + return f_none; + } +#endif // _di_fl_color_print2_ + #ifndef _di_fl_color_print_line_ f_return_status fl_color_print_line(FILE *file, const f_string_dynamic start_color, const f_string_dynamic end_color, const int8_t *string, ...) { #ifndef _di_level_1_parameter_checking_ @@ -178,6 +213,44 @@ extern "C" { } #endif // _di_fl_color_print_line_ +#ifndef _di_fl_color_print2_line_ + f_return_status fl_color_print2_line(FILE *file, const f_string_dynamic start_color, const f_string_dynamic extra_color, const f_string_dynamic end_color, const int8_t *string, ...) { + #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); + if (string == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_1_parameter_checking_ + + if (start_color.used != 0) { + f_status status = f_print_string_dynamic(file, start_color); + + if (f_status_is_error(status)) return status; + + status = f_print_string_dynamic(file, extra_color); + + if (f_status_is_error(status)) return status; + } + + va_list ap; + + va_start(ap, string); + + vfprintf(file, string, ap); + + va_end(ap); + + if (end_color.used != 0) { + f_status status = f_print_string_dynamic(file, end_color); + + if (f_status_is_error(status)) return status; + } + + // now print the trailing newline, this is done _after_ ending the colors to avoid color wrapping issues that can happen when a color code follows a newline + fprintf(file, "%c", f_string_eol); + + return f_none; + } +#endif // _di_fl_color_print2_line_ + #ifndef _di_fl_color_print_code_ f_return_status fl_color_print_code(FILE *file, const f_string_dynamic color) { if (color.used != 0) { diff --git a/level_1/fl_color/c/color.h b/level_1/fl_color/c/color.h index 51a6f3d..5640972 100644 --- a/level_1/fl_color/c/color.h +++ b/level_1/fl_color/c/color.h @@ -219,7 +219,39 @@ extern "C" { #endif // _di_fl_color_print_ /** - * Print a string, wrapped in a given start and stop color, and then print an EOL character. + * Print a string, wrapped in a given start, extra, and stop color. + * + * If the colors strings have nothing used in them, then this will only print the string. + * + * It is common for colors to be bolded. + * This is intended to simplify printing bold colors. + * + * @param file + * The file or standard io. + * @param format + * The color format parts. + * @param start_color + * The color code to place at the beginning of the string, set to 0 to disable. + * @param extra_color + * The color code to place immediately following the start_color, set to 0 to disable. + * @param stop_color + * The color code to place at the end of the string, set to 0 to disable. + * @param string + * The string to print to the file or standard io. + * @param ... + * Variable arguments, processed in the same way fprintf() processes them. + * + * @return + * f_none on success. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_output (with error bit) on output error. + */ +#ifndef _di_fl_color_print2_ + extern f_return_status fl_color_print2(FILE *file, const f_string_dynamic start_color, const f_string_dynamic extra_color, const f_string_dynamic end_color, const int8_t *string, ...); +#endif // _di_fl_color_print2_ + +/** + * Print a string, wrapped in a given start and stop color, then print an EOL character. * * If the colors strings have nothing used in them, then this will only print the string. * @@ -246,6 +278,38 @@ extern "C" { #endif // _di_fl_color_print_line_ /** + * Print a string, wrapped in a given start, extra, and stop color, then print an EOL character. + * + * If the colors strings have nothing used in them, then this will only print the string. + * + * It is common for colors to be bolded. + * This is intended to simplify printing bold colors. + * + * @param file + * The file or standard io. + * @param format + * The color format parts. + * @param start_color + * The color code to place at the beginning of the string, set to 0 to disable. + * @param extra_color + * The color code to place immediately following the start_color, set to 0 to disable. + * @param stop_color + * The color code to place at the end of the string, set to 0 to disable. + * @param string + * The string to print to the file or standard io. + * @param ... + * Variable arguments, processed in the same way fprintf() processes them. + * + * @return + * f_none on success. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_output (with error bit) on output error. + */ +#ifndef _di_fl_color_print2_line_ + extern f_return_status fl_color_print2_line(FILE *file, const f_string_dynamic start_color, const f_string_dynamic extra_color, const f_string_dynamic end_color, const int8_t *string, ...); +#endif // _di_fl_color_print2_line_ + +/** * Print a single color code to the given file or standard io. * * Try not to forget to print the color reset when done. @@ -264,7 +328,6 @@ extern "C" { extern f_return_status fl_color_print_code(FILE *file, const f_string_dynamic color); #endif // _di_fl_color_print_code_ - /** * Load the appropriate colors into the color context. *