Skip to content

Commit

Permalink
move mutt_pretty_size to libmutt
Browse files Browse the repository at this point in the history
  • Loading branch information
flatcap committed Jan 17, 2018
1 parent ba46229 commit 835bb31
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions curs_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion hdrline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions mutt/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions mutt/string2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 0 additions & 20 deletions muttlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 0 additions & 1 deletion protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion recvattach.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 835bb31

Please sign in to comment.