From a7c8fe3970716a0962b837bf70dd5b119f16f3ba Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 9 Jan 2017 13:28:24 +0100 Subject: [PATCH] libsmartcols: add scols_cell_get_alignment() Just to hide that we use cell flags (bitwise operations) to define cell content alignment. The patch also more explicitly specifies the flags in the header file. The alignment is evaluated in the order: right, center, left. The default is left. Note that SCOLS_CELL_FL_* are used for for table title only. v2.29.1: function not exported by API Signed-off-by: Karel Zak --- libsmartcols/src/cell.c | 16 ++++++++++++++++ libsmartcols/src/libsmartcols.h.in | 7 ++++--- libsmartcols/src/smartcolsP.h | 3 +++ libsmartcols/src/table_print.c | 17 +++++++++++------ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index 4450c576b0..04e9bfc203 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -214,6 +214,22 @@ int scols_cell_get_flags(const struct libscols_cell *ce) return ce->flags; } +/** + * scols_cell_get_alignment: + * @ce: a pointer to a struct libscols_cell instance + * + * Returns: SCOLS_CELL_FL_{RIGHT,CELNTER,LEFT} + */ +int scols_cell_get_alignment(const struct libscols_cell *ce) +{ + if (ce->flags & SCOLS_CELL_FL_RIGHT) + return SCOLS_CELL_FL_RIGHT; + else if (ce->flags & SCOLS_CELL_FL_CENTER) + return SCOLS_CELL_FL_CENTER; + + return SCOLS_CELL_FL_LEFT; /* default */ +} + /** * scols_cell_copy_content: * @dest: a pointer to a struct libscols_cell instance diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 4a00fed1bd..88c64668fb 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -91,9 +91,10 @@ enum { * Cell flags, see scols_cell_set_flags() before use */ enum { - SCOLS_CELL_FL_LEFT = 0, - SCOLS_CELL_FL_CENTER, - SCOLS_CELL_FL_RIGHT + /* alignment evaluated in order: right,center,left */ + SCOLS_CELL_FL_LEFT = 0, + SCOLS_CELL_FL_CENTER = (1 << 0), + SCOLS_CELL_FL_RIGHT = (1 << 1) }; extern struct libscols_iter *scols_new_iter(int direction); diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 2a76048e1d..2e8eee7fdc 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -201,4 +201,7 @@ static inline int scols_iter_is_last(const struct libscols_iter *itr) return itr->p == itr->head; } +/* temporary for 2.29.1, public for v2.30 */ +extern int scols_cell_get_alignment(const struct libscols_cell *ce); + #endif /* _LIBSMARTCOLS_PRIVATE_H */ diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index c73154ec6f..9ed4f943da 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -782,14 +782,19 @@ static int print_title(struct libscols_table *tb) goto done; } - if (tb->title.flags & SCOLS_CELL_FL_LEFT) - align = MBS_ALIGN_LEFT; - else if (tb->title.flags & SCOLS_CELL_FL_RIGHT) + switch (scols_cell_get_alignment(&tb->title)) { + case SCOLS_CELL_FL_RIGHT: align = MBS_ALIGN_RIGHT; - else if (tb->title.flags & SCOLS_CELL_FL_CENTER) + break; + case SCOLS_CELL_FL_CENTER: align = MBS_ALIGN_CENTER; - else - align = MBS_ALIGN_LEFT; /* default */ + break; + case SCOLS_CELL_FL_LEFT: + default: + align = MBS_ALIGN_LEFT; + break; + + } /* copy from buf to title and align to width with title_padding */ rc = mbsalign_with_padding(buf, title, titlesz, -- 2.47.2