]> git.ipfire.org Git - pakfire.git/commitdiff
cli: repo list: Use the new thing to format tables
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 16:06:26 +0000 (16:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 16:06:26 +0000 (16:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/repo_list.c

index 7b0a436752b1e46b922a7dd7a2e1f9dadb1e3297..7b99593f9f4045c44234b5bddcea5e720fa2aca4 100644 (file)
@@ -28,6 +28,7 @@
 #include "dump.h"
 #include "pakfire.h"
 #include "repo_list.h"
+#include "table.h"
 
 static const char* doc = "Lists all repositories";
 
@@ -58,6 +59,7 @@ static int response_callback(struct pakfire_xfer* xfer,
                const pakfire_xfer_response* response, void* data) {
        struct cli_local_args* local_args = data;
        json_object* repo = NULL;
+       cli_table* table = NULL;
        const char* name = NULL;
        int64_t priority = 0;
        int r;
@@ -67,8 +69,19 @@ static int response_callback(struct pakfire_xfer* xfer,
        if (r < 0)
                return 0;
 
-       // Show a header
-       printf("%-12s %-12s\n", "Name", "Priority");
+       // Create a new table
+       r = cli_table_create(&table);
+       if (r < 0)
+               goto ERROR;
+
+       // Add the header
+       r = cli_table_add_col(table, "Name", CLI_TABLE_STRING, CLI_TABLE_ALIGN_LEFT);
+       if (r < 0)
+               goto ERROR;
+
+       r = cli_table_add_col(table, "Priority", CLI_TABLE_INTEGER, CLI_TABLE_ALIGN_RIGHT);
+       if (r < 0)
+               goto ERROR;
 
        // Loop through all repos
        for (size_t i = 0; i < json_object_array_length(response->data); i++) {
@@ -79,16 +92,26 @@ static int response_callback(struct pakfire_xfer* xfer,
                // Fetch the name
                r = pakfire_json_get_string(repo, "name", &name);
                if (r < 0)
-                       break;
+                       goto ERROR;
 
                // Fetch the priority
                r = pakfire_json_get_int64(repo, "priority", &priority);
                if (r < 0)
-                       break;
+                       goto ERROR;
 
-               printf("%-12s %-12ld\n", name, priority);
+               // Add a new row to the table
+               r = cli_table_add_row(table, name, priority);
+               if (r < 0)
+                       goto ERROR;
        }
 
+       // Print the table
+       r = cli_table_print(table, stdout);
+
+ERROR:
+       if (table)
+               cli_table_free(table);
+
        return r;
 }