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

@@ -94,3 +94,35 @@ LoadingIndicator {
border: panel $surface; border: panel $surface;
margin: 1 0 0 1; margin: 1 0 0 1;
} }
#file-screen {
#file-commit {
Static {
width: auto;
}
padding: 0 1;
margin-bottom: 1;
border: tall $surface;
}
#file-info {
Static {
width: auto;
height: 3;
content-align: center middle;
}
Button {
max-width: 5;
}
Right {
layout: horizontal;
}
padding: 0 1;
margin: 0 1 1 1;
background: $surface;
}
}

View File

@@ -9,7 +9,7 @@ from widgets import Navbar
from datetime import datetime from datetime import datetime
from human_readable import time_delta from human_readable import time_delta
import requests, asyncio import requests, asyncio, base64
class RepoViewScreen(Screen): class RepoViewScreen(Screen):
@@ -81,6 +81,8 @@ class RepoViewScreen(Screen):
return "\ue620" return "\ue620"
case 'zip' | 'tar' | 'gz' | "7z": case 'zip' | 'tar' | 'gz' | "7z":
return "\ue6aa" return "\ue6aa"
case "rb":
return "\ue605"
case _: case _:
return "\uf15b" return "\uf15b"
else: else:
@@ -88,8 +90,6 @@ class RepoViewScreen(Screen):
@work(thread=False, exclusive=True) @work(thread=False, exclusive=True)
async def action_view_file(self, path: str, type: str): async def action_view_file(self, path: str, type: str):
self.notify(f"{path, type}")
if type == "dir": if type == "dir":
self.current_dir = path self.current_dir = path
self.show_directory(path) self.show_directory(path)
@@ -99,38 +99,59 @@ class RepoViewScreen(Screen):
def show_file(self, path: str): def show_file(self, path: str):
files: DataTable = self.query_one("#files") files: DataTable = self.query_one("#files")
loading = self.query_one(LoadingIndicator) 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("#readme").display = False
self.query_one("#repo-info").display = False self.query_one("#repo-info").display = False
files.display = False files.display = False
open_file.display = False file_screen.display = False
loading.display = True loading.display = True
content = requests.get( file = requests.get(
self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/raw/{path}" self.app.GITEA_HOST + f"api/v1/repos/{self.owner_name}/{self.repo_name}/contents/{path}"
).text ).json()
languages = { languages = {
"py": "python", "py": "python",
"md": "markdown", "md": "markdown",
"h": "c", "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:] extension = path[path.rfind(".")+1:]
open_file.text = content decoded_text = base64.decodebytes(file["content"].encode()).decode()
open_file.text = decoded_text
try: try:
open_file.language = languages.get(extension, extension) open_file.language = languages.get(extension, extension)
except: except:
open_file.language = None 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 loading.display = False
open_file.display = True file_screen.display = True
def show_directory(self, path: str): def show_directory(self, path: str):
files: DataTable = self.query_one("#files") files: DataTable = self.query_one("#files")
self.query_one("#open-file").display = False self.query_one("#file-screen").display = False
files.clear(columns=True) files.clear(columns=True)
loading = self.query_one(LoadingIndicator) 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") #table.add_row("\ue5ff screens", "[d]switched from tabs to spaces", "[d]51 minutes ago")
yield table 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() yield LoadingIndicator()