From 83c6b9cca3817b193ad4a088c6ce93c7a167f54f Mon Sep 17 00:00:00 2001 From: seleznevae Date: Wed, 3 Jan 2018 11:21:45 +0300 Subject: [PATCH] [A] Added empty string height to options --- include/fort.h | 1 + src/fort.c | 8 +++-- tests/test_table.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/include/fort.h b/include/fort.h index 0029c7c..13e9bf9 100644 --- a/include/fort.h +++ b/include/fort.h @@ -107,6 +107,7 @@ struct fort_table_options int cell_padding_bottom; int cell_padding_left; int cell_padding_right; + int cell_empty_string_height; char hor_separator; char ver_separator; }; diff --git a/src/fort.c b/src/fort.c index 28261c5..914585b 100644 --- a/src/fort.c +++ b/src/fort.c @@ -89,7 +89,7 @@ static fort_status_t fill_buffer_from_string(string_buffer_t *buffer, const char static size_t buffer_text_height(string_buffer_t *buffer) { - if (buffer == NULL) { + if (buffer == NULL || buffer->str == NULL || strlen(buffer->str) == 0) { return 0; } return 1; @@ -147,7 +147,7 @@ static void* vector_at(vector_t*, size_t index); // int padding_right; //}; //typedef struct cell_options cell_options_t; -static fort_table_options_t g_table_options = {1, 1, 1, 1, '=', '|'}; +static fort_table_options_t g_table_options = {1, 1, 1, 1, 1, '=', '|'}; static void init_cell_options(fort_table_options_t *options) { @@ -156,6 +156,7 @@ static void init_cell_options(fort_table_options_t *options) options->cell_padding_bottom = g_table_options.cell_padding_bottom; options->cell_padding_left = g_table_options.cell_padding_left; options->cell_padding_right = g_table_options.cell_padding_right; + options->cell_empty_string_height = g_table_options.cell_empty_string_height; } struct fort_cell; @@ -203,7 +204,8 @@ static int hint_height_cell(const fort_cell_t *cell) assert(cell); int result = cell->options.cell_padding_top + cell->options.cell_padding_bottom; if (cell->str_buffer && cell->str_buffer->str) { - result += buffer_text_height(cell->str_buffer); + size_t text_height = buffer_text_height(cell->str_buffer); + result += text_height == 0 ? cell->options.cell_empty_string_height : text_height; } return result; } diff --git a/tests/test_table.c b/tests/test_table.c index fcbf352..5fbccb4 100644 --- a/tests/test_table.c +++ b/tests/test_table.c @@ -391,9 +391,82 @@ void test_table_options(void **state) } + WHEN("Empty string has 0 heigt") { + fort_table_options_t table_options; + memcpy(&table_options, &def_options, sizeof(fort_table_options_t)); + table_options.cell_padding_bottom = 1; + table_options.cell_padding_top = 1; + table_options.cell_padding_left = 1; + table_options.cell_padding_right = 1; + table_options.cell_empty_string_height = 0; + ft_set_default_options(&table_options); + + table = create_test_int_table(); + int n = ft_row_printf(table, 3, "|||"); + assert_true( n == 4 ); + + char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "===================\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "===================\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "===================\n" + "| | | | |\n" + "| 3 | 4 | 55 | 67 |\n" + "| | | | |\n" + "===================\n" + "| | | | |\n" + "| | | | |\n" + "===================\n"; +// fprintf(stderr, "content:\n%s", table_str); + + assert_true( strcmp(table_str, table_str_etalon) == 0); + + free(table_str); + ft_destroy_table(table); + } + WHEN("Changing cell separators") { + fort_table_options_t table_options; + memcpy(&table_options, &def_options, sizeof(fort_table_options_t)); + table_options.hor_separator = '|'; + table_options.ver_separator = '='; + ft_set_default_options(&table_options); + + table = create_test_int_table(); + + + char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "|||||||||||||||||||\n" + "= = = = =\n" + "= 3 = 4 = 55 = 67 =\n" + "= = = = =\n" + "|||||||||||||||||||\n" + "= = = = =\n" + "= 3 = 4 = 55 = 67 =\n" + "= = = = =\n" + "|||||||||||||||||||\n" + "= = = = =\n" + "= 3 = 4 = 55 = 67 =\n" + "= = = = =\n" + "|||||||||||||||||||\n"; +// fprintf(stderr, "content:\n%s", table_str); + + assert_true( strcmp(table_str, table_str_etalon) == 0); + + free(table_str); + ft_destroy_table(table); + } }