From: Michael Tremer Date: Tue, 1 Jul 2025 16:17:01 +0000 (+0000) Subject: cli: table: Automatically determine the alignment based on type X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc6acb079301c5e4021d098aaea7d6b050c7c1c6;p=pakfire.git cli: table: Automatically determine the alignment based on type We should not ever need any of the other cases. Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/repo_list.c b/src/cli/lib/repo_list.c index 7b99593f..9ea8a3d1 100644 --- a/src/cli/lib/repo_list.c +++ b/src/cli/lib/repo_list.c @@ -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; diff --git a/src/cli/lib/repolist.c b/src/cli/lib/repolist.c index 5a627acc..2d84e88f 100644 --- a/src/cli/lib/repolist.c +++ b/src/cli/lib/repolist.c @@ -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; diff --git a/src/cli/lib/table.c b/src/cli/lib/table.c index 5236c295..5d4cb4e6 100644 --- a/src/cli/lib/table.c +++ b/src/cli/lib/table.c @@ -28,6 +28,11 @@ #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); diff --git a/src/cli/lib/table.h b/src/cli/lib/table.h index c75f9f7e..7ed2088a 100644 --- a/src/cli/lib/table.h +++ b/src/cli/lib/table.h @@ -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, ...);