From: Kevin McCarthy Date: Fri, 28 Jun 2019 22:09:51 +0000 (-0700) Subject: Add size display configuration variables X-Git-Tag: 2019-10-25~98^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=62ed9fbcd120598463ae07f277a9cfdd37078eb1;p=neomutt Add size display configuration variables Although it would be nice to "open this up" more, performance and security/stability issues would arise. Based on the thread in mutt-dev, I hope these vars will satisfy most customization needs: $size_show_bytes $size_show_mb $size_show_fractions $size_units_on_left Co-authored-by: Richard Russon --- diff --git a/doc/manual.xml.head b/doc/manual.xml.head index c3a93376c..8d765b7b5 100644 --- a/doc/manual.xml.head +++ b/doc/manual.xml.head @@ -6997,6 +6997,52 @@ set index_format="%4C %Z %{%b %d} %-15.15L (%?l?%4l&%4c?)%* %s" + + + Bytes size display + + + Various format strings contain expandos that display the size of + messages in bytes. This includes + %s in $attach_format, + %l in $compose_format, + %s in $folder_format, + %c in $index_format, + and %l and %L in $status_format. + There are four configuration variables that can be used to customize + how the numbers are displayed. + + + + $size_show_bytes + will display the number of bytes when the size is < 1 + kilobyte. When unset, kilobytes will be displayed instead. + + + + $size_show_mb will display the + number of megabytes when the size is >= 1 megabyte. When + unset, kilobytes will be displayed instead (which could be a large + number). + + + + $size_show_fractions, + will display numbers with a single decimal place for values from + 0 to 10 kilobytes, and 1 to 10 megabytes. + + + + $size_units_on_left will + display the unit (K or M to the left + of the number, instead of the right if unset. + + + + These variables also affect size display in a few other places, such + as progress indicators and attachment delimeters in the pager. + + diff --git a/globals.h b/globals.h index d3ab4a7ac..f53885be4 100644 --- a/globals.h +++ b/globals.h @@ -250,6 +250,10 @@ WHERE bool C_Score; ///< Config: Use message scoring WHERE bool C_SidebarVisible; ///< Config: (sidebar) Show the sidebar WHERE bool C_SidebarOnRight; ///< Config: (sidebar) Display the sidebar on the right #endif +WHERE bool C_SizeShowBytes; ///< Config: Show smaller sizes in bytes +WHERE bool C_SizeShowFractions; ///< Config: Show size fractions with a single decimal place +WHERE bool C_SizeShowMb; ///< Config: Show sizes in megabytes for sizes greater than 1 megabyte +WHERE bool C_SizeUnitsOnLeft; ///< Config: Show the units as a prefix to the size WHERE bool C_StatusOnTop; ///< Config: Display the status bar at the top WHERE bool C_Suspend; ///< Config: Allow the user to suspend NeoMutt using '^Z' WHERE bool C_TextFlowed; ///< Config: Generate 'format=flowed' messages diff --git a/init.h b/init.h index 27bf92e89..bd93d684d 100644 --- a/init.h +++ b/init.h @@ -314,7 +314,7 @@ struct ConfigDef MuttVars[] = { ** .dt %M .dd MIME subtype ** .dt %n .dd Attachment number ** .dt %Q .dd "Q", if MIME part qualifies for attachment counting - ** .dt %s .dd Size + ** .dt %s .dd Size (see $formatstrings-size) ** .dt %T .dd Graphic tree characters ** .dt %t .dd Tagged flag ** .dt %u .dd Unlink (=to delete) flag @@ -554,7 +554,7 @@ struct ConfigDef MuttVars[] = { ** .dl ** .dt %a .dd Total number of attachments ** .dt %h .dd Local hostname - ** .dt %l .dd Approximate size (in bytes) of the current message + ** .dt %l .dd Approximate size (in bytes) of the current message (see $formatstrings-size) ** .dt %v .dd NeoMutt version string ** .de ** .pp @@ -1154,7 +1154,7 @@ struct ConfigDef MuttVars[] = { ** .dt %m .dd Number of messages in the mailbox * ** .dt %n .dd Number of unread messages in the mailbox * ** .dt %N .dd "N" if mailbox has new mail, blank otherwise - ** .dt %s .dd Size in bytes + ** .dt %s .dd Size in bytes (see $formatstrings-size) ** .dt %t .dd "*" if the file is tagged, blank otherwise ** .dt %u .dd Owner name (or numeric uid, if missing) ** .dt %>X .dd Right justify the rest of the string and pad with character "X" @@ -1816,7 +1816,7 @@ struct ConfigDef MuttVars[] = { ** .dt %b .dd Filename of the original message folder (think mailbox) ** .dt %B .dd The list to which the letter was sent, or else the folder name (%b). ** .dt %C .dd Current message number - ** .dt %c .dd Number of characters (bytes) in the message + ** .dt %c .dd Number of characters (bytes) in the message (see $formatstrings-size) ** .dt %D .dd Date and time of message using $date_format and local timezone ** .dt %d .dd Date and time of message using $date_format and sender's timezone ** .dt %e .dd Current message number in thread @@ -3814,6 +3814,31 @@ struct ConfigDef MuttVars[] = { ** replacing "%s" with the supplied string. ** For the default value, "joe" would be expanded to: "~f joe | ~s joe". */ + { "size_show_bytes", DT_BOOL|R_MENU, &C_SizeShowBytes, false }, + /* + ** .pp + ** If \fIset\fP, message sizes will display bytes for values less than + ** 1 kilobyte. See $formatstrings-size. + */ + { "size_show_fractions", DT_BOOL|R_MENU, &C_SizeShowFractions, true }, + /* + ** .pp + ** If \fIset\fP, message sizes will be displayed with a single decimal value + ** for sizes from 0 to 10 kilobytes and 1 to 10 megabytes. + ** See $formatstrings-size. + */ + { "size_show_mb", DT_BOOL|R_MENU, &C_SizeShowMb, true }, + /* + ** .pp + ** If \fIset\fP, message sizes will display megabytes for values greater than + ** or equal to 1 megabyte. See $formatstrings-size. + */ + { "size_units_on_left", DT_BOOL|R_MENU, &C_SizeUnitsOnLeft, false }, + /* + ** .pp + ** If \fIset\fP, message sizes units will be displayed to the left of the number. + ** See $formatstrings-size. + */ { "skip_quoted_offset", DT_NUMBER|DT_NOT_NEGATIVE, &C_SkipQuotedOffset, 0 }, /* ** .pp @@ -4417,9 +4442,9 @@ struct ConfigDef MuttVars[] = { ** .dt %f .dd The full pathname of the current mailbox ** .dt %F .dd Number of flagged messages * ** .dt %h .dd Local hostname - ** .dt %l .dd Size (in bytes) of the current mailbox * + ** .dt %l .dd Size (in bytes) of the current mailbox (see $formatstrings-size) * ** .dt %L .dd Size (in bytes) of the messages shown - ** (i.e., which match the current limit) * + ** (i.e., which match the current limit) (see $formatstrings-size) * ** .dt %m .dd The number of messages in the mailbox * ** .dt %M .dd The number of messages shown (i.e., which match the current limit) * ** .dt %n .dd Number of new messages in the mailbox * diff --git a/mutt/string.c b/mutt/string.c index ebd1046cb..4a582d7c3 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -1038,41 +1038,6 @@ const char *mutt_str_find_word(const char *src) return src; } -/** - * mutt_str_pretty_size - Display an abbreviated size, like 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 (!buf || (buflen == 0)) - return; - - if (num < 1000) - { - snprintf(buf, buflen, "%dB", (int) num); - } - else if (num < 10189) /* 0.1K - 9.9K */ - { - snprintf(buf, buflen, "%3.1fK", 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); - } -} - /** * mutt_str_getenv - Get an environment variable * @param name Environment variable to get diff --git a/mutt/string2.h b/mutt/string2.h index f70012a43..19249f026 100644 --- a/mutt/string2.h +++ b/mutt/string2.h @@ -86,7 +86,6 @@ bool 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); int mutt_str_remall_strcasestr(char *str, const char *target); void mutt_str_remove_trailing_ws(char *s); void mutt_str_replace(char **p, const char *s); diff --git a/muttlib.c b/muttlib.c index 390342aae..a6bea7054 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1710,3 +1710,43 @@ void mutt_buffer_sanitize_filename(struct Buffer *buf, const char *path, short s mutt_buffer_addch(buf, *path); } } + +/** + * mutt_str_pretty_size - Display an abbreviated size, like 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 (!buf || (buflen == 0)) + return; + + if (C_SizeShowBytes && (num < 1024)) + { + snprintf(buf, buflen, "%d", (int) num); + } + else if (num == 0) + { + mutt_str_strfcpy(buf, C_SizeUnitsOnLeft ? "K0" : "0K", buflen); + } + else if (C_SizeShowFractions && (num < 10189)) /* 0.1K - 9.9K */ + { + snprintf(buf, buflen, C_SizeUnitsOnLeft ? "K%3.1f" : "%3.1fK", + (num < 103) ? 0.1 : (num / 1024.0)); + } + else if (!C_SizeShowMb || (num < 1023949)) /* 10K - 999K */ + { + /* 51 is magic which causes 10189/10240 to be rounded up to 10 */ + snprintf(buf, buflen, C_SizeUnitsOnLeft ? ("K%zu") : ("%zuK"), (num + 51) / 1024); + } + else if (C_SizeShowFractions && (num < 10433332)) /* 1.0M - 9.9M */ + { + snprintf(buf, buflen, C_SizeUnitsOnLeft ? "M%3.1f" : "%3.1fM", num / 1048576.0); + } + else /* 10M+ */ + { + /* (10433332 + 52428) / 1048576 = 10 */ + snprintf(buf, buflen, C_SizeUnitsOnLeft ? ("M%zu") : ("%zuM"), (num + 52428) / 1048576); + } +} diff --git a/muttlib.h b/muttlib.h index 24bec738c..a60530f5a 100644 --- a/muttlib.h +++ b/muttlib.h @@ -71,6 +71,7 @@ void mutt_safe_path(char *s, size_t l, const struct Address *a); int mutt_save_confirm(const char *s, struct stat *st); void mutt_save_path(char *d, size_t dsize, const struct Address *a); void mutt_sleep(short s); +void mutt_str_pretty_size(char *buf, size_t buflen, size_t num); #define mutt_mktemp(buf, buflen) mutt_mktemp_pfx_sfx(buf, buflen, "neomutt", NULL) #define mutt_mktemp_pfx_sfx(buf, buflen, prefix, suffix) mutt_mktemp_full(buf, buflen, prefix, suffix, __FILE__, __LINE__) diff --git a/progress.c b/progress.c index d8f0e9932..5563769ca 100644 --- a/progress.c +++ b/progress.c @@ -37,6 +37,7 @@ #include "curs_lib.h" #include "mutt_curses.h" #include "mutt_logging.h" +#include "muttlib.h" #include "options.h" /* These Config Variables are only used in progress.c */