From 835bb311b37edd38f03b3d52ef716732abb5f000 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Thu, 4 Jan 2018 19:13:27 +0000 Subject: [PATCH] move mutt_pretty_size to libmutt --- browser.c | 2 +- compose.c | 2 +- curs_lib.c | 4 ++-- handler.c | 4 ++-- hdrline.c | 2 +- mutt/string.c | 32 ++++++++++++++++++++++++++++++++ mutt/string2.h | 1 + muttlib.c | 20 -------------------- protos.h | 1 - recvattach.c | 2 +- status.c | 4 ++-- 11 files changed, 43 insertions(+), 31 deletions(-) diff --git a/browser.c b/browser.c index 847d52d37df..0aa21a7b21a 100644 --- a/browser.c +++ b/browser.c @@ -485,7 +485,7 @@ static const char *folder_format_str(char *buf, size_t buflen, size_t col, int c case 's': if (folder->ff->local) { - mutt_pretty_size(fn, sizeof(fn), folder->ff->size); + mutt_str_pretty_size(fn, sizeof(fn), folder->ff->size); snprintf(fmt, sizeof(fmt), "%%%ss", prec); snprintf(buf, buflen, fmt, fn); } diff --git a/compose.c b/compose.c index d22bfc9a1dc..c0c85ec5d07 100644 --- a/compose.c +++ b/compose.c @@ -734,7 +734,7 @@ static const char *compose_format_str(char *buf, size_t buflen, size_t col, int case 'l': /* approx length of current message in bytes */ snprintf(fmt, sizeof(fmt), "%%%ss", prec); - mutt_pretty_size(tmp, sizeof(tmp), menu ? cum_attachs_size(menu) : 0); + mutt_str_pretty_size(tmp, sizeof(tmp), menu ? cum_attachs_size(menu) : 0); snprintf(buf, buflen, fmt, tmp); break; diff --git a/curs_lib.c b/curs_lib.c index fb9c4c582c7..6280451b127 100644 --- a/curs_lib.c +++ b/curs_lib.c @@ -465,7 +465,7 @@ void mutt_progress_init(struct Progress *progress, const char *msg, if (progress->size) { if (progress->flags & MUTT_PROGRESS_SIZE) - mutt_pretty_size(progress->sizestr, sizeof(progress->sizestr), progress->size); + mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr), progress->size); else snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size); } @@ -582,7 +582,7 @@ void mutt_progress_update(struct Progress *progress, long pos, int percent) if (progress->flags & MUTT_PROGRESS_SIZE) { pos = pos / (progress->inc << 10) * (progress->inc << 10); - mutt_pretty_size(posstr, sizeof(posstr), pos); + mutt_str_pretty_size(posstr, sizeof(posstr), pos); } else snprintf(posstr, sizeof(posstr), "%ld", pos); diff --git a/handler.c b/handler.c index b20fd709871..6706ae08b09 100644 --- a/handler.c +++ b/handler.c @@ -76,7 +76,7 @@ const int IndexHex[128] = { static void print_part_line(struct State *s, struct Body *b, int n) { char length[5]; - mutt_pretty_size(length, sizeof(length), b->length); + mutt_str_pretty_size(length, sizeof(length), b->length); state_mark_attach(s); char *charset = mutt_param_get("charset", b->parameter); if (n != 0) @@ -1526,7 +1526,7 @@ static int external_body_handler(struct Body *b, struct State *s) length = mutt_param_get("length", b->parameter); if (length) { - mutt_pretty_size(pretty_size, sizeof(pretty_size), strtol(length, NULL, 10)); + mutt_str_pretty_size(pretty_size, sizeof(pretty_size), strtol(length, NULL, 10)); state_printf(s, _("(size %s bytes) "), pretty_size); } state_puts(_("has been deleted --]\n"), s); diff --git a/hdrline.c b/hdrline.c index 70e03095863..c38e6e797bb 100644 --- a/hdrline.c +++ b/hdrline.c @@ -575,7 +575,7 @@ static const char *index_format_str(char *buf, size_t buflen, size_t col, int co case 'c': colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_SIZE); - mutt_pretty_size(tmp, sizeof(tmp), (long) hdr->content->length); + mutt_str_pretty_size(tmp, sizeof(tmp), (long) hdr->content->length); mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp); add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX); break; diff --git a/mutt/string.c b/mutt/string.c index 3db74db58e8..ffa4010ddd2 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -869,3 +869,35 @@ const char *mutt_str_find_word(const char *src) p++; return p; } + +/** + * mutt_str_pretty_size - Display an abbreviated size, e.g. 3.4K + * @param buf Buffer for the result + * @param buflen Length of the buffer + * @param num Number to abbreviate + */ +void mutt_str_pretty_size(char *buf, size_t buflen, size_t num) +{ + if (num < 1000) + { + snprintf(buf, buflen, "%d", (int) num); + } + else if (num < 10189) /* 0.1K - 9.9K */ + { + snprintf(buf, buflen, "%3.1fK", (num < 103) ? 0.1 : (num / 1024.0)); + } + else if (num < 1023949) /* 10K - 999K */ + { + /* 51 is magic which causes 10189/10240 to be rounded up to 10 */ + snprintf(buf, buflen, "%zuK", (num + 51) / 1024); + } + else if (num < 10433332) /* 1.0M - 9.9M */ + { + snprintf(buf, buflen, "%3.1fM", num / 1048576.0); + } + else /* 10M+ */ + { + /* (10433332 + 52428) / 1048576 = 10 */ + snprintf(buf, buflen, "%zuM", (num + 52428) / 1048576); + } +} diff --git a/mutt/string2.h b/mutt/string2.h index 17890d92d6b..081ccc7e953 100644 --- a/mutt/string2.h +++ b/mutt/string2.h @@ -73,6 +73,7 @@ int mutt_str_is_email_wsp(char c); size_t mutt_str_lws_len(const char *s, size_t n); size_t mutt_str_lws_rlen(const char *s, size_t n); const char *mutt_str_next_word(const char *s); +void mutt_str_pretty_size(char *buf, size_t buflen, size_t num); void mutt_str_remove_trailing_ws(char *s); void mutt_str_replace(char **p, const char *s); const char *mutt_str_rstrnstr(const char *haystack, size_t haystack_length, const char *needle); diff --git a/muttlib.c b/muttlib.c index 599ad43f0b9..993f0e27c24 100644 --- a/muttlib.c +++ b/muttlib.c @@ -566,26 +566,6 @@ void mutt_pretty_mailbox(char *s, size_t buflen) } } -void mutt_pretty_size(char *s, size_t len, size_t n) -{ - if (n < 1000) - snprintf(s, len, "%d", (int) n); - else if (n < 10189) /* 0.1K - 9.9K */ - snprintf(s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0); - else if (n < 1023949) /* 10K - 999K */ - { - /* 51 is magic which causes 10189/10240 to be rounded up to 10 */ - snprintf(s, len, "%zuK", (n + 51) / 1024); - } - else if (n < 10433332) /* 1.0M - 9.9M */ - snprintf(s, len, "%3.1fM", n / 1048576.0); - else /* 10M+ */ - { - /* (10433332 + 52428) / 1048576 = 10 */ - snprintf(s, len, "%zuM", (n + 52428) / 1048576); - } -} - void mutt_expand_file_fmt(char *dest, size_t destlen, const char *fmt, const char *src) { char tmp[LONG_STRING]; diff --git a/protos.h b/protos.h index b891ac72364..5cbb97ac9d6 100644 --- a/protos.h +++ b/protos.h @@ -199,7 +199,6 @@ void mutt_perror_debug(const char *s); void mutt_prepare_envelope(struct Envelope *env, int final); void mutt_unprepare_envelope(struct Envelope *env); void mutt_pretty_mailbox(char *s, size_t buflen); -void mutt_pretty_size(char *s, size_t len, size_t n); void mutt_pipe_message(struct Header *h); void mutt_print_message(struct Header *h); void mutt_query_exit(void); diff --git a/recvattach.c b/recvattach.c index 2a94f9d2a86..c9098fad486 100644 --- a/recvattach.c +++ b/recvattach.c @@ -348,7 +348,7 @@ const char *attach_format_str(char *buf, size_t buflen, size_t col, int cols, if (!optional) { - mutt_pretty_size(tmp, sizeof(tmp), l); + mutt_str_pretty_size(tmp, sizeof(tmp), l); mutt_format_s(buf, buflen, prec, tmp); } else if (l == 0) diff --git a/status.c b/status.c index b99c411dd78..5b4194ae1c2 100644 --- a/status.c +++ b/status.c @@ -168,7 +168,7 @@ static const char *status_format_str(char *buf, size_t buflen, size_t col, int c if (!optional) { snprintf(fmt, sizeof(fmt), "%%%ss", prec); - mutt_pretty_size(tmp, sizeof(tmp), Context ? Context->size : 0); + mutt_str_pretty_size(tmp, sizeof(tmp), Context ? Context->size : 0); snprintf(buf, buflen, fmt, tmp); } else if (!Context || !Context->size) @@ -179,7 +179,7 @@ static const char *status_format_str(char *buf, size_t buflen, size_t col, int c if (!optional) { snprintf(fmt, sizeof(fmt), "%%%ss", prec); - mutt_pretty_size(tmp, sizeof(tmp), Context ? Context->vsize : 0); + mutt_str_pretty_size(tmp, sizeof(tmp), Context ? Context->vsize : 0); snprintf(buf, buflen, fmt, tmp); } else if (!Context || !Context->pattern)