libxbps::xbps_humanize_number(): use common values and hide implementation details from API.
So now its prototype is the following: int xbps_humanize_number(char *buf, int64_t bytes) It is a wrapper around NetBSD's humanize_number(3) which uses 6 digits for max length, HN_AUTOSCALE and HN_NOSPACE|HN_DECIMAL. All users have been updated.
This commit is contained in:
parent
e2b55c90be
commit
992e8c6a14
@ -195,7 +195,7 @@ show_transaction_sizes(struct transaction *trans)
|
|||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
uint64_t dlsize = 0, instsize = 0;
|
uint64_t dlsize = 0, instsize = 0;
|
||||||
const char *tract;
|
const char *tract;
|
||||||
char size[64];
|
char size[8];
|
||||||
bool trans_inst, trans_up, trans_conf;
|
bool trans_inst, trans_up, trans_conf;
|
||||||
|
|
||||||
trans_inst = trans_up = trans_conf = false;
|
trans_inst = trans_up = trans_conf = false;
|
||||||
@ -240,15 +240,13 @@ show_transaction_sizes(struct transaction *trans)
|
|||||||
prop_dictionary_get_uint64(trans->dict, "total-download-size", &dlsize);
|
prop_dictionary_get_uint64(trans->dict, "total-download-size", &dlsize);
|
||||||
prop_dictionary_get_uint64(trans->dict, "total-installed-size",
|
prop_dictionary_get_uint64(trans->dict, "total-installed-size",
|
||||||
&instsize);
|
&instsize);
|
||||||
if (xbps_humanize_number(size, 5, (int64_t)dlsize,
|
if (xbps_humanize_number(size, (int64_t)dlsize) == -1) {
|
||||||
"", HN_AUTOSCALE, HN_B|HN_DECIMAL|HN_NOSPACE) == -1) {
|
|
||||||
fprintf(stderr, "xbps-bin: error: humanize_number returns "
|
fprintf(stderr, "xbps-bin: error: humanize_number returns "
|
||||||
"%s\n", strerror(errno));
|
"%s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
printf("Total download size: %sB\n", size);
|
printf("Total download size: %sB\n", size);
|
||||||
if (xbps_humanize_number(size, 5, (int64_t)instsize,
|
if (xbps_humanize_number(size, (int64_t)instsize) == -1) {
|
||||||
"", HN_AUTOSCALE, HN_B|HN_DECIMAL|HN_NOSPACE) == -1) {
|
|
||||||
fprintf(stderr, "xbps-bin: error: humanize_number2 returns "
|
fprintf(stderr, "xbps-bin: error: humanize_number2 returns "
|
||||||
"%s\n", strerror(errno));
|
"%s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -39,17 +39,16 @@ void
|
|||||||
show_pkg_info_only_repo(prop_dictionary_t dict)
|
show_pkg_info_only_repo(prop_dictionary_t dict)
|
||||||
{
|
{
|
||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
char size[64];
|
char size[8];
|
||||||
int rv = 0;
|
int rv;
|
||||||
|
|
||||||
obj = prop_dictionary_get(dict, "filename");
|
obj = prop_dictionary_get(dict, "filename");
|
||||||
if (prop_object_type(obj) == PROP_TYPE_STRING) {
|
if (prop_object_type(obj) == PROP_TYPE_STRING) {
|
||||||
printf("Filename: %s", prop_string_cstring_nocopy(obj));
|
printf("Filename: %s", prop_string_cstring_nocopy(obj));
|
||||||
obj = prop_dictionary_get(dict, "filename-size");
|
obj = prop_dictionary_get(dict, "filename-size");
|
||||||
if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
|
if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
|
||||||
rv = xbps_humanize_number(size, 5,
|
rv = xbps_humanize_number(size,
|
||||||
(int64_t)prop_number_unsigned_integer_value(obj),
|
(int64_t)prop_number_unsigned_integer_value(obj));
|
||||||
"", HN_AUTOSCALE, HN_B|HN_DECIMAL|HN_NOSPACE);
|
|
||||||
if (rv == -1)
|
if (rv == -1)
|
||||||
printf(" (size: %ju)\n",
|
printf(" (size: %ju)\n",
|
||||||
prop_number_unsigned_integer_value(obj));
|
prop_number_unsigned_integer_value(obj));
|
||||||
@ -69,8 +68,7 @@ show_pkg_info(prop_dictionary_t dict)
|
|||||||
{
|
{
|
||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
const char *sep;
|
const char *sep;
|
||||||
char size[64];
|
char size[8];
|
||||||
int rv = 0;
|
|
||||||
|
|
||||||
assert(dict != NULL);
|
assert(dict != NULL);
|
||||||
assert(prop_dictionary_count(dict) != 0);
|
assert(prop_dictionary_count(dict) != 0);
|
||||||
@ -87,10 +85,8 @@ show_pkg_info(prop_dictionary_t dict)
|
|||||||
obj = prop_dictionary_get(dict, "installed_size");
|
obj = prop_dictionary_get(dict, "installed_size");
|
||||||
if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
|
if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
|
||||||
printf("Installed size: ");
|
printf("Installed size: ");
|
||||||
rv = xbps_humanize_number(size, 5,
|
if (xbps_humanize_number(size,
|
||||||
(int64_t)prop_number_unsigned_integer_value(obj),
|
(int64_t)prop_number_unsigned_integer_value(obj)) == -1)
|
||||||
"", HN_AUTOSCALE, HN_B|HN_DECIMAL|HN_NOSPACE);
|
|
||||||
if (rv == -1)
|
|
||||||
printf("%ju\n",
|
printf("%ju\n",
|
||||||
prop_number_unsigned_integer_value(obj));
|
prop_number_unsigned_integer_value(obj));
|
||||||
else
|
else
|
||||||
|
@ -217,15 +217,7 @@ const char *xbps_fetch_error_string(void);
|
|||||||
|
|
||||||
/*@}*/
|
/*@}*/
|
||||||
|
|
||||||
/* From lib/humanize_number.c */
|
int xbps_humanize_number(char *, int64_t);
|
||||||
#define HN_DECIMAL 0x01
|
|
||||||
#define HN_NOSPACE 0x02
|
|
||||||
#define HN_B 0x04
|
|
||||||
#define HN_DIVISOR_1000 0x08
|
|
||||||
#define HN_GETSCALE 0x10
|
|
||||||
#define HN_AUTOSCALE 0x20
|
|
||||||
|
|
||||||
int xbps_humanize_number(char *, size_t, int64_t, const char *, int, int);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ingroup dircreate
|
* @ingroup dircreate
|
||||||
|
@ -106,7 +106,7 @@ static const char *
|
|||||||
stat_bps(struct xferstat *xsp)
|
stat_bps(struct xferstat *xsp)
|
||||||
{
|
{
|
||||||
static char str[16];
|
static char str[16];
|
||||||
char size[32];
|
char size[8];
|
||||||
double delta, bps;
|
double delta, bps;
|
||||||
|
|
||||||
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
|
delta = (xsp->last.tv_sec + (xsp->last.tv_usec / 1.e6))
|
||||||
@ -115,9 +115,8 @@ stat_bps(struct xferstat *xsp)
|
|||||||
snprintf(str, sizeof str, "-- stalled --");
|
snprintf(str, sizeof str, "-- stalled --");
|
||||||
} else {
|
} else {
|
||||||
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
|
bps = ((double)(xsp->rcvd - xsp->offset) / delta);
|
||||||
(void)xbps_humanize_number(size, 6, (int64_t)bps, "",
|
(void)xbps_humanize_number(size, (int64_t)bps);
|
||||||
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
snprintf(str, sizeof str, "%s/s", size);
|
||||||
snprintf(str, sizeof str, "%sB/s", size);
|
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -129,18 +128,16 @@ static void
|
|||||||
stat_display(struct xferstat *xsp)
|
stat_display(struct xferstat *xsp)
|
||||||
{
|
{
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
char totsize[32], recvsize[32];
|
char totsize[8], recvsize[8];
|
||||||
|
|
||||||
gettimeofday(&now, NULL);
|
gettimeofday(&now, NULL);
|
||||||
if (now.tv_sec <= xsp->last.tv_sec)
|
if (now.tv_sec <= xsp->last.tv_sec)
|
||||||
return;
|
return;
|
||||||
xsp->last = now;
|
xsp->last = now;
|
||||||
|
|
||||||
(void)xbps_humanize_number(totsize, 7, (int64_t)xsp->size, "",
|
(void)xbps_humanize_number(totsize, (int64_t)xsp->size);
|
||||||
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
(void)xbps_humanize_number(recvsize, (int64_t)xsp->rcvd);
|
||||||
(void)xbps_humanize_number(recvsize, 7, (int64_t)xsp->rcvd, "",
|
fprintf(stderr, "\r%s: %s [%d%% of %s]",
|
||||||
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
|
||||||
fprintf(stderr, "\r%s: %sB [%d%% of %sB]",
|
|
||||||
xsp->name, recvsize,
|
xsp->name, recvsize,
|
||||||
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size),
|
(int)((double)(100.0 * (double)xsp->rcvd) / (double)xsp->size),
|
||||||
totsize);
|
totsize);
|
||||||
@ -181,11 +178,10 @@ stat_update(struct xferstat *xsp, off_t rcvd)
|
|||||||
static void
|
static void
|
||||||
stat_end(struct xferstat *xsp)
|
stat_end(struct xferstat *xsp)
|
||||||
{
|
{
|
||||||
char size[32];
|
char size[8];
|
||||||
|
|
||||||
(void)xbps_humanize_number(size, 6, (int64_t)xsp->size, "",
|
(void)xbps_humanize_number(size, (int64_t)xsp->size);
|
||||||
HN_AUTOSCALE, HN_NOSPACE|HN_DECIMAL);
|
fprintf(stderr, "\rDownloaded %s for %s [avg rate: %s]",
|
||||||
fprintf(stderr, "\rDownloaded %sB for %s [avg rate: %s]",
|
|
||||||
size, xsp->name, stat_bps(xsp));
|
size, xsp->name, stat_bps(xsp));
|
||||||
fprintf(stderr, "\033[K\n");
|
fprintf(stderr, "\033[K\n");
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,15 @@
|
|||||||
|
|
||||||
#include <xbps_api.h>
|
#include <xbps_api.h>
|
||||||
|
|
||||||
int
|
#define HN_DECIMAL 0x01
|
||||||
xbps_humanize_number(char *buf, size_t len, int64_t bytes,
|
#define HN_NOSPACE 0x02
|
||||||
|
#define HN_B 0x04
|
||||||
|
#define HN_DIVISOR_1000 0x08
|
||||||
|
#define HN_GETSCALE 0x10
|
||||||
|
#define HN_AUTOSCALE 0x20
|
||||||
|
|
||||||
|
static int
|
||||||
|
humanize_number(char *buf, size_t len, int64_t bytes,
|
||||||
const char *suffix, int scale, int flags)
|
const char *suffix, int scale, int flags)
|
||||||
{
|
{
|
||||||
const char *prefixes, *sep;
|
const char *prefixes, *sep;
|
||||||
@ -141,3 +148,14 @@ xbps_humanize_number(char *buf, size_t len, int64_t bytes,
|
|||||||
|
|
||||||
return (r);
|
return (r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Small wrapper for NetBSD's humanize_number(3) with some
|
||||||
|
* defaults set that we care about.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
xbps_humanize_number(char *buf, int64_t bytes)
|
||||||
|
{
|
||||||
|
return humanize_number(buf, 6, bytes, "B",
|
||||||
|
HN_AUTOSCALE, HN_DECIMAL|HN_NOSPACE);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user