]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add scols_cell_get_alignment()
authorKarel Zak <kzak@redhat.com>
Mon, 9 Jan 2017 12:28:24 +0000 (13:28 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 17 Jan 2017 15:10:17 +0000 (16:10 +0100)
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 <kzak@redhat.com>
libsmartcols/src/cell.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/smartcolsP.h
libsmartcols/src/table_print.c

index 4450c576b04b5b653d8eef2b58c9edcac2e98267..04e9bfc203eae9992f6b5c83cbe4aa3c4e71e65d 100644 (file)
@@ -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
index 4a00fed1bd4100c2c84ce5e8ba5f1aeed3f489a4..88c64668fb451a4f5777b70fec30066d56b0a572 100644 (file)
@@ -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);
index 2a76048e1d586d940864ac08de0242ed99858bee..2e8eee7fdcd465229a8c5fea69aa682a3f9b72f4 100644 (file)
@@ -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 */
index c73154ec6facba6354589d43936d9d07b7e666ab..9ed4f943da4534aef92b2b4bfe98b289a0715e97 100644 (file)
@@ -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,