From 1be90e57d78960fdea2637e8ba72fa00f3ac6aab Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 31 Jul 2012 17:50:50 +0200 Subject: [PATCH] xbps-{bin,repo}: wrap long lines for list, pkg-list and search targets. Fixes issue #26 --- bin/xbps-bin/defs.h | 14 ++++++++------ bin/xbps-bin/list.c | 16 +++++++++++++--- bin/xbps-bin/main.c | 20 ++++++++++++++------ bin/xbps-bin/transaction.c | 34 +++++++++++++++++++--------------- bin/xbps-bin/util.c | 20 ++++++++++++-------- bin/xbps-repo/defs.h | 1 + bin/xbps-repo/list.c | 8 ++++---- bin/xbps-repo/show.c | 25 ++++++++++++++++++------- 8 files changed, 89 insertions(+), 49 deletions(-) diff --git a/bin/xbps-bin/defs.h b/bin/xbps-bin/defs.h index 77de09f8..3d8d7fb3 100644 --- a/bin/xbps-bin/defs.h +++ b/bin/xbps-bin/defs.h @@ -41,16 +41,17 @@ struct xferstat { struct list_pkgver_cb { pkg_state_t state; size_t pkgver_len; + size_t maxcols; bool check_state; }; /* from transaction.c */ int install_new_pkg(struct xbps_handle *, const char *, bool); int update_pkg(struct xbps_handle *, const char *); -int remove_pkg(struct xbps_handle *, const char *, bool); -int remove_pkg_orphans(struct xbps_handle *, bool, bool); -int dist_upgrade(struct xbps_handle *, bool, bool, bool); -int exec_transaction(struct xbps_handle *, bool, bool, bool); +int remove_pkg(struct xbps_handle *, const char *, size_t, bool); +int remove_pkg_orphans(struct xbps_handle *, size_t, bool, bool); +int dist_upgrade(struct xbps_handle *, size_t, bool, bool, bool); +int exec_transaction(struct xbps_handle *, size_t, bool, bool, bool); /* from remove.c */ int remove_installed_pkgs(int, char **, bool, bool, bool, bool); @@ -116,8 +117,9 @@ int list_strings_sep_in_array(struct xbps_handle *, prop_object_t, void *, bool *); -size_t find_longest_pkgver(struct xbps_handle *, prop_object_t); -void print_package_line(const char *, bool); +size_t find_longest_pkgver(struct xbps_handle *, prop_object_t); +void print_package_line(const char *, size_t, bool); +size_t get_maxcols(void); /* from list.c */ int list_pkgs_in_dict(struct xbps_handle *, prop_object_t, void *, bool *); diff --git a/bin/xbps-bin/list.c b/bin/xbps-bin/list.c index 71f67a05..2bc55f78 100644 --- a/bin/xbps-bin/list.c +++ b/bin/xbps-bin/list.c @@ -40,9 +40,9 @@ list_pkgs_in_dict(struct xbps_handle *xhp, { struct list_pkgver_cb *lpc = arg; const char *pkgver, *short_desc, *arch; - char *tmp = NULL; + char *tmp = NULL, *out = NULL; pkg_state_t curstate; - size_t i = 0; + size_t i, len = 0; bool chkarch; (void)xhp; @@ -78,7 +78,17 @@ list_pkgs_in_dict(struct xbps_handle *xhp, tmp[i] = ' '; tmp[i] = '\0'; - printf("%s %s\n", tmp, short_desc); + len = strlen(tmp) + strlen(short_desc) + 1; + if (len > lpc->maxcols) { + out = malloc(lpc->maxcols); + assert(out); + snprintf(out, lpc->maxcols-2, "%s %s", tmp, short_desc); + strncat(out, "...", lpc->maxcols); + printf("%s\n", out); + free(out); + } else { + printf("%s %s\n", tmp, short_desc); + } free(tmp); return 0; diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index 738f2197..e8d7be76 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -115,12 +115,14 @@ main(int argc, char **argv) int i, c, flags, rv; bool rsync, yes, reqby_force, force_rm_with_deps, recursive_rm; bool reinstall, show_download_pkglist_url, dry_run; + size_t maxcols; rootdir = cachedir = conffile = option = NULL; flags = rv = 0; reqby_force = rsync = yes = dry_run = force_rm_with_deps = false; recursive_rm = reinstall = show_download_pkglist_url = false; + while ((c = getopt(argc, argv, "AC:c:dDFfhMno:Rr:SVvy")) != -1) { switch (c) { case 'A': @@ -228,6 +230,8 @@ main(int argc, char **argv) sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); + maxcols = get_maxcols(); + if (strcasecmp(argv[0], "list") == 0) { /* Lists packages currently registered in database. */ if (argc < 1 || argc > 2) @@ -258,6 +262,7 @@ main(int argc, char **argv) * Find the longest pkgver string to pretty print the output. */ lpc.pkgver_len = find_longest_pkgver(&xh, NULL); + lpc.maxcols = maxcols; rv = xbps_pkgdb_foreach_cb(&xh, list_pkgs_in_dict, &lpc); if (rv == ENOENT) { printf("No packages currently registered.\n"); @@ -276,7 +281,8 @@ main(int argc, char **argv) if ((rv = install_new_pkg(&xh, argv[i], reinstall)) != 0) goto out; - rv = exec_transaction(&xh, yes, dry_run, show_download_pkglist_url); + rv = exec_transaction(&xh, maxcols, yes, dry_run, + show_download_pkglist_url); } else if (strcasecmp(argv[0], "update") == 0) { /* Update an installed package. */ @@ -290,7 +296,8 @@ main(int argc, char **argv) if ((rv = update_pkg(&xh, argv[i])) != 0) goto out; - rv = exec_transaction(&xh, yes, dry_run, show_download_pkglist_url); + rv = exec_transaction(&xh, yes, maxcols, dry_run, + show_download_pkglist_url); } else if (strcasecmp(argv[0], "remove") == 0) { /* Removes a package. */ @@ -298,7 +305,7 @@ main(int argc, char **argv) usage(true); for (i = 1; i < argc; i++) { - rv = remove_pkg(&xh, argv[i], recursive_rm); + rv = remove_pkg(&xh, argv[i], maxcols, recursive_rm); if (rv == 0) continue; else if (rv != EEXIST) @@ -310,7 +317,7 @@ main(int argc, char **argv) rv = EINVAL; goto out; } - rv = exec_transaction(&xh, yes, dry_run, false); + rv = exec_transaction(&xh, maxcols, yes, dry_run, false); } else if (strcasecmp(argv[0], "show") == 0) { /* Shows info about an installed binary package. */ @@ -355,7 +362,8 @@ main(int argc, char **argv) if (rsync && ((rv = xbps_rpool_sync(&xh, NULL)) != 0)) goto out; - rv = dist_upgrade(&xh, yes, dry_run, show_download_pkglist_url); + rv = dist_upgrade(&xh, maxcols, yes, dry_run, + show_download_pkglist_url); } else if (strcasecmp(argv[0], "show-orphans") == 0) { /* @@ -377,7 +385,7 @@ main(int argc, char **argv) if (argc != 1) usage(true); - rv = remove_pkg_orphans(&xh, yes, dry_run); + rv = remove_pkg_orphans(&xh, maxcols, yes, dry_run); } else if (strcasecmp(argv[0], "reconfigure") == 0) { /* diff --git a/bin/xbps-bin/transaction.c b/bin/xbps-bin/transaction.c index e31c5626..2628efa9 100644 --- a/bin/xbps-bin/transaction.c +++ b/bin/xbps-bin/transaction.c @@ -135,7 +135,7 @@ show_binpkgs_url(struct xbps_handle *xhp, prop_object_iterator_t iter) } static void -show_package_list(prop_object_iterator_t iter, const char *match) +show_package_list(prop_object_iterator_t iter, const char *match, size_t cols) { prop_object_t obj; const char *pkgver, *tract; @@ -145,14 +145,14 @@ show_package_list(prop_object_iterator_t iter, const char *match) prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract); if (strcmp(match, tract)) continue; - print_package_line(pkgver, false); + print_package_line(pkgver, cols, false); } prop_object_iterator_reset(iter); - print_package_line(NULL, true); + print_package_line(NULL, cols, true); } static int -show_transaction_sizes(struct transaction *trans) +show_transaction_sizes(struct transaction *trans, size_t cols) { uint64_t dlsize = 0, instsize = 0, rmsize = 0; char size[8]; @@ -164,28 +164,28 @@ show_transaction_sizes(struct transaction *trans) &trans->inst_pkgcnt)) { printf("%u package%s will be installed:\n", trans->inst_pkgcnt, trans->inst_pkgcnt == 1 ? "" : "s"); - show_package_list(trans->iter, "install"); + show_package_list(trans->iter, "install", cols); printf("\n"); } if (prop_dictionary_get_uint32(trans->d, "total-update-pkgs", &trans->up_pkgcnt)) { printf("%u package%s will be updated:\n", trans->up_pkgcnt, trans->up_pkgcnt == 1 ? "" : "s"); - show_package_list(trans->iter, "update"); + show_package_list(trans->iter, "update", cols); printf("\n"); } if (prop_dictionary_get_uint32(trans->d, "total-configure-pkgs", &trans->cf_pkgcnt)) { printf("%u package%s will be configured:\n", trans->cf_pkgcnt, trans->cf_pkgcnt == 1 ? "" : "s"); - show_package_list(trans->iter, "configure"); + show_package_list(trans->iter, "configure", cols); printf("\n"); } if (prop_dictionary_get_uint32(trans->d, "total-remove-pkgs", &trans->rm_pkgcnt)) { printf("%u package%s will be removed:\n", trans->rm_pkgcnt, trans->rm_pkgcnt == 1 ? "" : "s"); - show_package_list(trans->iter, "remove"); + show_package_list(trans->iter, "remove", cols); printf("\n"); } /* @@ -227,6 +227,7 @@ show_transaction_sizes(struct transaction *trans) int dist_upgrade(struct xbps_handle *xhp, + size_t cols, bool yes, bool dry_run, bool show_download_pkglist_url) @@ -254,11 +255,12 @@ dist_upgrade(struct xbps_handle *xhp, return -1; } } - return exec_transaction(xhp, yes, dry_run, show_download_pkglist_url); + return exec_transaction(xhp, cols, yes, dry_run, + show_download_pkglist_url); } int -remove_pkg_orphans(struct xbps_handle *xhp, bool yes, bool dry_run) +remove_pkg_orphans(struct xbps_handle *xhp, size_t cols, bool yes, bool dry_run) { int rv; @@ -272,7 +274,7 @@ remove_pkg_orphans(struct xbps_handle *xhp, bool yes, bool dry_run) return rv; } } - return exec_transaction(xhp, yes, dry_run, false); + return exec_transaction(xhp, cols, yes, dry_run, false); } int @@ -323,7 +325,8 @@ update_pkg(struct xbps_handle *xhp, const char *pkgname) } int -remove_pkg(struct xbps_handle *xhp, const char *pkgname, bool recursive) +remove_pkg(struct xbps_handle *xhp, const char *pkgname, size_t cols, + bool recursive) { prop_dictionary_t pkgd; prop_array_t reqby; @@ -342,10 +345,10 @@ remove_pkg(struct xbps_handle *xhp, const char *pkgname, bool recursive) prop_array_count(reqby) > 1 ? "S" : ""); for (x = 0; x < prop_array_count(reqby); x++) { prop_array_get_cstring_nocopy(reqby, x, &pkgver); - print_package_line(pkgver, false); + print_package_line(pkgver, cols, false); } printf("\n\n"); - print_package_line(NULL, true); + print_package_line(NULL, cols, true); return rv; } else if (rv == ENOENT) { printf("Package `%s' is not currently installed.\n", pkgname); @@ -361,6 +364,7 @@ remove_pkg(struct xbps_handle *xhp, const char *pkgname, bool recursive) int exec_transaction(struct xbps_handle *xhp, + size_t maxcols, bool yes, bool dry_run, bool show_download_urls) @@ -419,7 +423,7 @@ exec_transaction(struct xbps_handle *xhp, /* * Show download/installed size for the transaction. */ - if ((rv = show_transaction_sizes(trans)) != 0) + if ((rv = show_transaction_sizes(trans, maxcols)) != 0) goto out; /* * Ask interactively (if -y not set). diff --git a/bin/xbps-bin/util.c b/bin/xbps-bin/util.c index 7a2bac09..f9d73955 100644 --- a/bin/xbps-bin/util.c +++ b/bin/xbps-bin/util.c @@ -216,18 +216,22 @@ list_strings_sep_in_array(struct xbps_handle *xhp, return 0; } +size_t +get_maxcols(void) +{ + struct winsize ws; + + if (ioctl(1, TIOCGWINSZ, &ws) == 0) + return ws.ws_col; + + return 80; +} + void -print_package_line(const char *str, bool reset) +print_package_line(const char *str, size_t maxcols, bool reset) { static size_t cols; static bool first; - struct winsize ws; - size_t maxcols; - - if (ioctl(1, TIOCGWINSZ, &ws) == 0) - maxcols = ws.ws_col; - else - maxcols = 80; if (reset) { cols = 0; diff --git a/bin/xbps-repo/defs.h b/bin/xbps-repo/defs.h index 59cc1fec..c9caef5e 100644 --- a/bin/xbps-repo/defs.h +++ b/bin/xbps-repo/defs.h @@ -32,6 +32,7 @@ struct repo_search_data { int npatterns; char **patterns; size_t pkgver_len; + size_t maxcols; }; /* From common.c */ diff --git a/bin/xbps-repo/list.c b/bin/xbps-repo/list.c index 94f10bda..9ce7cc5f 100644 --- a/bin/xbps-repo/list.c +++ b/bin/xbps-repo/list.c @@ -50,11 +50,10 @@ repo_pkg_list_cb(struct xbps_handle *xhp, lpc.check_state = false; lpc.state = 0; lpc.pkgver_len = find_longest_pkgver(xhp, rpi->repo); - - if (arg == NULL) - printf("From %s repository ...\n", rpi->uri); + lpc.maxcols = get_maxcols(); (void)xbps_callback_array_iter(xhp, rpi->repo, list_pkgs_in_dict, &lpc); + return 0; } @@ -83,9 +82,10 @@ 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); - printf("From %s repository ...\n", rpi->uri); (void)xbps_callback_array_iter(xhp, rpi->repo, show_pkg_namedesc, rsd); + return 0; } diff --git a/bin/xbps-repo/show.c b/bin/xbps-repo/show.c index bd84e44b..19bd33a2 100644 --- a/bin/xbps-repo/show.c +++ b/bin/xbps-repo/show.c @@ -100,9 +100,9 @@ show_pkg_namedesc(struct xbps_handle *xhp, bool *loop_done) { struct repo_search_data *rsd = arg; - const char *pkgver, *pkgname, *desc, *arch; - char *tmp = NULL; - size_t i, x; + const char *pkgver, *pkgname, *desc, *arch, *inststr; + char *tmp = NULL, *out = NULL; + size_t i, x, len; (void)xhp; (void)loop_done; @@ -129,11 +129,22 @@ show_pkg_namedesc(struct xbps_handle *xhp, tmp[x] = '\0'; if (xbps_pkgdb_get_pkgd_by_pkgver(xhp, pkgver)) - printf(" * "); + inststr = "[*] "; else - printf(" "); - printf("%s %s\n", tmp, desc); - free(tmp); + inststr = "[ ] "; + + len = strlen(inststr) + strlen(tmp) + strlen(desc) + 1; + if (len > rsd->maxcols) { + out = malloc(rsd->maxcols); + assert(out); + snprintf(out, rsd->maxcols-2, "%s%s %s", + inststr, tmp, desc); + strncat(out, "...", rsd->maxcols); + printf("%s\n", out); + free(out); + } else { + printf("%s%s %s\n", inststr, tmp, desc); + } } }