optimized getting reading directories, started work on dir tree in file view

This commit is contained in:
2026-02-06 16:54:20 +11:00
parent 7956361044
commit 16afb5e2d0
3 changed files with 33 additions and 34 deletions

View File

@@ -4,7 +4,7 @@ from textual.widgets import Input, Select, TextArea, LoadingIndicator, Static, D
from textual.containers import VerticalGroup, Vertical, HorizontalGroup, Right from textual.containers import VerticalGroup, Vertical, HorizontalGroup, Right
from textual import work from textual import work
from widgets import Navbar from widgets import Navbar, RepoDirectoryTree
from datetime import datetime from datetime import datetime
from human_readable import time_delta from human_readable import time_delta
@@ -180,7 +180,10 @@ class RepoViewScreen(Screen):
# get files in dir # get files in dir
files_response = requests.get( files_response = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents/{path}" self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents-ext/{path}",
params={
"includes": "commit_metadata,commit_message"
}
) )
if not files_response.ok: if not files_response.ok:
@@ -212,32 +215,6 @@ class RepoViewScreen(Screen):
print("Getting root commits...") print("Getting root commits...")
rows = [] rows = []
commit_page_number = 1
page_size = 50
commit_data = {}
# loop over all the root commits until we get em' all
while True:
commits = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/commits",
params={
"path": path,
"verification": False,
"files": False,
"stat": False,
"page": commit_page_number,
"limit": page_size,
}
).json()
commit_data.update({commit["sha"]: commit for commit in commits})
if len(commits) == page_size: # we reached end of the page
commit_page_number += 1
print("Next page...")
else:
break
found_readme = False found_readme = False
@@ -251,14 +228,12 @@ class RepoViewScreen(Screen):
"" ""
)) ))
for file in files_response.json(): for file in files_response.json()["dir_contents"]:
commit = commit_data[file["last_commit_sha"]] commit_created_at = datetime.fromisoformat(file["last_committer_date"]).replace(tzinfo=None)
commit_created_at = datetime.fromisoformat(commit["created"]).replace(tzinfo=None)
rows.append(( rows.append((
f"[cyan]{self.get_icon_from_name_and_type(file["name"], file["type"] != "file")}[/] [@click=screen.view_file('{self.current_dir}/{file["name"]}','{file["type"]}')]{file["name"]}[/]", f"[cyan]{self.get_icon_from_name_and_type(file["name"], file["type"] != "file")}[/] [@click=screen.view_file('{self.current_dir}/{file["name"]}','{file["type"]}')]{file["name"]}[/]",
f"[d]{commit["commit"]["message"]}[/]", f"[d]{file["last_commit_message"]}[/]",
f"[d]Updated {time_delta(datetime.now() - commit_created_at)} ago[/]" f"[d]Updated {time_delta(datetime.now() - commit_created_at)} ago[/]"
)) ))
@@ -342,6 +317,7 @@ class RepoViewScreen(Screen):
yield table yield table
with Vertical(id="file-screen"): with Vertical(id="file-screen"):
yield RepoDirectoryTree(self.owner_name, self.repo_name, ".")
with HorizontalGroup(id="file-commit"): with HorizontalGroup(id="file-commit"):
yield Static("[b]Person[/] [r]aaaaabbbbb[/] [d]some commit message[/d]") yield Static("[b]Person[/] [r]aaaaabbbbb[/] [d]some commit message[/d]")
with Right(): with Right():

View File

@@ -1 +1,2 @@
from .navbar import Navbar from .navbar import Navbar
from .repo_dir_tree import RepoDirectoryTree

22
widgets/repo_dir_tree.py Normal file
View File

@@ -0,0 +1,22 @@
from textual.widgets import Tree, DirectoryTree
from textual.widgets.directory_tree import DirEntry
from textual import work
import requests
class RepoDirectoryTree(DirectoryTree):
ICON_NODE_EXPANDED = "\uf07c "
ICON_NODE = "\ue5ff "
ICON_FILE = "\uf15b "
def __init__(self, repo_owner: str, repo_name: str, path: str, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False):
super().__init__(path=path, name=name, id=id, classes=classes, disabled=disabled)
self.repo_owner = repo_owner
self.repo_name = repo_name
def _directory_content(self, location: Path, worker: Worker) -> Iterator[Path]:
contents = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.repo_owner}/{self.repo_name}/contents/{location}"
).json()