open folders

This commit is contained in:
2026-05-16 21:51:10 +10:00
parent b43392d0b7
commit fe184e1c2a
3 changed files with 50 additions and 8 deletions

View File

@@ -45,15 +45,16 @@ class HomePage(Vertical):
#bindings {
Horizontal {
max-width: 50;
width: 50%;
margin-top: 1;
height: 1;
.bind-name {
width: 85%;
width: 75%;
}
.bind-keys {
width: 15%;
width: 25%;
}
Static {
@@ -94,9 +95,13 @@ class HomePage(Vertical):
yield Static(f"[@click=app.new]{binds["new"]}[/]", classes="bind-keys")
with Horizontal():
yield Static("[b] Open file[/]", classes="bind-name")
yield Static("[b] Open file[/]", classes="bind-name")
yield Static(f"[@click=app.open]{binds["open"]}[/]", classes="bind-keys")
with Horizontal():
yield Static("[b] Open Folder[/]", classes="bind-name")
yield Static(f"[@click=app.open_folder]{binds["open-folder"]}[/]", classes="bind-keys")
with Horizontal():
yield Static("[b] Settings[/]", classes="bind-name")
yield Static(f"[@click=app.settings]{binds["settings"]}[/]", classes="bind-keys")

31
main.py
View File

@@ -4,7 +4,7 @@ from textual.containers import HorizontalGroup, Vertical
from textual.binding import Binding
from textual_window import Window
from textual import on
from textual_fspicker import FileOpen, FileSave
from textual_fspicker import FileOpen, FileSave, SelectDirectory
from pathlib import Path
@@ -37,16 +37,17 @@ class Berry(App):
def __init__(self, path: str):
self.config_handler = ConfigHandler(self)
self.bindings = eval(self.config_handler.get("editor", "bindings"))
super().__init__()
self.config_handler = ConfigHandler(self)
self.bindings = eval(self.config_handler.get("editor", "bindings"))
self.path = path
self.BINDINGS = (
Binding(self.bindings["new"], "new", "New File"),
Binding(self.bindings["open"], "open", "Open File"),
Binding(self.bindings["open-folder"], "open_folder", "Open Folder"),
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", priority=True), # this is priority because otherwise TextArea overrides it
@@ -161,6 +162,30 @@ class Berry(App):
def action_open(self):
self.app.push_screen(FileOpen(), self.chose_file_to_open)
async def choose_folder_to_open(self, result):
if result == None: return
self.path = result
tabs: Tabs = self.query_one("#file-tabs")
tabs.clear()
first_tab = Tab("Home")
first_tab.file_path = "berry://home"
self.file_tabs["berry://home"] = first_tab
tabs.add_tab(first_tab)
self.current_editor.remove_children()
self.current_editor.mount(HomePage())
file_tree: CustomDirectoryTree = self.query_one("#directory")
file_tree.path = result
def action_open_folder(self):
self.push_screen(SelectDirectory(), self.choose_folder_to_open)
def action_find(self):
try:
self.query_one("#find-window")

View File

@@ -14,6 +14,8 @@ class ConfigHandler:
self.config_dir: str = self.ensure_hidden_config_dir()
self.load_settings()
def define_plugin_setting(self, plugin_name: str, option_name: str, description: str, option_type: str, default_value: Any, on_changed_func = None):
actual_type = None
if option_type == "boolean":
@@ -88,10 +90,19 @@ class ConfigHandler:
def load_settings(self):
if os.path.isfile(self.config_dir / "config.ini"):
self.config.read(self.config_dir / "config.ini")
if int(self.get("config", "version")) < 2:
self.app.notify(
message="Your config version is out of date! We've loaded the defaults but your config is untouched.",
title="Config Version Outdated",
severity="warning",
timeout=15
)
else:
return
self.config["config"] = {
"version": "1"
"version": "2"
}
self.config["editor"] = {
"word_wrap": "0",
@@ -99,6 +110,7 @@ class ConfigHandler:
"default_editors": {},
"bindings": {
"open": "ctrl+o",
"open-folder": "ctrl+shift+o",
"new": "ctrl+n",
"save": "ctrl+s",
"save-as": "ctrl+shift+s",