]> git.ipfire.org Git - pakfire.git/commitdiff
cli: table: Automatically determine the alignment based on type
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 16:17:01 +0000 (16:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 16:17:01 +0000 (16:17 +0000)
We should not ever need any of the other cases.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/repo_list.c
src/cli/lib/repolist.c
src/cli/lib/table.c
src/cli/lib/table.h

index 7b99593f9f4045c44234b5bddcea5e720fa2aca4..9ea8a3d15f1d7246568b3d64640ab0e47a03c2c5 100644 (file)
@@ -75,11 +75,11 @@ static int response_callback(struct pakfire_xfer* xfer,
                goto ERROR;
 
        // Add the header
-       r = cli_table_add_col(table, "Name", CLI_TABLE_STRING, CLI_TABLE_ALIGN_LEFT);
+       r = cli_table_add_col(table, "Name", CLI_TABLE_STRING);
        if (r < 0)
                goto ERROR;
 
-       r = cli_table_add_col(table, "Priority", CLI_TABLE_INTEGER, CLI_TABLE_ALIGN_RIGHT);
+       r = cli_table_add_col(table, "Priority", CLI_TABLE_INTEGER);
        if (r < 0)
                goto ERROR;
 
index 5a627acc29824d40b2f7763e7b307b9bd349a98c..2d84e88fcf04df926f2cfb067b6b3bf73409676b 100644 (file)
@@ -60,20 +60,20 @@ int cli_repolist(void* data, int argc, char* argv[]) {
                goto ERROR;
 
        // Add a column with the name
-       r = cli_table_add_col(table, "Repository", CLI_TABLE_STRING, CLI_TABLE_ALIGN_LEFT);
+       r = cli_table_add_col(table, "Repository", CLI_TABLE_STRING);
        if (r < 0)
                goto ERROR;
 
        // Add enabled status
-       r = cli_table_add_col(table, "Enabled", CLI_TABLE_YESNO, CLI_TABLE_ALIGN_RIGHT);
+       r = cli_table_add_col(table, "Enabled", CLI_TABLE_YESNO);
        if (r < 0)
                goto ERROR;
 
-       r = cli_table_add_col(table, "Priority", CLI_TABLE_INTEGER, CLI_TABLE_ALIGN_RIGHT);
+       r = cli_table_add_col(table, "Priority", CLI_TABLE_INTEGER);
        if (r < 0)
                goto ERROR;
 
-       r = cli_table_add_col(table, "Packages", CLI_TABLE_INTEGER, CLI_TABLE_ALIGN_RIGHT);
+       r = cli_table_add_col(table, "Packages", CLI_TABLE_INTEGER);
        if (r < 0)
                goto ERROR;
 
index 5236c2952551016dd1686b8c4ecad601697de050..5d4cb4e6d4bc092d8b272f2e5a5637295999670c 100644 (file)
 #include "table.h"
 #include "terminal.h"
 
+typedef enum {
+       CLI_TABLE_ALIGN_LEFT  = 'L',
+       CLI_TABLE_ALIGN_RIGHT = 'R',
+} cli_table_col_align;
+
 typedef struct cli_table_col {
        STAILQ_ENTRY(cli_table_col) nodes;
 
@@ -138,8 +143,7 @@ void cli_table_free(cli_table* self) {
        free(self);
 }
 
-int cli_table_add_col(cli_table* self,
-               const char* name, cli_table_col_type type, cli_table_col_align align) {
+int cli_table_add_col(cli_table* self, const char* name, cli_table_col_type type) {
        cli_table_col* col = NULL;
 
        // Allocate a new col
@@ -154,7 +158,17 @@ int cli_table_add_col(cli_table* self,
        col->type = type;
 
        // Store the alignment
-       col->align = align;
+       switch (col->type) {
+               case CLI_TABLE_STRING:
+                       col->align = CLI_TABLE_ALIGN_LEFT;
+                       break;
+
+               case CLI_TABLE_INTEGER:
+               case CLI_TABLE_FLOAT:
+               case CLI_TABLE_YESNO:
+                       col->align = CLI_TABLE_ALIGN_RIGHT;
+                       break;
+       }
 
        // The column needs to be wide enough to fit the title
        col->width = strlen(col->name);
index c75f9f7e5e8b9d9dfc07520ce05e46ae7bce1134..7ed2088ab766ba8f35ed6da0407b99df69538730 100644 (file)
@@ -36,13 +36,7 @@ typedef enum {
        CLI_TABLE_YESNO,
 } cli_table_col_type;
 
-typedef enum {
-       CLI_TABLE_ALIGN_LEFT,
-       CLI_TABLE_ALIGN_RIGHT,
-} cli_table_col_align;
-
-int cli_table_add_col(cli_table* self,
-       const char* name, cli_table_col_type type, cli_table_col_align align);
+int cli_table_add_col(cli_table* self, const char* name, cli_table_col_type type);
 
 int cli_table_add_row(cli_table* self, ...);