From 64ce7cbbc0a43010d97e333cc56b31e3218ab646 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 7 Sep 2016 11:07:04 +0200 Subject: [PATCH] libsmartcols: allow to change cell padding char Signed-off-by: Karel Zak --- libsmartcols/docs/libsmartcols-sections.txt | 1 + libsmartcols/src/libsmartcols.h.in | 1 + libsmartcols/src/libsmartcols.sym | 1 + libsmartcols/src/smartcolsP.h | 1 + libsmartcols/src/symbols.c | 20 +++++++++++++++++++- libsmartcols/src/table.c | 1 + libsmartcols/src/table_print.c | 16 +++++++++------- 7 files changed, 33 insertions(+), 8 deletions(-) diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index e752e7056..f2bbbf38f 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -86,6 +86,7 @@ scols_symbols_set_branch scols_symbols_set_right scols_symbols_set_vertical scols_symbols_set_title_padding +scols_symbols_set_cell_padding scols_unref_symbols diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 02867e2f5..1165e0c2d 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -118,6 +118,7 @@ extern int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str extern int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str); extern int scols_symbols_set_right(struct libscols_symbols *sb, const char *str); extern int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str); +extern int scols_symbols_set_cell_padding(struct libscols_symbols *sb, const char *str); /* cell.c */ extern int scols_reset_cell(struct libscols_cell *ce); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 9a4997dd8..7b2cb88bb 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -140,4 +140,5 @@ global: SMARTCOLS_2.29 { global: scols_column_is_wrapnl; + scols_symbols_set_cell_padding; } SMARTCOLS_2.28; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index b3c8e7cfa..c1bb1e07f 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -54,6 +54,7 @@ struct libscols_symbols { char *vert; char *right; char *title_padding; + char *cell_padding; }; /* diff --git a/libsmartcols/src/symbols.c b/libsmartcols/src/symbols.c index df31f454c..d2f7a71ba 100644 --- a/libsmartcols/src/symbols.c +++ b/libsmartcols/src/symbols.c @@ -106,7 +106,7 @@ int scols_symbols_set_right(struct libscols_symbols *sb, const char *str) /** * scols_symbols_set_title_padding: * @sb: a pointer to a struct libscols_symbols instance - * @str: a string which will represent the symbols which wraps title output + * @str: a string which will represent the symbols which fill title output * * The current implementation uses only the first byte from the padding string. * A multibyte chars are not supported yet. @@ -120,6 +120,22 @@ int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str return strdup_to_struct_member(sb, title_padding, str); } +/** + * scols_symbols_set_cell_padding: + * @sb: a pointer to a struct libscols_symbols instance + * @str: a string which will represent the symbols which fill cells + * + * The padding char has to take up just one cell on the terminal. + * + * Returns: 0, a negative value in case of an error. + * + * Since: 2.29 + */ +int scols_symbols_set_cell_padding(struct libscols_symbols *sb, const char *str) +{ + return strdup_to_struct_member(sb, cell_padding, str); +} + /** * scols_copy_symbols: * @sb: a pointer to a struct libscols_symbols instance @@ -146,6 +162,8 @@ struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sb) rc = scols_symbols_set_right(ret, sb->right); if (!rc) rc = scols_symbols_set_title_padding(ret, sb->title_padding); + if (!rc) + rc = scols_symbols_set_cell_padding(ret, sb->cell_padding); if (!rc) return ret; diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 00188fb69..dc3a3016c 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -695,6 +695,7 @@ int scols_table_set_symbols(struct libscols_table *tb, scols_symbols_set_right(tb->symbols, "`-"); } scols_symbols_set_title_padding(tb->symbols, " "); + scols_symbols_set_cell_padding(tb->symbols, " "); } return 0; diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 32b9ddab1..69ee32689 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -27,6 +27,11 @@ #include "carefulputc.h" #include "smartcolsP.h" +#define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ") +#define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n") +#define cellpadding(tb) ((tb) && (tb)->symbols && (tb)->symbols->cell_padding ? (tb)->symbols->cell_padding : " ") + + /* This is private struct to work with output data */ struct libscols_buffer { char *begin; /* begin of the buffer */ @@ -190,9 +195,6 @@ static int is_last_column(struct libscols_column *cl) return 0; } -#define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ") -#define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n") - static int has_pending_data(struct libscols_table *tb) { @@ -248,7 +250,7 @@ static void print_empty_cell(struct libscols_table *tb, /* fill rest of cell with space */ for(; len_pad < cl->width; ++len_pad) - fputc(' ', tb->out); + fputs(cellpadding(tb), tb->out); fputs(colsep(tb), tb->out); } @@ -393,7 +395,7 @@ static int print_pending_data( return 0; for (i = len; i < width; i++) - fputc(' ', tb->out); /* padding */ + fputs(cellpadding(tb), tb->out); /* padding */ fputs(colsep(tb), tb->out); /* columns separator */ return 0; @@ -505,7 +507,7 @@ static int print_data(struct libscols_table *tb, if (color) fputs(color, tb->out); for (i = len; i < width; i++) - fputc(' ', tb->out); + fputs(cellpadding(tb), tb->out); fputs(data, tb->out); if (color) fputs(UL_COLOR_RESET, tb->out); @@ -528,7 +530,7 @@ static int print_data(struct libscols_table *tb, fputs(data, tb->out); } for (i = len; i < width; i++) - fputc(' ', tb->out); /* padding */ + fputs(cellpadding(tb), tb->out); /* padding */ if (is_last_column(cl)) return 0;