diff --git a/example/main.c b/example/main.c index 1cac688..f3a719d 100644 --- a/example/main.c +++ b/example/main.c @@ -22,6 +22,29 @@ static ft_table_t *create_basic_table(void) return table; } +#if defined(FT_HAVE_WCHAR) +static ft_table_t *create_basic_wtable(const struct ft_border_style *style) +{ + ft_table_t *table = ft_create_table(); + ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER); + ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT); + + ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); + ft_wwrite_ln(table, L"Rank", L"Title", L"Year", L"Rating"); + + ft_wwrite_ln(table, L"1", L"The Shawshank Redemption", L"1994", L"9.5"); + ft_wwrite_ln(table, L"2", L"12 Angry Men", L"1957", L"8.8"); + ft_wwrite_ln(table, L"3", L"It's a Wonderful Life", L"1946", L"8.6"); + ft_add_separator(table); + ft_wwrite_ln(table, L"4", L"2001: A Space Odyssey", L"1968", L"8.5"); + ft_wwrite_ln(table, L"5", L"Blade Runner", L"1982", L"8.1"); + + ft_set_border_style(table, style); + + return table; +} +#endif + void print_char_str(const char *str) { printf("Char_repr:\n"); @@ -210,6 +233,67 @@ void colorfull_table(void) #endif } + +void print_different_border_styles() +{ +#if defined(FT_HAVE_WCHAR) + + ft_table_t *table = ft_create_table(); + ft_set_border_style(table, FT_EMPTY_STYLE); + + ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 3); + ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_LEFT_PADDING, 10); + + struct ft_border_style *styles[] = { + FT_BASIC_STYLE, + FT_BASIC2_STYLE, + FT_SIMPLE_STYLE, + FT_PLAIN_STYLE, + FT_DOT_STYLE, + FT_EMPTY_STYLE, + FT_SOLID_STYLE, + FT_SOLID_ROUND_STYLE, + FT_NICE_STYLE, + FT_DOUBLE_STYLE, + FT_DOUBLE2_STYLE, + FT_BOLD_STYLE, + FT_BOLD2_STYLE, + FT_FRAME_STYLE + + }; + + const wchar_t *style_names[] = { + L"FT_BASIC_STYLE", + L"FT_BASIC2_STYLE", + L"FT_SIMPLE_STYLE", + L"FT_PLAIN_STYLE", + L"FT_DOT_STYLE", + L"FT_EMPTY_STYLE", + L"FT_SOLID_STYLE", + L"FT_SOLID_ROUND_STYLE", + L"FT_NICE_STYLE", + L"FT_DOUBLE_STYLE", + L"FT_DOUBLE2_STYLE", + L"FT_BOLD_STYLE", + L"FT_BOLD2_STYLE", + L"FT_FRAME_STYLE" + }; + + for (size_t i = 0; i < sizeof(styles)/sizeof(styles[0]); i += 2) { + ft_table_t *table_tmp_1 = create_basic_wtable(styles[i]); + ft_table_t *table_tmp_2 = create_basic_wtable(styles[i + 1]); + + ft_wprintf(table, L" %ls \n\n%ls", style_names[i], ft_to_wstring(table_tmp_1)); + ft_wprintf_ln(table, L" %ls \n\n%ls", style_names[i + 1], ft_to_wstring(table_tmp_2)); + + ft_destroy_table(table_tmp_1); + ft_destroy_table(table_tmp_2); + } + fwprintf(stderr, L"Table:\n%ls\n ", ft_to_wstring(table)); + ft_destroy_table(table); +#endif +} + int main(void) { base_example(); @@ -400,6 +484,9 @@ int main(void) } ft_destroy_table(table); + + + print_different_border_styles(); #endif return result; diff --git a/lib/fort.c b/lib/fort.c index 267df3b..353f917 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -360,7 +360,7 @@ FT_INTERNAL void *buffer_get_data(string_buffer_t *buffer); FT_INTERNAL -size_t buffer_text_width(string_buffer_t *buffer); +size_t buffer_text_width(const string_buffer_t *buffer); FT_INTERNAL int buffer_printf(string_buffer_t *buffer, size_t buffer_row, char *buf, size_t total_buf_len, @@ -4252,8 +4252,9 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) #define CREATE_ROW_FROM_STRING create_row_from_string #define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_string #define FILL_CELL_FROM_STRING fill_cell_from_string +#define STR_BUF_TYPE CharBuf - string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, CharBuf); + string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; @@ -4325,6 +4326,7 @@ clear: #undef CREATE_ROW_FROM_STRING #undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef FILL_CELL_FROM_STRING +#undef STR_BUF_TYPE } #ifdef FT_HAVE_WCHAR @@ -4336,8 +4338,9 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) #define CREATE_ROW_FROM_STRING create_row_from_wstring #define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_wstring #define FILL_CELL_FROM_STRING fill_cell_from_wstring +#define STR_BUF_TYPE WCharBuf - string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, CharBuf); + string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; @@ -4409,6 +4412,7 @@ clear: #undef CREATE_ROW_FROM_STRING #undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef FILL_CELL_FROM_STRING +#undef STR_BUF_TYPE } #endif @@ -4892,7 +4896,7 @@ size_t buffer_text_height(string_buffer_t *buffer) FT_INTERNAL -size_t buffer_text_width(string_buffer_t *buffer) +size_t buffer_text_width(const string_buffer_t *buffer) { size_t max_length = 0; if (buffer->type == CharBuf) { diff --git a/lib/fort.h b/lib/fort.h index 2d6c608..b639ccb 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -542,9 +542,6 @@ int ft_table_write(ft_table_t *table, size_t rows, size_t cols, const char *tabl int ft_table_write_ln(ft_table_t *table, size_t rows, size_t cols, const char *table_cells[]); - - - /** * Add separator after the current row. * @@ -557,8 +554,6 @@ int ft_table_write_ln(ft_table_t *table, size_t rows, size_t cols, const char *t int ft_add_separator(ft_table_t *table); - - /** * Convert table to string representation. * @@ -584,12 +579,6 @@ const char *ft_to_string(const ft_table_t *table); - - - - - - /** * Structure describing border appearance. */ @@ -696,37 +685,37 @@ int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style); * Colors. */ enum ft_color { - FT_COLOR_DEFAULT = 0, - FT_COLOR_BLACK = 1, - FT_COLOR_RED = 2, - FT_COLOR_GREEN = 3, - FT_COLOR_YELLOW = 4, - FT_COLOR_BLUE = 5, - FT_COLOR_MAGENTA = 6, - FT_COLOR_CYAN = 7, - FT_COLOR_LIGHT_GRAY = 8, - FT_COLOR_DARK_GRAY = 9, - FT_COLOR_LIGHT_RED = 10, - FT_COLOR_LIGHT_GREEN = 11, - FT_COLOR_LIGHT_YELLOW = 12, - FT_COLOR_LIGHT_BLUE = 13, - FT_COLOR_LIGHT_MAGENTA = 15, - FT_COLOR_LIGHT_CYAN = 16, - FT_COLOR_LIGHT_WHYTE = 17 + FT_COLOR_DEFAULT = 0, /**< Default color */ + FT_COLOR_BLACK = 1, /**< Black color*/ + FT_COLOR_RED = 2, /**< Red color */ + FT_COLOR_GREEN = 3, /**< Green color */ + FT_COLOR_YELLOW = 4, /**< Yellow color */ + FT_COLOR_BLUE = 5, /**< Blue color */ + FT_COLOR_MAGENTA = 6, /**< Magenta color */ + FT_COLOR_CYAN = 7, /**< Cyan color */ + FT_COLOR_LIGHT_GRAY = 8, /**< Light gray color */ + FT_COLOR_DARK_GRAY = 9, /**< Dark gray color */ + FT_COLOR_LIGHT_RED = 10, /**< Light red color */ + FT_COLOR_LIGHT_GREEN = 11, /**< Light green color */ + FT_COLOR_LIGHT_YELLOW = 12, /**< Light yellow color */ + FT_COLOR_LIGHT_BLUE = 13, /**< Light blue color */ + FT_COLOR_LIGHT_MAGENTA = 15, /**< Light magenta color */ + FT_COLOR_LIGHT_CYAN = 16, /**< Light cyan color */ + FT_COLOR_LIGHT_WHYTE = 17 /**< Light whyte color */ }; /** * Text styles. */ enum ft_text_style { - FT_TSTYLE_DEFAULT = (1U << 0), - FT_TSTYLE_BOLD = (1U << 1), - FT_TSTYLE_DIM = (1U << 2), - FT_TSTYLE_ITALIC = (1U << 3), - FT_TSTYLE_UNDERLINED = (1U << 4), - FT_TSTYLE_BLINK = (1U << 5), - FT_TSTYLE_INVERTED = (1U << 6), - FT_TSTYLE_HIDDEN = (1U << 7) + FT_TSTYLE_DEFAULT = (1U << 0), /**< Default style */ + FT_TSTYLE_BOLD = (1U << 1), /**< Bold */ + FT_TSTYLE_DIM = (1U << 2), /**< Dim */ + FT_TSTYLE_ITALIC = (1U << 3), /**< Italic */ + FT_TSTYLE_UNDERLINED = (1U << 4), /**< Underlined */ + FT_TSTYLE_BLINK = (1U << 5), /**< Blink */ + FT_TSTYLE_INVERTED = (1U << 6), /**< Reverse (invert the foreground and background colors) */ + FT_TSTYLE_HIDDEN = (1U << 7) /**< Hidden (useful for passwords) */ }; diff --git a/src/row.c b/src/row.c index e65b878..3366054 100644 --- a/src/row.c +++ b/src/row.c @@ -772,8 +772,9 @@ fort_row_t *create_row_from_fmt_string(const char *fmt, va_list *va_args) #define CREATE_ROW_FROM_STRING create_row_from_string #define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_string #define FILL_CELL_FROM_STRING fill_cell_from_string +#define STR_BUF_TYPE CharBuf - string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, CharBuf); + string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; @@ -845,6 +846,7 @@ clear: #undef CREATE_ROW_FROM_STRING #undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef FILL_CELL_FROM_STRING +#undef STR_BUF_TYPE } #ifdef FT_HAVE_WCHAR @@ -856,8 +858,9 @@ fort_row_t *create_row_from_fmt_wstring(const wchar_t *fmt, va_list *va_args) #define CREATE_ROW_FROM_STRING create_row_from_wstring #define NUMBER_OF_COLUMNS_IN_FORMAT_STRING number_of_columns_in_format_wstring #define FILL_CELL_FROM_STRING fill_cell_from_wstring +#define STR_BUF_TYPE WCharBuf - string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, CharBuf); + string_buffer_t *buffer = create_string_buffer(DEFAULT_STR_BUF_SIZE, STR_BUF_TYPE); if (buffer == NULL) return NULL; @@ -929,6 +932,7 @@ clear: #undef CREATE_ROW_FROM_STRING #undef NUMBER_OF_COLUMNS_IN_FORMAT_STRING #undef FILL_CELL_FROM_STRING +#undef STR_BUF_TYPE } #endif diff --git a/src/string_buffer.c b/src/string_buffer.c index 97287ae..47b962f 100644 --- a/src/string_buffer.c +++ b/src/string_buffer.c @@ -290,7 +290,7 @@ size_t buffer_text_height(string_buffer_t *buffer) FT_INTERNAL -size_t buffer_text_width(string_buffer_t *buffer) +size_t buffer_text_width(const string_buffer_t *buffer) { size_t max_length = 0; if (buffer->type == CharBuf) { diff --git a/src/string_buffer.h b/src/string_buffer.h index 4fd1385..8632acb 100644 --- a/src/string_buffer.h +++ b/src/string_buffer.h @@ -54,7 +54,7 @@ FT_INTERNAL void *buffer_get_data(string_buffer_t *buffer); FT_INTERNAL -size_t buffer_text_width(string_buffer_t *buffer); +size_t buffer_text_width(const string_buffer_t *buffer); FT_INTERNAL int buffer_printf(string_buffer_t *buffer, size_t buffer_row, char *buf, size_t total_buf_len, diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index cfc2e01..7d7699e 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -3,6 +3,29 @@ #include #include "fort.h" +#ifdef FT_HAVE_WCHAR +void test_bug_fixes(void) +{ + SCENARIO("Bug 1") { + ft_table_t *table = ft_create_table(); + + ft_table_t *table_tmp_1 = ft_create_table(); + +// ft_set_border_style(table_tmp_1, FT_EMPTY_STYLE); + ft_set_cell_prop(table_tmp_1, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER); + ft_wwrite_ln(table_tmp_1, L"Rank", L"Title", L"Year", L"Rating"); + ft_wwrite_ln(table_tmp_1, L"1", L"The Shawshank Redemption", L"3"); + + const wchar_t *str = ft_to_wstring(table_tmp_1); + (void)str; + ft_wprintf_ln(table, str); + + ft_destroy_table(table_tmp_1); + ft_destroy_table(table); + } +} +#endif + void test_table_basic(void) { ft_table_t *table = NULL; diff --git a/tests/main_test.c b/tests/main_test.c index 26f22bf..20ab223 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -3,6 +3,9 @@ #include "fort.h" /* Test cases */ +#ifdef FT_HAVE_WCHAR +void test_bug_fixes(void); +#endif void test_vector_basic(void); void test_vector_stress(void); void test_string_buffer(void); @@ -35,6 +38,9 @@ struct test_case wb_test_suit [] = { struct test_case bb_test_suit [] = { +#ifdef FT_HAVE_WCHAR + {"test_bug_fixes", test_bug_fixes}, +#endif {"test_table_basic", test_table_basic}, #ifdef FT_HAVE_WCHAR {"test_wcs_table_boundaries", test_wcs_table_boundaries}, diff --git a/tests/wb_tests/test_string_buffer.c b/tests/wb_tests/test_string_buffer.c index fc577f9..47ff884 100644 --- a/tests/wb_tests/test_string_buffer.c +++ b/tests/wb_tests/test_string_buffer.c @@ -13,7 +13,7 @@ fort_status_t str_n_substring(const char *str, char ch_separator, size_t n, cons void wstr_n_substring(const wchar_t *str, wchar_t ch_separator, size_t n, const wchar_t **begin, const wchar_t **end); -size_t buffer_text_width(string_buffer_t *buffer); +//size_t buffer_text_width(string_buffer_t *buffer); void test_strchr_count(void);