diff --git a/lib/fort.c b/lib/fort.c index e9f7e19..0f09422 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -171,7 +171,9 @@ size_t number_of_columns_in_format_wstring(const wchar_t *fmt); /*int snprint_n_chars(char *buf, size_t length, size_t n, char ch);*/ /*int wsnprint_n_chars(wchar_t *buf, size_t length, size_t n, wchar_t ch);*/ int snprint_n_strings(char *buf, size_t length, size_t n, const char *str); +#if defined(FT_HAVE_WCHAR) int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str); +#endif #define CHCK_RSLT_ADD_TO_WRITTEN(statement) \ @@ -382,13 +384,19 @@ int buffer_wprintf(string_buffer_t *buffer, size_t buffer_row, wchar_t *buf, siz #define TEXT_STYLE_TAG_MAX_SIZE 64 +FT_INTERNAL void get_style_tag_for_cell(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); + +FT_INTERNAL void get_reset_style_tag_for_cell(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); +FT_INTERNAL void get_style_tag_for_content(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); + +FT_INTERNAL void get_reset_style_tag_for_content(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); @@ -873,6 +881,7 @@ const char *text_styles[] = { "", "\033[1m", "\033[2m", + "\033[3m", "\033[4m", "\033[5m", "\033[7m", @@ -883,6 +892,7 @@ const char *reset_text_styles[] = { "", "\033[21m", "\033[22m", + "\033[23m", "\033[24m", "\033[25m", "\033[27m", @@ -4126,12 +4136,15 @@ int snprint_n_strings(char *buf, size_t length, size_t n, const char *str) // return (int)n; //} +#if defined(FT_HAVE_WCHAR) +#define WCS_SIZE 64 + int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) { size_t str_len = strlen(str); - /* This function doesn't work properly with multibyte characters - * so it is better return an error in this case + /* note: baybe it's, better to return -1 in case of multibyte character strings + * (not sure this case is done correctly). */ if (str_len > 1) { const unsigned char *p = (const unsigned char *)str; @@ -4139,17 +4152,16 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) if (*p <= 127) p++; else { - const int SIZE = 64; - wchar_t wcs[SIZE]; + wchar_t wcs[WCS_SIZE]; const char *ptr = str; - int length; - length = mbsrtowcs(wcs, (const char **)&ptr, SIZE, NULL); - wcs[length] = L'\0'; - if (length > 1) { + size_t length; + length = mbsrtowcs(wcs, (const char **)&ptr, WCS_SIZE, NULL); + /* for simplicity */ + if ((length == (size_t) - 1) || length > 1) { return -1; } else { - swprintf(buf, length, L"%0*d", (int)(n * str_len), 0); - int k = n; + wcs[length] = L'\0'; + size_t k = n; while (k) { *buf = *wcs; ++buf; @@ -4158,8 +4170,6 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) buf[n] = L'\0'; return n; } - -// return -1; } } } @@ -4189,7 +4199,7 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) } return (int)(n * str_len); } - +#endif /******************************************************** End of file "fort_utils.c" diff --git a/lib/fort.h b/lib/fort.h index b83011f..3f787b6 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -721,10 +721,11 @@ int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style); #define FT_TSTYLE_DEFAULT 0 #define FT_TSTYLE_BOLD 1 #define FT_TSTYLE_DIM 2 -#define FT_TSTYLE_UNDERLINED 3 -#define FT_TSTYLE_BLINK 4 -#define FT_TSTYLE_INVERTED 5 -#define FT_TSTYLE_HIDDEN 6 +#define FT_TSTYLE_ITALIC 3 +#define FT_TSTYLE_UNDERLINED 4 +#define FT_TSTYLE_BLINK 5 +#define FT_TSTYLE_INVERTED 6 +#define FT_TSTYLE_HIDDEN 7 /** @} */ diff --git a/src/fort_utils.c b/src/fort_utils.c index 193a634..d436b89 100644 --- a/src/fort_utils.c +++ b/src/fort_utils.c @@ -236,12 +236,15 @@ int snprint_n_strings(char *buf, size_t length, size_t n, const char *str) // return (int)n; //} +#if defined(FT_HAVE_WCHAR) +#define WCS_SIZE 64 + int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) { size_t str_len = strlen(str); - /* This function doesn't work properly with multibyte characters - * so it is better return an error in this case + /* note: baybe it's, better to return -1 in case of multibyte character strings + * (not sure this case is done correctly). */ if (str_len > 1) { const unsigned char *p = (const unsigned char *)str; @@ -249,17 +252,16 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) if (*p <= 127) p++; else { - const int SIZE = 64; - wchar_t wcs[SIZE]; + wchar_t wcs[WCS_SIZE]; const char *ptr = str; - int length; - length = mbsrtowcs(wcs, (const char **)&ptr, SIZE, NULL); - wcs[length] = L'\0'; - if (length > 1) { + size_t length; + length = mbsrtowcs(wcs, (const char **)&ptr, WCS_SIZE, NULL); + /* for simplicity */ + if ((length == (size_t) - 1) || length > 1) { return -1; } else { - swprintf(buf, length, L"%0*d", (int)(n * str_len), 0); - int k = n; + wcs[length] = L'\0'; + size_t k = n; while (k) { *buf = *wcs; ++buf; @@ -268,8 +270,6 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) buf[n] = L'\0'; return n; } - -// return -1; } } } @@ -299,4 +299,4 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str) } return (int)(n * str_len); } - +#endif diff --git a/src/fort_utils.h b/src/fort_utils.h index e4eb278..beb6224 100644 --- a/src/fort_utils.h +++ b/src/fort_utils.h @@ -134,7 +134,9 @@ size_t number_of_columns_in_format_wstring(const wchar_t *fmt); /*int snprint_n_chars(char *buf, size_t length, size_t n, char ch);*/ /*int wsnprint_n_chars(wchar_t *buf, size_t length, size_t n, wchar_t ch);*/ int snprint_n_strings(char *buf, size_t length, size_t n, const char *str); +#if defined(FT_HAVE_WCHAR) int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str); +#endif #define CHCK_RSLT_ADD_TO_WRITTEN(statement) \ diff --git a/src/properties.c b/src/properties.c index 3d794ac..051ff32 100644 --- a/src/properties.c +++ b/src/properties.c @@ -90,6 +90,7 @@ const char *text_styles[] = { "", "\033[1m", "\033[2m", + "\033[3m", "\033[4m", "\033[5m", "\033[7m", @@ -100,6 +101,7 @@ const char *reset_text_styles[] = { "", "\033[21m", "\033[22m", + "\033[23m", "\033[24m", "\033[25m", "\033[27m", diff --git a/src/properties.h b/src/properties.h index 2535ab0..212b78c 100644 --- a/src/properties.h +++ b/src/properties.h @@ -11,13 +11,19 @@ #define TEXT_STYLE_TAG_MAX_SIZE 64 +FT_INTERNAL void get_style_tag_for_cell(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); + +FT_INTERNAL void get_reset_style_tag_for_cell(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); +FT_INTERNAL void get_style_tag_for_content(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); + +FT_INTERNAL void get_reset_style_tag_for_content(const fort_table_properties_t *props, size_t row, size_t col, char *style_tag, size_t sz); diff --git a/tests/main_test.c b/tests/main_test.c index 93faed0..488a67d 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -53,146 +53,16 @@ int main(void) * Essential for OSX, because swprintf can fail in case of real unicode * chars. */ -//#if defined(FT_HAVE_WCHAR) -// setlocale(LC_CTYPE, ""); -//#endif - -//#ifdef FORT_WB_TESTING_ENABLED -// status |= run_wb_test_suit(); -// fprintf(stderr, "\n"); -//#endif -// status |= run_bb_test_suit(); - - +#if defined(FT_HAVE_WCHAR) setlocale(LC_CTYPE, ""); +#endif - ft_table_t *table = NULL; - table = ft_create_table(); - ft_set_border_style(table, FT_DOUBLE2_STYLE); - ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); - - ft_wwrite_ln(table, L"some", L"some3333", L"11111"); - ft_wwrite_ln(table, L"s", L"some333377777", L"1111111111"); - ft_wwrite_ln(table, L"some", L"some3333", L"111111"); - ft_wwrite_ln(table, L"✖", L"☑", L"✔"); +#ifdef FORT_WB_TESTING_ENABLED + status |= run_wb_test_suit(); + fprintf(stderr, "\n"); +#endif + status |= run_bb_test_suit(); - ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); - ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - - ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW); - ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA); - ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - - ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED); - ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED); - - ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN); - ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD); - - const wchar_t *table_wstr = ft_to_wstring(table); - if (table_wstr) { - fwprintf(stdout, L"Table:\n%ls\n ", table_wstr); - } else { - fwprintf(stdout, L"Table conversion failed !!!\n "); - } - - ft_destroy_table(table); - - - - // ///////////////////////// - - - table = ft_create_table(); - ft_set_border_style(table, FT_DOUBLE_STYLE); - ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); - - ft_wwrite_ln(table, L"Test", L"Iterations", L"ms/op", L"Passed"); - ft_wwrite_ln(table, L"n-body", L"1000", L"1.629"); - ft_wwrite_ln(table, L"", L"2500", L"6.3", L"✔"); - ft_wwrite_ln(table, L"", L"10000", L"10.5"); - ft_add_separator(table); - ft_wwrite_ln(table, L"regex-redux", L"1000", L"1.629"); - ft_wwrite_ln(table, L"", L"2500", L"6.3", L"✖"); - ft_wwrite_ln(table, L"", L"10000", L"10.5"); - ft_add_separator(table); - ft_wwrite_ln(table, L"mandelbrot", L"1000", L"1.629"); - ft_wwrite_ln(table, L"", L"2500", L"6.3", L"✔"); - ft_wwrite_ln(table, L"", L"10000", L"10.5"); - ft_add_separator(table); - ft_set_cell_span(table, 10, 0, 3); - ft_wwrite_ln(table, L"Total result", L"", L"", L"✖"); - - - ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD); - ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD); - - ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_RIGHT); - ft_set_cell_prop(table, FT_ANY_ROW, 3, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); - ft_set_cell_prop(table, 10, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); - - ft_set_cell_prop(table, 2, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN); - ft_set_cell_prop(table, 5, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_RED); - ft_set_cell_prop(table, 8, 3, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN); - - -// ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); -// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); -// ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - -// ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED); -// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_RED); - -// ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN); -// ft_set_cell_prop(table, 3, FT_ANY_COLUMN, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_BOLD); - - table_wstr = ft_to_wstring(table); - if (table_wstr) { - fwprintf(stdout, L"Table:\n%ls\n ", table_wstr); - } else { - fwprintf(stdout, L"Table conversion failed !!!\n "); - } - - ft_destroy_table(table); - - -// printf("\n\n------ Some debug stuff delete later --------\n\n\n"); - - -// ft_table_t *table = ft_create_table(); -// ft_set_cell_prop(table, 0, 0, FT_CPROP_CONT_FG_COLOR, FT_COLOR_RED); -// ft_set_cell_prop(table, 1, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_GREEN); -// ft_set_cell_prop(table, FT_ANY_ROW, 2, FT_CPROP_CONT_FG_COLOR, FT_COLOR_MAGENTA); -// ft_set_cell_prop(table, FT_ANY_ROW, 2, FT_CPROP_CONT_BG_COLOR, FT_COLOR_YELLOW); - -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CONT_FG_COLOR, FT_COLOR_YELLOW); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_BG_COLOR, FT_COLOR_MAGENTA); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - -// ft_set_cell_prop(table, 2, FT_ANY_COLUMN, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_BOLD); -// ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); -// ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); -// ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); -// ft_set_cell_prop(table, 0, 1, FT_CPROP_CELL_TEXT_STYLE, FT_TSTYLE_UNDERLINED); -// ft_set_cell_prop(table, 0, 2, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); -// ft_set_cell_prop(table, 2, 1, FT_CPROP_CONT_TEXT_STYLE, FT_TSTYLE_UNDERLINED); - - -// ft_write_ln(table, "some", "some3333", "11111"); -// ft_write_ln(table, "s", "some333377777", "1111111111"); -// ft_write_ln(table, "some", "some3333", "111111"); -// const char *str_repr = ft_to_string(table); -// printf("%s\n", str_repr ? str_repr : "NULL"); -// ft_destroy_table(table); - -// printf("aa \033[42m 11 \033[4m 222 \033[49m 123 \033[24m 666\n"); return status; }