[gnome-usage] utils: Use GLib format size



commit 6f14f96534061af37c5a88b1d1290a6db6fa9622
Author: Nishal Kulkarni <nishalkulkarni gmail com>
Date:   Wed Dec 8 14:54:26 2021 +0530

    utils: Use GLib format size
    
    Currently we perform unit conversion manually,
    this patch uses GLib provided standard function `format_size()`
    for converting larger byte values into KB, MB, GB etc.
    
    Fixes: #26

 src/utils.vala | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
---
diff --git a/src/utils.vala b/src/utils.vala
index 437bf88..5767502 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -21,29 +21,17 @@
 namespace Usage {
     public class Utils {
         public static string format_size_values(uint64 value) {
-            if (value >= 1000000000000)
-                return "%.3f TB".printf((double) value / 1000000000000d);
-            else if (value >= 1000000000)
-                return "%.1f GB".printf((double) value / 1000000000d);
-            else if (value >= 1000000)
-                return(value / 1000000).to_string() + " MB";
-            else if (value >= 1000)
-                return (value / 1000).to_string() + " KB";
+            if (value >= 1000)
+                return GLib.format_size(value);
             else
-                return value.to_string() + " B";
+                return _("%llu B").printf(value);
         }
 
         public static string format_size_speed_values(uint64 value) {
-            if (value >= 1000000000000)
-                return "%.3f TB/s".printf((double) value / 1000000000000d);
-            else if (value >= 1000000000)
-                return "%.1f GB/s".printf((double) value / 1000000000d);
-            else if (value >= 1000000)
-                return "%.2f MB/s".printf((double) value / 1000000d);
-            else if (value >= 1000)
-                return (value / 1000).to_string() + " KB/s";
+            if (value >= 1000)
+                return _("%s/s").printf(GLib.format_size(value));
             else
-                return value.to_string() + " B/s";
+                return _("%llu B/s").printf(value);
         }
 
         public static Gdk.RGBA generate_color(Gdk.RGBA default_color, uint order, uint all_count, bool 
reverse = false) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]