[baobab/reroot-view: 13/30] Use the same formatting function everywhere in the UI



commit 9a618e6c6c290d8e51158c85ef50de839db53272
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Thu Jun 18 10:48:41 2020 +0200

    Use the same formatting function everywhere in the UI

 src/baobab-cellrenderers.vala | 78 +++++++++++++++++++++++++------------------
 src/baobab-chart.vala         |  7 +---
 src/baobab-window.vala        |  6 ++--
 3 files changed, 50 insertions(+), 41 deletions(-)
---
diff --git a/src/baobab-cellrenderers.vala b/src/baobab-cellrenderers.vala
index 042be57..23bae81 100644
--- a/src/baobab-cellrenderers.vala
+++ b/src/baobab-cellrenderers.vala
@@ -20,6 +20,48 @@
 
 namespace Baobab {
 
+    public string format_name (string? display_name, string? name) {
+        if (display_name != null) {
+            return display_name;
+        }
+        if (name != null) {
+            return Filename.display_name (name);
+        }
+        return "";
+    }
+
+    public string format_items (int items) {
+        return ngettext ("%d item", "%d items", items).printf (items);
+    }
+
+    public string format_time_approximate (uint64 time) {
+        if (time == 0) {
+            // Translators: when the last modified time is unknown
+            return _("Unknown");
+        }
+
+        var dt = new DateTime.from_unix_local ((int64) time);
+        var now = new DateTime.now_local ();
+        var ts = now.difference (dt);
+        if (ts < TimeSpan.DAY) {
+            // Translators: when the last modified time is today
+            return _("Today");
+        }
+        if (ts < 31 * TimeSpan.DAY) {
+            var days = (ulong) (ts / TimeSpan.DAY);
+            // Translators: when the last modified time is "days" days ago
+            return ngettext ("%lu day", "%lu days", days).printf (days);
+        }
+        if (ts < 365 * TimeSpan.DAY) {
+            var months = (ulong) (ts / (31 * TimeSpan.DAY));
+            // Translators: when the last modified time is "months" months ago
+            return ngettext ("%lu month", "%lu months", months).printf (months);
+        }
+        var years = (ulong) (ts / (365 * TimeSpan.DAY));
+        // Translators: when the last modified time is "years" years ago
+        return ngettext ("%lu year", "%lu years", years).printf (years);
+    }
+
     public class CellRendererName : Gtk.CellRendererText {
         public Scanner.State state { set; get; }
 
@@ -27,12 +69,8 @@ namespace Baobab {
 
         public string name {
             set {
-                string escaped = null;
-                if (display_name != null) {
-                    escaped = Markup.escape_text (display_name);
-                } else if (value != null) {
-                    escaped = Markup.escape_text (Filename.display_name (value));
-                }
+                string escaped = Markup.escape_text (format_name (display_name, value));
+
                 switch (state) {
                 case Scanner.State.ERROR:
                     markup = "<b>%s</b>".printf (escaped);
@@ -85,7 +123,7 @@ namespace Baobab {
 
         public int items {
             set {
-                text = (value >= 0 && state != Scanner.State.ERROR) ? ngettext ("%d item", "%d items", 
value).printf (value) : "";
+                text = (value >= 0 && state != Scanner.State.ERROR) ? format_items (value) : "";
             }
         }
     }
@@ -93,31 +131,7 @@ namespace Baobab {
     public class CellRendererTime : Gtk.CellRendererText {
         public uint64 time {
             set {
-                if (value == 0) {
-                    // Translators: when the last modified time is unknown
-                    text = _("Unknown");
-                    return;
-                }
-
-                var dt = new DateTime.from_unix_local ((int64)value);
-                var now = new DateTime.now_local ();
-                var ts = now.difference (dt);
-                if (ts < TimeSpan.DAY) {
-                    // Translators: when the last modified time is today
-                    text = _("Today");
-                } else if (ts < 31 * TimeSpan.DAY) {
-                    var days = (ulong) (ts / TimeSpan.DAY);
-                    // Translators: when the last modified time is "days" days ago
-                    text = ngettext ("%lu day", "%lu days", days).printf (days);
-                } else if (ts < 365 * TimeSpan.DAY) {
-                    var months = (ulong) (ts / (31 * TimeSpan.DAY));
-                    // Translators: when the last modified time is "months" months ago
-                    text = ngettext ("%lu month", "%lu months", months).printf (months);
-                } else {
-                    var years = (ulong) (ts / (365 * TimeSpan.DAY));
-                    // Translators: when the last modified time is "years" years ago
-                    text = ngettext ("%lu year", "%lu years", years).printf (years);
-                }
+                text = format_time_approximate (value);
             }
         }
     }
diff --git a/src/baobab-chart.vala b/src/baobab-chart.vala
index f7ff390..ce34208 100644
--- a/src/baobab-chart.vala
+++ b/src/baobab-chart.vala
@@ -257,12 +257,7 @@ namespace Baobab {
                        Scanner.Columns.SIZE, out size);
 
             var item = create_new_chartitem ();
-            item.name = "";
-            if (display_name != null) {
-                item.name = display_name;
-            } else if (name != null) {
-                item.name = Filename.display_name (name);
-            }
+            item.name = format_name (display_name, name);
             item.size = format_size (size);
             item.depth = depth;
             item.rel_start = rel_start;
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index 4459407..bb65a3d 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -70,11 +70,11 @@ namespace Baobab {
                 if (value.get_depth () == 1) {
                     folder_name.label = location.name;
                 } else {
-                    folder_name.label = display_name != null ? display_name : name;
+                    folder_name.label = format_name (display_name, name);
                 }
                 folder_size.label = format_size (size);
-                folder_elements.label = "%d items".printf (elements);
-                folder_time.label = new DateTime.from_unix_local ((int64) time).format ("%F");
+                folder_elements.label = format_items (elements);
+                folder_time.label = format_time_approximate (time);
             }
         }
     }


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