From fb24f6f2b049d15c32b0146f3b67f74118bec7b4 Mon Sep 17 00:00:00 2001 From: Justin Flannery Date: Thu, 14 Mar 2024 13:54:54 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20SFTPTextualPath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alternate_paths.py | 20 ++++++++++++++----- textual_universal_directorytree/app.py | 8 ++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/textual_universal_directorytree/alternate_paths.py b/textual_universal_directorytree/alternate_paths.py index 4b28950..f87b5f3 100644 --- a/textual_universal_directorytree/alternate_paths.py +++ b/textual_universal_directorytree/alternate_paths.py @@ -124,9 +124,22 @@ class SFTPTextualPath(UPath): SFTPTextualPath """ + @property + def path(self) -> str: + """ + Always return the path relative to the root + """ + pth = super().path + if pth.startswith("."): + return f"/{pth[1:]}" + elif pth.startswith("/"): + return pth + else: + return "/" + pth + def __str__(self) -> str: """ - String representation of the SFTPPath + Add the protocol prefix + extras to the string representation """ string_representation = f"{self.protocol}://" if "username" in self.storage_options: @@ -134,8 +147,5 @@ def __str__(self) -> str: string_representation += f"{self.storage_options['host']}" if "port" in self.storage_options: string_representation += f":{self.storage_options['port']}" - if self.path == ".": - string_representation += "/" - else: - string_representation += f"/{self.path}" + string_representation += self.path return string_representation diff --git a/textual_universal_directorytree/app.py b/textual_universal_directorytree/app.py index ae4e07d..61bfd94 100644 --- a/textual_universal_directorytree/app.py +++ b/textual_universal_directorytree/app.py @@ -5,6 +5,7 @@ from __future__ import annotations import argparse +import os from typing import Any, ClassVar from rich.syntax import Syntax @@ -46,6 +47,7 @@ def handle_file_selected(self, message: DirectoryTree.FileSelected) -> None: Objects returned by the FileSelected event are upath.UPath objects and they are compatible with the familiar pathlib.Path API built into Python. """ + self.sub_title = str(message.path) try: file_content = message.path.read_text() except UnicodeDecodeError: @@ -63,9 +65,11 @@ def cli() -> None: parser = argparse.ArgumentParser(description="Universal Directory Tree") parser.add_argument("path", type=str, help="Path to open", default=".") args = parser.parse_args() - app = UniversalDirectoryTreeApp(path=args.path) - app.run() + cli_app = UniversalDirectoryTreeApp(path=args.path) + cli_app.run() +app = UniversalDirectoryTreeApp(path=os.getenv("UDT_PATH", os.getcwd())) + if __name__ == "__main__": cli()