browsing directories works

This commit is contained in:
2026-02-05 20:22:08 +11:00
parent 48536c3b19
commit e07bd7b3e5
2 changed files with 57 additions and 15 deletions

View File

@@ -43,6 +43,10 @@
background: $surface;
}
link-style-hover: underline;
link-color-hover: $primary;
link-background-hover: transparent;
}
#description {

View File

@@ -19,6 +19,7 @@ class RepoViewScreen(Screen):
super().__init__()
self.owner_name = owner_name
self.repo_name = repo_name
self.current_dir = "./src"
def get_icon_from_name_and_type(self, file_name: str, is_folder: bool):
@@ -85,21 +86,28 @@ class RepoViewScreen(Screen):
return "\ue5ff"
@work(thread=True, exclusive=True)
def on_mount(self):
def action_view_file(self, path: str, type: str):
self.notify(f"{path, type}")
if type == "dir":
self.current_dir = path
self.show_directory(path)
def show_directory(self, path: str):
files: DataTable = self.query_one("#files")
loading = self.query_one(LoadingIndicator)
files.clear(columns=True)
files.display = False
# get files in root dir
# get files in dir
files_response = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents"
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents/{path}"
)
if not files_response.ok:
self.notify(files_response.text, title="Failed to get files:", severity="error")
return
print("Getting most recent commit...")
# get most recent commit
commits_response = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/commits",
@@ -119,7 +127,7 @@ class RepoViewScreen(Screen):
f"[d]{most_recent_commit["commit"]["message"]}"
)
print("Getting root commits...")
rows = []
commit_page_number = 1
@@ -134,8 +142,9 @@ class RepoViewScreen(Screen):
"path": ".",
"verification": False,
"files": False,
"stat": False,
"page": commit_page_number,
"limit": page_size
"limit": page_size,
}
).json()
@@ -144,11 +153,23 @@ class RepoViewScreen(Screen):
if len(commits) == page_size: # we reached end of the page
commit_page_number += 1
print("Next page...")
else:
break
found_readme = False
readme: Markdown = self.query_one("#readme")
found_readme = False
readme.display = path == "."
# add a ".." folder if we're not in the root directory
if path != ".":
previous_dir = self.current_dir[:self.current_dir.rfind("/")]
rows.append((
f"[cyan]{self.get_icon_from_name_and_type("..", True)}[/] [@click=screen.view_file('{previous_dir}','dir')]..[/]",
"",
""
))
for file in files_response.json():
commit = commit_data[file["last_commit_sha"]]
@@ -156,12 +177,14 @@ class RepoViewScreen(Screen):
commit_created_at = datetime.fromisoformat(commit["created"]).replace(tzinfo=None)
rows.append((
f"[cyan]{self.get_icon_from_name_and_type(file["name"], file["type"] != "file")}[/] {file["name"]}",
f"[d]{commit["commit"]["message"]}",
f"[d]Updated {time_delta(datetime.now() - commit_created_at)} ago"
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]Updated {time_delta(datetime.now() - commit_created_at)} ago[/]"
))
if file["name"].lower() == "readme.md":
if path == "." and file["name"].lower() == "readme.md":
print("Getting README...")
found_readme = True
readme.border_title = f"\uf405 {file["name"]}"
@@ -170,11 +193,26 @@ class RepoViewScreen(Screen):
).text
self.app.call_from_thread(readme.update, readme_content or "Empty README")
print("Adding rows...")
files.add_rows(rows)
if not found_readme:
if not found_readme and path == ".":
self.app.call_from_thread(readme.update, "This repository has no `README.md` file.")
return found_readme
@work(thread=True, exclusive=True)
def on_mount(self):
print("Getting files...")
files: DataTable = self.query_one("#files")
loading = self.query_one(LoadingIndicator)
files.display = False
self.show_directory(self.current_dir)
loading.display = False
files.display = True