working on more stuff for the berry.ui library
This commit is contained in:
37
main.py
37
main.py
@@ -10,10 +10,21 @@ from textual_fspicker import FileOpen, FileSave
|
||||
|
||||
from plugin_loader import PluginLoader
|
||||
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
|
||||
class Watcher(FileSystemEventHandler):
|
||||
def __init__(self, app: App):
|
||||
super().__init__()
|
||||
self.app = app
|
||||
|
||||
async def on_any_event(self, event):
|
||||
await self.app.query_one(DirectoryTree).reload()
|
||||
|
||||
class Berry(App):
|
||||
CSS_PATH = "assets/style.tcss"
|
||||
SUB_TITLE = "New File"
|
||||
@@ -26,6 +37,10 @@ class Berry(App):
|
||||
Binding("ctrl+f", "find", "Find", priority=True)
|
||||
]
|
||||
|
||||
def __init__(self, path: str):
|
||||
super().__init__()
|
||||
self.path = path
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header()
|
||||
with Vertical(id="sidebar"):
|
||||
@@ -36,7 +51,7 @@ class Berry(App):
|
||||
with ContentSwitcher(initial="files"):
|
||||
with Vertical(id="files"):
|
||||
yield Static("EXPLORER")
|
||||
yield DirectoryTree("./", id="directory")
|
||||
yield DirectoryTree(self.path, id="directory")
|
||||
|
||||
with Vertical(id="editor"):
|
||||
first_tab = Tab("New File")
|
||||
@@ -46,7 +61,7 @@ class Berry(App):
|
||||
first_tab,
|
||||
id="file-tabs"
|
||||
)
|
||||
yield TextArea.code_editor(placeholder="This file is empty.", language="python", theme="css", id="code-editor", disabled=True)
|
||||
yield TextArea.code_editor(placeholder="This file is empty.", theme="css", id="code-editor", disabled=True)
|
||||
|
||||
#if os.name == "nt":
|
||||
with Vertical(id="console-container"):
|
||||
@@ -64,7 +79,7 @@ class Berry(App):
|
||||
|
||||
self.run_command(event.input.value)
|
||||
event.input.clear()
|
||||
|
||||
|
||||
def run_command(self, command: str):
|
||||
console = self.query_one("#console")
|
||||
|
||||
@@ -248,7 +263,11 @@ class Berry(App):
|
||||
|
||||
with open(result, "wb") as f:
|
||||
f.write(self.query_one("#code-editor").text.encode())
|
||||
|
||||
tabs: Tabs = self.query_one("#file-tabs")
|
||||
tabs.active_tab.label = os.path.basename(result)
|
||||
self.notify(f"Saved to {result} successfully.", title="Done!", markup=False)
|
||||
self.query_one(DirectoryTree).reload()
|
||||
|
||||
def action_save_as(self):
|
||||
self.push_screen(FileSave(), callback=self.done_saving)
|
||||
@@ -269,6 +288,11 @@ class Berry(App):
|
||||
self.unsaved_files.pop(self.open_file)
|
||||
self.notify("Saved.")
|
||||
|
||||
def action_quit(self):
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
return super().action_quit()
|
||||
|
||||
def on_ready(self):
|
||||
# src/main.py: Tab<>
|
||||
self.file_tabs = {}
|
||||
@@ -277,12 +301,15 @@ class Berry(App):
|
||||
self.switching = False
|
||||
self.file_clicked = False
|
||||
|
||||
self.observer = Observer()
|
||||
self.observer.schedule(Watcher(self), path=self.path)
|
||||
self.observer.start()
|
||||
|
||||
#if os.name == "nt":
|
||||
self.query_one("#console").write("Run a command below.")
|
||||
#else:
|
||||
# self.query_one("#terminal").start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Berry()
|
||||
app = Berry("./")
|
||||
app.run()
|
||||
Reference in New Issue
Block a user