From 9c3cb200d4998b5b71d4ca707486f2db3cf2ee97 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Mon, 11 Feb 2013 11:04:25 +0100 Subject: [PATCH] xbps-query: align correctly -l results (state wasn't taken into account). --- bin/xbps-query/list.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/bin/xbps-query/list.c b/bin/xbps-query/list.c index 7f27c2c0..b11a25dd 100644 --- a/bin/xbps-query/list.c +++ b/bin/xbps-query/list.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2008-2012 Juan Romero Pardines. + * Copyright (c) 2008-2013 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -58,7 +58,7 @@ list_pkgs_in_dict(struct xbps_handle *xhp, struct list_pkgver_cb *lpc = arg; const char *pkgver, *short_desc, *state_str; char tmp[255], *out = NULL; - size_t i, len = 0, maxcols; + size_t i, len = 0; pkg_state_t state; (void)xhp; @@ -85,13 +85,14 @@ list_pkgs_in_dict(struct xbps_handle *xhp, tmp[i] = ' '; tmp[i] = '\0'; - maxcols = lpc->maxcols - 4; - len = strlen(tmp) + strlen(short_desc); - if (len > maxcols) { - out = malloc(lpc->maxcols); + len = strlen(tmp) + strlen(short_desc) + 2; + if (len > lpc->maxcols) { + out = malloc(lpc->maxcols+1); assert(out); - snprintf(out, maxcols, "%s %s", tmp, short_desc); - printf("%s...\n", out); + snprintf(out, lpc->maxcols - 3, + "%s %s", tmp, short_desc); + strncat(out, "...\n", lpc->maxcols); + printf("%s", out); free(out); } else { printf("%s %s\n", tmp, short_desc); @@ -154,7 +155,7 @@ list_pkgs_pkgdb(struct xbps_handle *xhp) { struct list_pkgver_cb lpc; - lpc.pkgver_len = find_longest_pkgver(xhp, NULL); + lpc.pkgver_len = find_longest_pkgver(xhp, NULL) + 3; /* for state */ lpc.maxcols = get_maxcols(); return xbps_pkgdb_foreach_cb(xhp, list_pkgs_in_dict, &lpc); @@ -200,6 +201,7 @@ _find_longest_pkgver_cb(struct xbps_handle *xhp, struct fflongest *ffl = arg; prop_dictionary_t pkgd; const char *pkgver; + size_t len; (void)xhp; (void)loop_done; @@ -210,8 +212,9 @@ _find_longest_pkgver_cb(struct xbps_handle *xhp, pkgd = obj; prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); - if (ffl->len == 0 || strlen(pkgver) > ffl->len) - ffl->len = strlen(pkgver); + len = strlen(pkgver); + if (ffl->len == 0 || len > ffl->len) + ffl->len = len; return 0; }