Support aliases on cwd segments

Add path_aliases argument to CwdSegment.get_shortened_path allowing to
specify aliases for well-known directory paths.
This commit is contained in:
Christophe Henry 2024-10-04 09:39:52 +02:00
parent 574bb1887b
commit 853d1d56f5

View File

@ -52,7 +52,7 @@ class CwdSegment(Segment):
else: else:
return super(CwdSegment, self).omitted_args(name, method) return super(CwdSegment, self).omitted_args(name, method)
def get_shortened_path(self, pl, segment_info, shorten_home=True, **kwargs): def get_shortened_path(self, pl, segment_info, shorten_home=True, path_aliases=None, **kwargs):
try: try:
path = out_u(segment_info['getcwd']()) path = out_u(segment_info['getcwd']())
except OSError as e: except OSError as e:
@ -63,7 +63,12 @@ class CwdSegment(Segment):
return '[not found]' return '[not found]'
else: else:
raise raise
if shorten_home: if isinstance(path_aliases, dict):
for alias_target, alias in path_aliases.items():
alias_target = os.path.expanduser(out_u(alias_target))
if path.startswith(alias_target):
path = out_u(alias) + path[len(alias_target):]
elif shorten_home:
home = segment_info['home'] home = segment_info['home']
if home: if home:
home = out_u(home) home = out_u(home)
@ -122,6 +127,10 @@ Returns a segment list to create a breadcrumb-like effect.
Use path separator in place of soft divider. Use path separator in place of soft divider.
:param bool shorten_home: :param bool shorten_home:
Shorten home directory to ``~``. Shorten home directory to ``~``.
:param dict path_aliases
Define aliases for well-known paths. Directory starting with one
of theses aliases will be shortenened to the provided alias.
Example: {"~/Music": "🎵"}
:param str ellipsis: :param str ellipsis:
Specifies what to use in place of omitted directories. Use None to not Specifies what to use in place of omitted directories. Use None to not
show this subsegment at all. show this subsegment at all.