Move table printer to xdg-app-utils.c

This commit is contained in:
Alexander Larsson committed 2015-09-24 21:36:35 +02:00
1 parent 41af86dc69
commit c40f2ad74e
3 files changed
+90 -79

No files matched your search

+10 -79
View File
@@ -29,6 +29,7 @@
#include "libglnx/libglnx.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
static gboolean opt_show_details;
@@ -37,76 +38,6 @@ static GOptionEntry options[] = {
{ NULL }
};
typedef struct {
GPtrArray *rows;
GPtrArray *current;
int n_columns;
} TablePrinter;
static TablePrinter *
table_printer_new (void)
{
TablePrinter *printer = g_new0 (TablePrinter, 1);
printer->rows = g_ptr_array_new_with_free_func ((GDestroyNotify)g_strfreev);
printer->current = g_ptr_array_new_with_free_func (g_free);
return printer;
}
static void
table_printer_free (TablePrinter *printer)
{
g_ptr_array_free (printer->rows, TRUE);
g_ptr_array_free (printer->current, TRUE);
g_free (printer);
}
static void
table_printer_add_column (TablePrinter *printer,
const char *text)
{
g_ptr_array_add (printer->current, text ? g_strdup (text) : g_strdup (""));
}
static void
table_printer_finish_row (TablePrinter *printer)
{
printer->n_columns = MAX (printer->n_columns, printer->current->len);
g_ptr_array_add (printer->current, NULL);
g_ptr_array_add (printer->rows,
g_ptr_array_free (printer->current, FALSE));
printer->current = g_ptr_array_new_with_free_func (g_free);
}
static void
table_printer_print (TablePrinter *printer)
{
g_autofree int *widths = NULL;
int i, j;
if (printer->current->len != 0)
table_printer_finish_row (printer);
widths = g_new0 (int, printer->n_columns);
for (i = 0; i < printer->rows->len; i++)
{
char **row = g_ptr_array_index (printer->rows,i);
for (j = 0; row[j] != NULL; j++)
widths[j] = MAX (widths[j], strlen (row[j]));
}
for (i = 0; i < printer->rows->len; i++)
{
char **row = g_ptr_array_index (printer->rows,i);
for (j = 0; row[j] != NULL; j++)
g_print ("%-*s%s", widths[j], row[j], j == printer->n_columns - 1 ? "" : "\t");
g_print ("\n");
}
}
gboolean
xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable, GError **error)
{
@@ -124,7 +55,7 @@ xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable,
if (opt_show_details)
{
TablePrinter *printer = table_printer_new ();
XdgAppTablePrinter *printer = xdg_app_table_printer_new ();
GKeyFile *config = ostree_repo_get_config (xdg_app_dir_get_repo (dir));;
g_autoptr(GString) options = g_string_new ("");
@@ -138,17 +69,17 @@ xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable,
group = g_strdup_printf ("remote \"%s\"", remote_name);
table_printer_add_column (printer, remote_name);
xdg_app_table_printer_add_column (printer, remote_name);
title = g_key_file_get_string (config, group, "xa.title", NULL);
if (title)
table_printer_add_column (printer, title);
xdg_app_table_printer_add_column (printer, title);
else
table_printer_add_column (printer, "-");
xdg_app_table_printer_add_column (printer, "-");
ostree_repo_remote_get_url (xdg_app_dir_get_repo (dir), remote_name, &remote_url, NULL);
table_printer_add_column (printer, remote_url);
xdg_app_table_printer_add_column (printer, remote_url);
ostree_repo_remote_get_gpg_verify (xdg_app_dir_get_repo (dir), remote_name,
&gpg_verify, NULL);
@@ -160,15 +91,15 @@ xdg_app_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable,
}
if (options->len != 0)
table_printer_add_column (printer, options->str);
xdg_app_table_printer_add_column (printer, options->str);
g_string_truncate (options, 0);
table_printer_finish_row (printer);
xdg_app_table_printer_finish_row (printer);
}
table_printer_print (printer);
table_printer_free (printer);
xdg_app_table_printer_print (printer);
xdg_app_table_printer_free (printer);
}
else
{
+70
View File
@@ -742,3 +742,73 @@ xdg_app_mkstempat (int dir_fd,
errno = EEXIST;
return -1;
}
struct XdgAppTablePrinter {
GPtrArray *rows;
GPtrArray *current;
int n_columns;
};
XdgAppTablePrinter *
xdg_app_table_printer_new (void)
{
XdgAppTablePrinter *printer = g_new0 (XdgAppTablePrinter, 1);
printer->rows = g_ptr_array_new_with_free_func ((GDestroyNotify)g_strfreev);
printer->current = g_ptr_array_new_with_free_func (g_free);
return printer;
}
void
xdg_app_table_printer_free (XdgAppTablePrinter *printer)
{
g_ptr_array_free (printer->rows, TRUE);
g_ptr_array_free (printer->current, TRUE);
g_free (printer);
}
void
xdg_app_table_printer_add_column (XdgAppTablePrinter *printer,
const char *text)
{
g_ptr_array_add (printer->current, text ? g_strdup (text) : g_strdup (""));
}
void
xdg_app_table_printer_finish_row (XdgAppTablePrinter *printer)
{
printer->n_columns = MAX (printer->n_columns, printer->current->len);
g_ptr_array_add (printer->current, NULL);
g_ptr_array_add (printer->rows,
g_ptr_array_free (printer->current, FALSE));
printer->current = g_ptr_array_new_with_free_func (g_free);
}
void
xdg_app_table_printer_print (XdgAppTablePrinter *printer)
{
g_autofree int *widths = NULL;
int i, j;
if (printer->current->len != 0)
xdg_app_table_printer_finish_row (printer);
widths = g_new0 (int, printer->n_columns);
for (i = 0; i < printer->rows->len; i++)
{
char **row = g_ptr_array_index (printer->rows,i);
for (j = 0; row[j] != NULL; j++)
widths[j] = MAX (widths[j], strlen (row[j]));
}
for (i = 0; i < printer->rows->len; i++)
{
char **row = g_ptr_array_index (printer->rows,i);
for (j = 0; row[j] != NULL; j++)
g_print ("%-*s%s", widths[j], row[j], j == printer->n_columns - 1 ? "" : "\t");
g_print ("\n");
}
}
+10
View File
@@ -130,6 +130,16 @@ gint xdg_app_mkstempat (int dir_fd,
int mode);
typedef struct XdgAppTablePrinter XdgAppTablePrinter;
XdgAppTablePrinter *xdg_app_table_printer_new (void);
void xdg_app_table_printer_free (XdgAppTablePrinter *printer);
void xdg_app_table_printer_add_column (XdgAppTablePrinter *printer,
const char *text);
void xdg_app_table_printer_finish_row (XdgAppTablePrinter *printer);
void xdg_app_table_printer_print (XdgAppTablePrinter *printer);
#define AUTOLOCK(name) G_GNUC_UNUSED __attribute__((cleanup(xdg_app_auto_unlock_helper))) GMutex * G_PASTE(auto_unlock, __LINE__) = xdg_app_auto_lock_helper (&G_LOCK_NAME (name))
G_DEFINE_AUTOPTR_CLEANUP_FUNC(OstreeRepo, g_object_unref)