From cae219c4de3909123779ba759be6c0216564103f Mon Sep 17 00:00:00 2001 From: Juan RP Date: Wed, 1 Aug 2012 08:25:15 +0200 Subject: [PATCH] xbps-repo: indent pkgs from all repos with longest pkgver found. --- bin/xbps-bin/list.c | 2 +- bin/xbps-repo/defs.h | 2 ++ bin/xbps-repo/list.c | 42 +++++++++++++++++++++++++++++++++++------- bin/xbps-repo/main.c | 23 ++++++++++++----------- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/bin/xbps-bin/list.c b/bin/xbps-bin/list.c index 2bc55f78..c6e56a2d 100644 --- a/bin/xbps-bin/list.c +++ b/bin/xbps-bin/list.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2008-2011 Juan Romero Pardines. + * Copyright (c) 2008-2012 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without diff --git a/bin/xbps-repo/defs.h b/bin/xbps-repo/defs.h index c9caef5e..cd8f64ae 100644 --- a/bin/xbps-repo/defs.h +++ b/bin/xbps-repo/defs.h @@ -31,6 +31,7 @@ struct repo_search_data { int npatterns; char **patterns; + void *arg; size_t pkgver_len; size_t maxcols; }; @@ -54,6 +55,7 @@ void release_repo_lock(char **, int); int repo_find_files_in_packages(struct xbps_handle *, int, char **); /* From list.c */ +size_t repo_find_longest_pkgver(struct xbps_handle *); int repo_pkg_list_cb(struct xbps_handle *, struct xbps_rpool_index *, void *, diff --git a/bin/xbps-repo/list.c b/bin/xbps-repo/list.c index 9ce7cc5f..d8a9de93 100644 --- a/bin/xbps-repo/list.c +++ b/bin/xbps-repo/list.c @@ -34,6 +34,37 @@ #include "defs.h" #include "../xbps-bin/defs.h" +static int +repo_longest_pkgver(struct xbps_handle *xhp, + struct xbps_rpool_index *rpi, + void *arg, + bool *done) +{ + size_t *len = arg, olen = 0; + + (void)done; + + if (*len == 0) { + *len = find_longest_pkgver(xhp, rpi->repo); + return 0; + } + olen = find_longest_pkgver(xhp, rpi->repo); + if (olen > *len) + *len = olen; + + return 0; +} + +size_t +repo_find_longest_pkgver(struct xbps_handle *xhp) +{ + size_t len = 0; + + xbps_rpool_foreach(xhp, repo_longest_pkgver, &len); + + return len; +} + int repo_pkg_list_cb(struct xbps_handle *xhp, struct xbps_rpool_index *rpi, @@ -41,16 +72,16 @@ repo_pkg_list_cb(struct xbps_handle *xhp, bool *done) { struct list_pkgver_cb lpc; - const char *repo = arg; + struct repo_search_data *rsd = arg; (void)done; - if (repo && strcmp(rpi->uri, repo)) + if (rsd->arg && strcmp(rpi->uri, rsd->arg)) return 0; lpc.check_state = false; lpc.state = 0; - lpc.pkgver_len = find_longest_pkgver(xhp, rpi->repo); - lpc.maxcols = get_maxcols(); + lpc.pkgver_len = rsd->pkgver_len; + lpc.maxcols = rsd->maxcols; (void)xbps_callback_array_iter(xhp, rpi->repo, list_pkgs_in_dict, &lpc); @@ -82,9 +113,6 @@ repo_search_pkgs_cb(struct xbps_handle *xhp, struct repo_search_data *rsd = arg; (void)done; - rsd->maxcols = get_maxcols(); - rsd->pkgver_len = find_longest_pkgver(xhp, rpi->repo); - (void)xbps_callback_array_iter(xhp, rpi->repo, show_pkg_namedesc, rsd); return 0; diff --git a/bin/xbps-repo/main.c b/bin/xbps-repo/main.c index ea8283f8..8f3b3064 100644 --- a/bin/xbps-repo/main.c +++ b/bin/xbps-repo/main.c @@ -90,7 +90,7 @@ main(int argc, char **argv) { struct xbps_handle xh; struct xferstat xfer; - struct repo_search_data *rsd = NULL; + struct repo_search_data rsd; prop_dictionary_t pkgd; const char *rootdir, *cachedir, *conffile, *option; int flags = 0, c, rv = 0; @@ -170,7 +170,11 @@ main(int argc, char **argv) if (argc < 1 || argc > 2) usage(true); - rv = xbps_rpool_foreach(&xh, repo_pkg_list_cb, argv[1]); + rsd.arg = argv[1]; + rsd.pkgver_len = repo_find_longest_pkgver(&xh); + rsd.maxcols = get_maxcols(); + + rv = xbps_rpool_foreach(&xh, repo_pkg_list_cb, &rsd); if (rv == ENOTSUP) xbps_error_printf("xbps-repo: no repositories " "currently registered!\n"); @@ -185,15 +189,12 @@ main(int argc, char **argv) if (argc < 2) usage(true); - rsd = malloc(sizeof(*rsd)); - if (rsd == NULL) { - rv = ENOMEM; - goto out; - } - rsd->npatterns = argc; - rsd->patterns = argv; - rv = xbps_rpool_foreach(&xh, repo_search_pkgs_cb, rsd); - free(rsd); + rsd.npatterns = argc; + rsd.patterns = argv; + rsd.pkgver_len = repo_find_longest_pkgver(&xh); + rsd.maxcols = get_maxcols(); + + rv = xbps_rpool_foreach(&xh, repo_search_pkgs_cb, &rsd); if (rv == ENOTSUP) xbps_error_printf("xbps-repo: no repositories " "currently registered!\n");