show file info and also fix a ui bug with file screen

This commit is contained in:
2026-02-06 06:59:39 +11:00
parent 38b063f7af
commit 39aeffa8db
2 changed files with 77 additions and 14 deletions

View File

@@ -9,7 +9,7 @@ from widgets import Navbar
from datetime import datetime
from human_readable import time_delta
import requests, asyncio
import requests, asyncio, base64
class RepoViewScreen(Screen):
@@ -81,6 +81,8 @@ class RepoViewScreen(Screen):
return "\ue620"
case 'zip' | 'tar' | 'gz' | "7z":
return "\ue6aa"
case "rb":
return "\ue605"
case _:
return "\uf15b"
else:
@@ -88,8 +90,6 @@ class RepoViewScreen(Screen):
@work(thread=False, exclusive=True)
async 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)
@@ -99,38 +99,59 @@ class RepoViewScreen(Screen):
def show_file(self, path: str):
files: DataTable = self.query_one("#files")
loading = self.query_one(LoadingIndicator)
open_file: TextArea = self.query_one("#open-file")
file_screen = self.query_one("#file-screen")
open_file: TextArea = file_screen.query_one("#open-file")
file_info = file_screen.query_one("#file-info-text")
self.query_one("#readme").display = False
self.query_one("#repo-info").display = False
files.display = False
open_file.display = False
file_screen.display = False
loading.display = True
content = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/raw/{path}"
).text
file = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents/{path}"
).json()
languages = {
"py": "python",
"md": "markdown",
"h": "c",
"cpp": "c++"
}
language_names = {
"py": "Python",
"c": "C",
"h": "C/C++",
"cpp": "C++",
"js": "Javascript",
"rs": "Rust",
"htm": "HTML",
"html": "HTML",
"css": "CSS",
"tcss": "Textual CSS",
"rb": "Ruby",
"md": "Markdown",
"txt": "Raw Text"
}
extension = path[path.rfind(".")+1:]
open_file.text = content
decoded_text = base64.decodebytes(file["content"].encode()).decode()
open_file.text = decoded_text
try:
open_file.language = languages.get(extension, extension)
except:
open_file.language = None
self.notify(f"{open_file._languages}")
file_info.update(f"{len(decoded_text.split('\n'))} lines | {file['size']} bytes | {language_names.get(extension, "Unkown")}")
loading.display = False
open_file.display = True
file_screen.display = True
def show_directory(self, path: str):
files: DataTable = self.query_one("#files")
self.query_one("#open-file").display = False
self.query_one("#file-screen").display = False
files.clear(columns=True)
loading = self.query_one(LoadingIndicator)
@@ -307,7 +328,17 @@ class RepoViewScreen(Screen):
#table.add_row("\ue5ff screens", "[d]switched from tabs to spaces", "[d]51 minutes ago")
yield table
yield TextArea.code_editor(theme="css", placeholder="Empty file", read_only=True, id="open-file")
with Vertical(id="file-screen"):
with HorizontalGroup(id="file-commit"):
yield Static("[b]Person[/] [r]aaaaabbbbb[/] [d]some commit message[/d]")
with Right():
yield Static("[d]yesterday[/]")
with HorizontalGroup(id="file-info"):
yield Static("127 lnes | 16 KiB | C++", id="file-info-text")
with Right():
yield Button("\uf409", flat=True, tooltip="Download file", id="download-file")
yield Button("\uf4bb", flat=True, tooltip="Copy content", id="copy-file")
yield TextArea.code_editor(theme="css", placeholder="Empty file", read_only=True, id="open-file")
yield LoadingIndicator()