]> 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>
Mon, 9 Jan 2017 12:28:24 +0000 (13:28 +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.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/cell.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/table_print.c

index ee696642a63a5d81a4b604cc78d49e02deff9faf..7e31cdda24fcf6046787e11b97c51eeeac41ea7e 100644 (file)
@@ -2,6 +2,7 @@
 <FILE>cell</FILE>
 libscols_cell
 scols_cell_copy_content
+scols_cell_get_alignment
 scols_cell_get_color
 scols_cell_get_data
 scols_cell_get_flags
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..e9df9333c8a281bfb3a670ee8b3300bf96558404 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);
@@ -131,6 +132,7 @@ extern const char *scols_cell_get_color(const struct libscols_cell *ce);
 
 extern int scols_cell_set_flags(struct libscols_cell *ce, int flags);
 extern int scols_cell_get_flags(const struct libscols_cell *ce);
+extern int scols_cell_get_alignment(const struct libscols_cell *ce);
 
 extern void *scols_cell_get_userdata(struct libscols_cell *ce);
 extern int scols_cell_set_userdata(struct libscols_cell *ce, void *data);
index 2bf2b4ee4d237fd3e7a8b7869a4dab0e675c924e..b8a3a5b4c956274d8cc17048a2ba988c5c15409b 100644 (file)
@@ -157,3 +157,9 @@ global:
        scols_wrapnl_chunksize;
        scols_wrapnl_nextchunk;
 } SMARTCOLS_2.28;
+
+
+SMARTCOLS_2.30 {
+global:
+       scols_cell_get_alignment;
+} SMARTCOLS_2.29;
index 65f2612457c4e3d4f4574d96326579602c09da8d..afcbc1647004574226ae5e21c827868b1b178f0b 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,