Skip to content

Commit

Permalink
✨ FileInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Mar 20, 2024
1 parent d92e8bb commit bbc1d7d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
2 changes: 2 additions & 0 deletions browsr/screens/code_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(
self.file_information.file_info = get_file_info(
file_path=self.code_browser.selected_file_path
)
else:
self.file_information.file_info = get_file_info(self.config_object.path)
self.footer = Footer()

def compose(self) -> Iterable[Widget]:
Expand Down
86 changes: 73 additions & 13 deletions browsr/widgets/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from textual.message import Message
from textual.reactive import reactive
from textual.widget import Widget
from textual_universal_directorytree import (
GitHubTextualPath,
S3TextualPath,
SFTPTextualPath,
is_remote_path,
)

from browsr.utils import FileInfo

Expand Down Expand Up @@ -56,23 +62,77 @@ def render(self) -> RenderableType:
"""
Render the Current File Info Bar
"""
if self.file_info is None or not self.file_info.is_file:
return Text("")
status_string = "🗄️️️ " + self._convert_size(self.file_info.size)
status_string = self.render_file_protocol()
if self.file_info is None:
return Text(status_string)
elif self.file_info.is_file:
file_options = self.render_file_options()
status_string += file_options
if (
self.file_info.last_modified is not None
and self.file_info.last_modified.timestamp() != 0
):
modify_time = self.file_info.last_modified.strftime("%b %d, %Y %I:%M %p")
status_string += " 📅 " + modify_time
parent_name = self.file_info.file.parent.name
if not parent_name:
parent_name = str(self.file_info.file.parent)
parent_name = parent_name.lstrip(f"{self.file_info.file.protocol}://")
parent_name = parent_name.rstrip("/")
status_string += " 💾 " + self.file_info.file.name + " 📂 " + parent_name
status_string += f" 📅 {modify_time}"
directory_options = self.render_directory_options()
status_string += directory_options
return Text(status_string.strip(), style="dim")

def render_file_options(self) -> str:
"""
Render the file options
"""
status_string = ""
if not self.file_info:
return status_string
if self.file_info.is_file:
file_size = self._convert_size(self.file_info.size)
status_string += f" 🗄️️ {file_size}"
if self.file_info.owner not in ["", None]:
status_string += " 👤 " + self.file_info.owner
status_string += f" 👤 {self.file_info.owner}"
if self.file_info.group.strip() not in ["", None]:
status_string += " 🏠 " + self.file_info.group
return Text(status_string, style="dim")
status_string += f" 🏠 {self.file_info.group}"
return status_string

def render_directory_options(self) -> str:
"""
Render the directory options
"""
status_string = ""
if not self.file_info:
return status_string
if self.file_info.is_file:
directory_name = self.file_info.file.parent.name
if not directory_name or (
self.file_info.file.protocol
and f"{self.file_info.file.protocol}://" in directory_name
):
directory_name = str(self.file_info.file.parent)
directory_name = directory_name.lstrip(
f"{self.file_info.file.protocol}://"
)
directory_name = directory_name.rstrip("/")
status_string += f" 📂 {directory_name}"
status_string += f" 💾 {self.file_info.file.name}"
else:
status_string += f" 📂 {self.file_info.file.name}"
return status_string

def render_file_protocol(self) -> str:
"""
Render the file protocol
"""
status_string = ""
if not self.file_info:
return status_string
if is_remote_path(self.file_info.file):
if isinstance(self.file_info.file, GitHubTextualPath):
protocol = "GitHub"
elif isinstance(self.file_info.file, S3TextualPath):
protocol = "S3"
elif isinstance(self.file_info.file, SFTPTextualPath):
protocol = "SFTP"
else:
protocol = self.file_info.file.protocol
status_string += f"🗂️ {protocol}"
return status_string
11 changes: 9 additions & 2 deletions browsr/widgets/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,13 @@ class WindowSwitcher(Container):

show_tree: Reactive[bool] = reactive(True)

datatable_extensions: ClassVar[list[str]] = [".csv", ".parquet", ".feather", ".fea"]
datatable_extensions: ClassVar[list[str]] = [
".csv",
".parquet",
".feather",
".fea",
".csv.gz",
]
image_extensions: ClassVar[list[str]] = image_file_extensions.copy()
markdown_extensions: ClassVar[list[str]] = [".md"]
json_extensions: ClassVar[list[str]] = [".json"]
Expand Down Expand Up @@ -343,7 +349,8 @@ def render_file(self, file_path: UPath, scroll_home: bool = True) -> None:
Render a file
"""
switch_window = self.static_window
if file_path.suffix.lower() in self.datatable_extensions:
joined_suffixes = "".join(file_path.suffixes).lower()
if joined_suffixes in self.datatable_extensions:
self.datatable_window.refresh_from_file(
file_path=file_path, max_lines=self.config_object.max_lines
)
Expand Down

0 comments on commit bbc1d7d

Please sign in to comment.