[meld/pathlabel] pathlabel: Format paths as home-relative where sensible



commit 1939da0670684e71f06267a2666db23c0cbb7399
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Tue Aug 24 07:48:39 2021 +1000

    pathlabel: Format paths as home-relative where sensible
    
    This changes the formatting of two cases, being when we're showing the
    parent file path, and when we don't have a common parent set at all. In
    reality, the first of these currently can't happen, because we only show
    the parent when it's the root. The second is the interesting case here,
    and only really affects version control view, making it so that viewing
    version controlled folders under a user's home folder summarises the
    path nicely.

 meld/iohelpers.py    | 11 ++++++++++-
 meld/ui/pathlabel.py |  7 +++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
---
diff --git a/meld/iohelpers.py b/meld/iohelpers.py
index 62dab95c..1a824128 100644
--- a/meld/iohelpers.py
+++ b/meld/iohelpers.py
@@ -148,6 +148,15 @@ def find_shared_parent_path(
     return current_parent
 
 
+def format_home_relative_path(gfile: Gio.File) -> str:
+    home_file = Gio.File.new_for_path(GLib.get_home_dir())
+    home_relative = home_file.get_relative_path(gfile)
+    if home_relative:
+        return GLib.build_filenamev(['~', home_relative])
+    else:
+        return gfile.get_parse_name()
+
+
 def format_parent_relative_path(parent: Gio.File, descendant: Gio.File) -> str:
     """Format shortened child paths using a common parent
 
@@ -224,7 +233,7 @@ def format_parent_relative_path(parent: Gio.File, descendant: Gio.File) -> str:
         '…' if elided_path else None,
         descendant.get_basename(),
     ]
-    label_text = parent.get_parse_name() if show_parent else ''
+    label_text = format_home_relative_path(parent) if show_parent else ""
     label_text += GLib.build_filenamev([s for s in label_segments if s])
 
     return label_text
diff --git a/meld/ui/pathlabel.py b/meld/ui/pathlabel.py
index 440ae9ca..b3cbdeb0 100644
--- a/meld/ui/pathlabel.py
+++ b/meld/ui/pathlabel.py
@@ -19,7 +19,10 @@ from typing import Optional
 from gi.repository import Gdk, Gio, GObject, Gtk
 
 from meld.conf import _
-from meld.iohelpers import format_parent_relative_path
+from meld.iohelpers import (
+    format_home_relative_path,
+    format_parent_relative_path,
+)
 from meld.melddoc import open_files_external
 
 log = logging.getLogger(__name__)
@@ -187,7 +190,7 @@ class PathLabel(Gtk.MenuButton):
             # If we have no parent yet but have a descendant, we'll use
             # the descendant name as the better-than-nothing label.
             if descendant:
-                self._path_label = descendant.get_parse_name()
+                self._path_label = format_home_relative_path(descendant)
                 self.notify('path_label')
             return
 


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