continued adding support for other text editors, added a home screen
This commit is contained in:
68
main.py
68
main.py
@@ -8,6 +8,8 @@ from textual_fspicker import FileOpen, FileSave
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from home_page import HomePage
|
||||
|
||||
from assets.theme_mappings import theme_mappings
|
||||
from plugin_loader import PluginLoader
|
||||
from settings import SettingsScreen
|
||||
@@ -32,19 +34,32 @@ class Berry(App):
|
||||
CSS_PATH = "assets/style.tcss"
|
||||
SUB_TITLE = "New File"
|
||||
|
||||
BINDINGS = [
|
||||
Binding("ctrl+o", "open", "Open File"),
|
||||
Binding("ctrl+n", "new", "New File"),
|
||||
Binding("ctrl+s", "save", "Save"),
|
||||
Binding("ctrl+shift+s", "save_as", "Save As...", priority=True),
|
||||
Binding("ctrl+f", "find", "Find", priority=True),
|
||||
Binding("ctrl+f1", "settings", "Settings")
|
||||
]
|
||||
|
||||
|
||||
def __init__(self, path: str):
|
||||
self.config_handler = ConfigHandler(self)
|
||||
|
||||
self.bindings = eval(self.config_handler.get("editor", "bindings"))
|
||||
|
||||
super().__init__()
|
||||
self.path = path
|
||||
self.config_handler = ConfigHandler(self)
|
||||
|
||||
self.BINDINGS = (
|
||||
Binding(self.bindings["new"], "new", "New File"),
|
||||
Binding(self.bindings["open"], "open", "Open File"),
|
||||
Binding(self.bindings["save"], "save", "Save File"),
|
||||
Binding(self.bindings["save-as"], "save_as", "Save File As..."),
|
||||
Binding(self.bindings["find"], "find", "Find and Replace"),
|
||||
Binding(self.bindings["settings"], "settings", "Settings"),
|
||||
)
|
||||
|
||||
for binding in self.BINDINGS:
|
||||
self._bindings._add_binding(binding)
|
||||
|
||||
self.refresh_bindings()
|
||||
|
||||
self.current_editor = None
|
||||
self.file_tabs = {}
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header()
|
||||
@@ -59,14 +74,18 @@ class Berry(App):
|
||||
yield CustomDirectoryTree(self.path, id="directory")
|
||||
|
||||
with Vertical(id="editor"):
|
||||
first_tab = Tab("New File")
|
||||
first_tab.file_path = None
|
||||
first_tab = Tab("Home")
|
||||
first_tab.file_path = "berry://home"
|
||||
self.file_tabs["berry://home"] = first_tab
|
||||
|
||||
yield Tabs(
|
||||
first_tab,
|
||||
id="file-tabs"
|
||||
)
|
||||
yield TextArea.code_editor(placeholder="This file is empty.", theme="css", id="code-editor", disabled=True, soft_wrap=True, show_line_numbers=bool(int(self.config_handler.get("editor", "line_numbers"))))
|
||||
|
||||
with Vertical() as editor:
|
||||
yield HomePage()
|
||||
self.current_editor = editor
|
||||
|
||||
#if os.name == "nt":
|
||||
#with Vertical(id="console-container"):
|
||||
@@ -201,7 +220,9 @@ class Berry(App):
|
||||
tabs.active = self.file_tabs[str(event.path)].id
|
||||
|
||||
def open_text_editor(self, file_content: bytes, file_extension: str):
|
||||
code_editor: TextArea = self.query_one("#code-editor")
|
||||
code_editor = TextArea.code_editor(placeholder="This file is empty.", theme="css", disabled=True, soft_wrap=bool(int(self.config_handler.get("editor", "word_wrap"))), show_line_numbers=bool(int(self.config_handler.get("editor", "line_numbers"))))
|
||||
|
||||
self.current_editor.mount(code_editor)
|
||||
|
||||
code_editor.language = theme_mappings.get(file_extension, None)
|
||||
|
||||
@@ -215,10 +236,12 @@ class Berry(App):
|
||||
|
||||
code_editor.focus()
|
||||
|
||||
def open_editor(self, file_extension: str, file_content: str):
|
||||
def open_editor(self, file_extension: str, file_content: bytes):
|
||||
default_editors: dict = eval(self.config_handler.get("editor", "default_editors"))
|
||||
editor_for_file = default_editors.get(file_extension, None)
|
||||
|
||||
|
||||
self.current_editor.remove_children()
|
||||
|
||||
if not editor_for_file:
|
||||
self.open_text_editor(file_content, file_extension)
|
||||
|
||||
@@ -230,8 +253,12 @@ class Berry(App):
|
||||
else:
|
||||
self.open_file = getattr(event.tab, "file_path")
|
||||
|
||||
if self.open_file and self.open_file.startswith(r"berry://"):
|
||||
return
|
||||
|
||||
self.switching = True
|
||||
|
||||
|
||||
content = b""
|
||||
file_extension = ""
|
||||
|
||||
@@ -265,8 +292,6 @@ class Berry(App):
|
||||
event.window.remove_window()
|
||||
|
||||
def on_text_area_changed(self, event: TextArea.Changed):
|
||||
if event.text_area.id != "code-editor":
|
||||
return
|
||||
if self.switching:
|
||||
self.switching = False
|
||||
return
|
||||
@@ -301,10 +326,9 @@ class Berry(App):
|
||||
|
||||
self.open_file = None
|
||||
self.switching = True
|
||||
code_editor: TextArea = self.query_one("#code-editor")
|
||||
|
||||
code_editor.disabled = False
|
||||
code_editor.text = ""
|
||||
|
||||
# open empty editor
|
||||
self.open_editor("", b"")
|
||||
|
||||
def done_saving(self, result):
|
||||
if result is None: return
|
||||
@@ -342,8 +366,6 @@ class Berry(App):
|
||||
return super().action_quit()
|
||||
|
||||
def on_ready(self):
|
||||
# src/main.py: Tab<>
|
||||
self.file_tabs = {}
|
||||
self.open_file = None
|
||||
self.unsaved_files = {} # list of paths
|
||||
self.switching = False
|
||||
|
||||
Reference in New Issue
Block a user