mikesart / inotify-info

Linux inotify info reporting app
MIT License
280 stars 29 forks source link

[Feature Request] Thousands separator #8

Closed lonix1 closed 1 year ago

lonix1 commented 1 year ago

Would be nice to have thousands separator, e.g. 12,345 or 12.345 depending on locale.

Maybe: inotify-info -s or inotify-info --separators.

mikesart commented 1 year ago

I assume we'd just want number of Watches to have this? And how about localization? Is using a comma ok or will some international folks want a different character here?

When you get a chance, can you try the below patch? It's a bit of a quick hack but let me know if it's ~ what you want. We can also hide it behind a command line parameter as well of course. Thanks!

diff --git i/inotify-info.cpp w/inotify-info.cpp
index fe12b52..82aba2b 100644
--- i/inotify-info.cpp
+++ w/inotify-info.cpp
@@ -582,8 +582,61 @@ static bool init_inotify_proclist( std::vector< procinfo_t > &inotify_proclist )
     return true;
 }

+// From:
+//  https://stackoverflow.com/questions/1449805/how-to-format-a-number-using-comma-as-thousands-separator-in-c
+size_t str_format_uint32(char dst[16], uint32_t num)
+{
+    char src[16];
+    char *p_src = src;
+    char *p_dst = dst;
+
+    int num_len, commas;
+    const char separator = ',';
+
+    num_len = sprintf(src, "%u", num);
+
+    for (commas = 2 - num_len % 3; *p_src; commas = (commas + 1) % 3)
+    {
+        *p_dst++ = *p_src++;
+        if (commas == 1) {
+            *p_dst++ = separator;
+        }
+    }
+    *--p_dst = '\0';
+
+    return (size_t)(p_dst - dst);
+}
+
 static void print_inotify_proclist( std::vector< procinfo_t > &inotify_proclist )
 {
+#if 0
+    // test data
+    procinfo_t proc_info = {};
+    proc_info.pid = 100;
+    proc_info.appname = "fsnotifier";
+    proc_info.watches = 2;
+    proc_info.instances = 1;
+    inotify_proclist.push_back(proc_info);
+
+    proc_info.pid = 1000;
+    proc_info.appname = "evolution-addressbook-factor";
+    proc_info.watches = 116;
+    proc_info.instances = 10;
+    inotify_proclist.push_back(proc_info);
+
+    proc_info.pid = 22154;
+    proc_info.appname = "evolution-addressbook-factor blah blah";
+    proc_info.watches = 28200;
+    proc_info.instances = 100;
+    inotify_proclist.push_back(proc_info);
+
+    proc_info.pid = 0x7fffffff;
+    proc_info.appname = "evolution-addressbook-factor blah blah2";
+    proc_info.watches = 999999;
+    proc_info.instances = 999999999;
+    inotify_proclist.push_back(proc_info);
+#endif
+
     int lenPid = 10;
     int lenApp = 10;
     int lenWatches = 8;
@@ -597,10 +650,14 @@ static void print_inotify_proclist( std::vector< procinfo_t > &inotify_proclist

     for ( procinfo_t &procinfo : inotify_proclist )
     {
-        printf( "%*d %s%-*s%s %*u %*u\n",
+        char watches_str[16];
+
+        str_format_uint32(watches_str, procinfo.watches);
+
+        printf( "%*d %s%-*s%s %*s %*u\n",
             lenPid, procinfo.pid,
             BYELLOW, lenApp, procinfo.appname.c_str(), RESET,
-            lenWatches, procinfo.watches,
+            lenWatches, watches_str,
             lenInstances, procinfo.instances );

         if ( g_verbose > 1 )
lonix1 commented 1 year ago

I'm unsure how to use that code?... Looking forward to this feature once it's released. Thanks for working on it.

mikesart commented 1 year ago

Should be fixed. Thanks lonix.

lonix1 commented 1 year ago

Works like a charm, thanks Mike!

(The only issue I;ve noticed is the "Total inotify Watches: 18081" summary at the bottom, but that's not an issue as the real interesting info is in the table itself.)