Giter Club home page Giter Club logo

Comments (8)

Pierre-Colin avatar Pierre-Colin commented on September 25, 2024 2

My bad. It wasn’t my intent to solve the entire issue (hence why I didn’t go any further). I mostly wanted to point out that interface change that I believe should have been among the tasks. Maybe providing a diff was too much. I’ll let @reido2012 do the rest as I was going to all along.

from neomutt.

Pierre-Colin avatar Pierre-Colin commented on September 25, 2024 1

Hi. I noticed that mutt_str_pretty_size() doesn’t communicate errors. I think this refactoring is an appropriate time to address this. I propose this change for task 1. It returns the number of characters written, 0 meaning a (non-fatal) error occurred. It also includes updated Doxygen comments, though I haven’t checked whether they compile.

Patch
diff --git a/muttlib.c b/muttlib.c
index fefeb93..db54514 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -1094,47 +1094,63 @@ void buf_sanitize_filename(struct Buffer *buf, const char *path, short slash)
 
 /**
  * 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
+ * @param buf  Buffer to write into
+ * @param num  Number to abbreviate
+ * @retval num Number of characters written
+ * @retval 0   Error
+ *
+ * Formats a number to abbreviate it with the metric prefixes K and M if
+ * needed. If necessary, the buffer is expanded. The following configuration
+ * flags affect the formatting:
+ *
+ * - `size_show_bytes`: no prefix is used if `num < 1024`.
+ *
+ * - `size_units_on_left`: the prefix precedes the number (e.g. K2 instead of
+ * 2K).
+ *
+ * - `size_show_fractions`: at most one decimal digit is written (e.g. 1.2K).
+ *
+ * - `size_show_mb`: enable the prefix M.
  */
-void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
+size_t mutt_str_pretty_size(struct Buffer *buf, size_t num)
 {
-  if (!buf || (buflen == 0))
-    return;
+  if (!buf)
+    return 0;
 
   const bool c_size_show_bytes = cs_subset_bool(NeoMutt->sub, "size_show_bytes");
   const bool c_size_show_fractions = cs_subset_bool(NeoMutt->sub, "size_show_fractions");
   const bool c_size_show_mb = cs_subset_bool(NeoMutt->sub, "size_show_mb");
   const bool c_size_units_on_left = cs_subset_bool(NeoMutt->sub, "size_units_on_left");
 
+  int r;
   if (c_size_show_bytes && (num < 1024))
   {
-    snprintf(buf, buflen, "%d", (int) num);
+    r = buf_add_printf(buf, "%zu", num);
   }
   else if (num == 0)
   {
-    mutt_str_copy(buf, c_size_units_on_left ? "K0" : "0K", buflen);
+    return buf_addstr(buf, c_size_units_on_left? "K0" : "0K");
   }
   else if (c_size_show_fractions && (num < 10189)) /* 0.1K - 9.9K */
   {
-    snprintf(buf, buflen, c_size_units_on_left ? "K%3.1f" : "%3.1fK",
-             (num < 103) ? 0.1 : (num / 1024.0));
+    r = buf_add_printf(buf, c_size_units_on_left ? "K%3.1f" : "%3.1fK",
+                       (num < 103) ? 0.1 : (num / 1024.0));
   }
   else if (!c_size_show_mb || (num < 1023949)) /* 10K - 999K */
   {
     /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
-    snprintf(buf, buflen, c_size_units_on_left ? ("K%zu") : ("%zuK"), (num + 51) / 1024);
+    r = buf_add_printf(buf, c_size_units_on_left ? "K%zu" : "%zuK", (num + 51) / 1024);
   }
   else if (c_size_show_fractions && (num < 10433332)) /* 1.0M - 9.9M */
   {
-    snprintf(buf, buflen, c_size_units_on_left ? "M%3.1f" : "%3.1fM", num / 1048576.0);
+    r = buf_add_printf(buf, c_size_units_on_left ? "M%3.1f" : "%3.1fM", num / 1048576.0);
   }
   else /* 10M+ */
   {
     /* (10433332 + 52428) / 1048576 = 10 */
-    snprintf(buf, buflen, c_size_units_on_left ? ("M%zu") : ("%zuM"), (num + 52428) / 1048576);
+    r = buf_add_printf(buf, units_on_left ? "M%zu" : "%zuM", (num + 52428) / 1048576);
   }
+  return r * (r > 0);
 }
 
 /**

from neomutt.

flatcap avatar flatcap commented on September 25, 2024 1

General note -- Sorry if this sounds angry; it's not directed at anyone.

Finding "nice easy tasks" for beginners isn't easy for me.
They take time to find and time to document clearly.

In the past, I've tried creating lots of "easy-first-issue" issues, but they've proved a waste of my time.

NeoMutt's code is under constant change/improvement, so the "shelf-life" of an easy task is quite short.
Usually, nobody's interested in tackling them,
so frequently the instructions became out-of-date, because of other refactoring going on,
or the issue would become a bottleneck that I had to resolve myself.

So, I ask everyone I speak to, "Would you like to help?" and I create issues on demand.

/rant

from neomutt.

reido2012 avatar reido2012 commented on September 25, 2024

Hi @flatcap I can't find the branch devel/expando branch.

I do see the devel/new-expando-parser branch though.

Just going to assume you meant that branch but let me know if that's incorrect.

from neomutt.

flatcap avatar flatcap commented on September 25, 2024

Ah, I've just merged it, so it's gone.
As a member of NeoMutt, you can create branches on this repo, preferably: devel/SHORT-NAME

If you let me know your email address (I'm, [email protected]) I'll send you my standard Welcome! email.

from neomutt.

reido2012 avatar reido2012 commented on September 25, 2024

Ah got it. Cheers.

I've sent you an email. My email is [email protected]

from neomutt.

flatcap avatar flatcap commented on September 25, 2024

@Pierre-Colin

mutt_str_pretty_size() doesn’t communicate errors

true

I think this refactoring is an appropriate time to address this

yep

I propose this change

Right.
Please don't take this the wrong way, but I created the issue for @reido2012 (and assigned it to him) after a discussion on IRC.
I'd love you to help, but I don't want people treading on each others' toes.

If you'd like me to find you a similar task, just ask. Here, on IRC, or drop me an email.

from neomutt.

reido2012 avatar reido2012 commented on September 25, 2024

Thanks for the hints with your diff @Pierre-Colin. I'll take it from here 🙏🏾

@flatcap I appreciate the easy first task documentation and support! Given that the shelf life is short I will try to get the changes done as soon as I can.

from neomutt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.