From bcd3c228deff1e3d3e0754c6ab3e4fd10a52ceee Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Fri, 31 Oct 2025 14:44:53 +1100 Subject: [PATCH] implemented more right click options --- directory_tree_custom.py | 118 ++++++++---- plugin_loader.py | 340 +++++++++++++++++---------------- plugins/my plugin/lua/main.lua | 7 + plugins/my plugin/plugin.json | 6 + 4 files changed, 267 insertions(+), 204 deletions(-) create mode 100644 plugins/my plugin/lua/main.lua create mode 100644 plugins/my plugin/plugin.json diff --git a/directory_tree_custom.py b/directory_tree_custom.py index ce46b40..63248c8 100644 --- a/directory_tree_custom.py +++ b/directory_tree_custom.py @@ -9,52 +9,96 @@ import os, shutil class CustomDirectoryTree(DirectoryTree): - def __init__(self, path, *, name = None, id = None, classes = None, disabled = False): - super().__init__(path, name=name, id=id, classes=classes, disabled=disabled) - self.right_clicked_node: TreeNode | None = None + def __init__(self, path, *, name = None, id = None, classes = None, disabled = False): + super().__init__(path, name=name, id=id, classes=classes, disabled=disabled) + self.right_clicked_node: TreeNode | None = None - + - def context_menu_chosen(self, result): - if result == "Delete": - def delete_confirm(will_delete: bool | None): - if will_delete == True: - shutil.rmtree(self.right_clicked_node.data.path) - self.reload() + def context_menu_chosen(self, result): + if result == "Delete": + def delete_confirm(will_delete: bool | None): + if will_delete == True: + if os.path.isfile(self.right_clicked_node.data.path): + os.remove(self.right_clicked_node.data.path) + else: + shutil.rmtree(self.right_clicked_node.data.path) + self.reload() - self.notify(f"Deleted \"{self.right_clicked_node.data.path}\".") - self.right_clicked_node = None + self.notify(f"Deleted \"{self.right_clicked_node.data.path}\".") + self.right_clicked_node = None - self.app.push_screen(Prompt(f"Are you sure you want to delete \"{self.right_clicked_node.label}\"?", "confirm", "Confirm deletion"), delete_confirm) - elif result == "Rename": - def rename_confirm(new_name: str | None): - if new_name == None: return + self.app.push_screen(Prompt(f"Are you sure you want to delete \"{self.right_clicked_node.label}\"?", "confirm", "Confirm deletion"), delete_confirm) + elif result == "Rename": + def rename_confirm(new_name: str | None): + if new_name == None: return + if new_name.strip() == "": + self.notify("Filename can't be empty.", title="Failed to rename", severity="error") + return - os.rename(self.right_clicked_node.data.path, os.path.join(os.path.dirname(self.right_clicked_node.data.path), new_name)) - self.reload() + os.rename(self.right_clicked_node.data.path, os.path.join(os.path.dirname(self.right_clicked_node.data.path), new_name)) + self.reload() - self.notify("Renamed successfully.") + self.notify("Renamed successfully.") - self.right_clicked_node = None + self.right_clicked_node = None - self.app.push_screen(Prompt(f"Enter the new name for \"{self.right_clicked_node.label}\".", "string", "Rename"), rename_confirm) + self.app.push_screen(Prompt(f"Enter the new name for \"{self.right_clicked_node.label}\".", "string", "Rename"), rename_confirm) + elif result == "New Folder": + def new_folder(folder_name: str | None): + if folder_name == None: return + if folder_name.strip() == "": + self.notify("Folder name can't be empty.", title="Failed to create folder", severity="error") + return - def on_mouse_down(self, event: MouseDown): - if event.button != 3 or not "line" in event.style.meta: - return - selected_node = self.get_node_at_line(event.style.meta["line"]) - self.right_clicked_node = selected_node + new_folder_path = os.path.join(self.right_clicked_node.data.path, folder_name) + try: + os.mkdir(new_folder_path) + self.reload() + except Exception as e: + self.notify(str(e), title="Failed to create folder", severity="error") + return - options = None - if self._safe_is_dir(self.right_clicked_node.data.path): - options = ["New Folder", "New File", NoSelectStatic(f'[d]{"-" * 17}[/]'), "Delete", "Rename"] - else: - options = ["Delete", "Rename"] + self.app.push_screen(Prompt("Enter the name of the new folder.", "string", "Create New Folder"), new_folder) + elif result == "New File": + def new_file(file_name: str | None): + if file_name == None: return + if file_name.strip() == "": + self.notify("File name can't be empty.", title="Failed to create file", severity="error") + return + + new_file_path = os.path.join(self.right_clicked_node.data.path, file_name) - file_name = str(self.right_clicked_node.label) if len(self.right_clicked_node.label) <= 17 else self.right_clicked_node.label[:14] + "..." - - self.app.push_screen(ContextMenu( - [NoSelectStatic(f"[b]{file_name}[/]"), NoSelectStatic(f'[d]{"-" * 17}[/]')] + options, - event.screen_offset - ), self.context_menu_chosen) + if os.path.isfile(new_file_path): + self.notify("That file already exists.", title="Failed to create file", severity="error") + return + + try: + with open(new_file_path, "w") as f: + pass + self.reload() + except Exception as e: + self.notify(str(e), title="Failed to create file", severity="error") + return + + self.app.push_screen(Prompt("Enter the name of the new file", "string", "Create New File"), new_file) + + def on_mouse_down(self, event: MouseDown): + if event.button != 3 or not "line" in event.style.meta: + return + selected_node = self.get_node_at_line(event.style.meta["line"]) + self.right_clicked_node = selected_node + + options = None + if self._safe_is_dir(self.right_clicked_node.data.path): + options = ["New Folder", "New File", NoSelectStatic(f'[d]{"-" * 17}[/]'), "Delete", "Rename"] + else: + options = ["Delete", "Rename"] + + file_name = str(self.right_clicked_node.label) if len(self.right_clicked_node.label) <= 17 else self.right_clicked_node.label[:14] + "..." + + self.app.push_screen(ContextMenu( + [NoSelectStatic(f"[b]{file_name}[/]"), NoSelectStatic(f'[d]{"-" * 17}[/]')] + options, + event.screen_offset + ), self.context_menu_chosen) diff --git a/plugin_loader.py b/plugin_loader.py index 38c2ea0..d9cf0df 100644 --- a/plugin_loader.py +++ b/plugin_loader.py @@ -11,200 +11,206 @@ import os, json, asyncio class PluginLoader(Window): - def __init__(self): - super().__init__( - id="Plugin Loader", - mode="permanent", - icon="⚙️", - starting_horizontal="right", - starting_vertical="bottom", - start_open=True, - allow_maximize=True - ) + def __init__(self): + super().__init__( + id="Plugin Loader", + mode="permanent", + icon="⚙️", + starting_horizontal="right", + starting_vertical="bottom", + start_open=True, + allow_maximize=True + ) - # region Lua functions + # region Lua functions - def fake_notify(self, message: str, title: str = None, severity: str = "information"): - self.app.notify(message=message, title=title, severity=severity) + def fake_notify(self, message: str, title: str = None, severity: str = "information"): + self.app.notify(message=message, title=title, severity=severity) - def create_sidebar_button(self, icon: str): - new_button = Button(icon) - self.app.query_one("#sidebar-buttons").mount(new_button) + def create_sidebar_button(self, icon: str): + new_button = Button(icon) + self.app.query_one("#sidebar-buttons").mount(new_button) - def set_theme(self, theme_name: str): - self.app.theme = theme_name + def set_theme(self, theme_name: str): + self.app.theme = theme_name - def add_bind(self, action_name: str, key: str, description: str, show: bool = True): - # a bit of a sneaky way of doing things - #self.app.bind(key, action_name, description=description, show=show) - self.app._bindings.bind(key, action_name, description, show, priority=True) - self.app.refresh_bindings() + def add_bind(self, action_name: str, key: str, description: str, show: bool = True): + # a bit of a sneaky way of doing things + #self.app.bind(key, action_name, description=description, show=show) + self.app._bindings.bind(key, action_name, description, show, priority=True) + self.app.refresh_bindings() - def fake_run_action(self, action_name: str): - getattr(self.app, f"action_{action_name}")() + def fake_run_action(self, action_name: str): + getattr(self.app, f"action_{action_name}")() - def create_action(self, action_name: str, function): - def wrapper(): - function() + def create_action(self, action_name: str, function): + def wrapper(): + function() - setattr(self.app, f"action_{action_name}", wrapper) - - def create_widget(self, widget_type: str, *args): - return getattr(textual.widgets, widget_type)(*args) - - def run_on_message(self, event, function): - raise NotImplementedError("onMessage is not implemented yet.") + setattr(self.app, f"action_{action_name}", wrapper) + + def create_widget(self, widget_type: str, *args): + return getattr(textual.widgets, widget_type)(*args) + + def run_on_message(self, event, function): + raise NotImplementedError("onMessage is not implemented yet.") - def create_window(self, title: str, icon: str = "", show_title: bool = True, can_close: bool = True, can_maximize: bool = True, can_resize: bool = True, start_horizontal: str = "center", start_vertical: str = "middle"): - new_window = Window( - id=title, - mode="temporary" if can_close else 'permanent', - allow_maximize=can_maximize, - allow_resize=can_resize, - start_open=True, - icon=icon, - show_title=show_title, - starting_horizontal=start_horizontal, - starting_vertical=start_vertical - ) - #self.app.mount(new_window) - return new_window - - def create_setting(self, section_name: str, option_name: str, description: str, option_type: str, default_value: Any, on_changed_func = None): - self.app.config_handler.define_plugin_setting(section_name, option_name, description, option_type, default_value, on_changed_func) + def create_window(self, title: str, icon: str = "", show_title: bool = True, can_close: bool = True, can_maximize: bool = True, can_resize: bool = True, start_horizontal: str = "center", start_vertical: str = "middle"): + new_window = Window( + id=title, + mode="temporary" if can_close else 'permanent', + allow_maximize=can_maximize, + allow_resize=can_resize, + start_open=True, + icon=icon, + show_title=show_title, + starting_horizontal=start_horizontal, + starting_vertical=start_vertical + ) + #self.app.mount(new_window) + return new_window + + def create_setting(self, section_name: str, option_name: str, description: str, option_type: str, default_value: Any, on_changed_func = None): + self.app.config_handler.define_plugin_setting(section_name, option_name, description, option_type, default_value, on_changed_func) - # endregion + # endregion - @work - async def find_plugins(self): - log = self.query_one(RichLog) + @work + async def find_plugins(self): + log = self.query_one(RichLog) - no_errors = True + no_errors = True - log.write("[b]Setting up LUA runtime..[/]") - self.lua_runtime = lua51.LuaRuntime() + log.write("[b]Setting up LUA runtime..[/]") + self.lua_runtime = lua51.LuaRuntime() - lua_runtime_stuff = { - "ui": { - "notify": self.fake_notify, - "runAction": self.fake_run_action, - "addBind": self.add_bind, - "createAction": self.create_action, - "createWindow": self.create_window, - "createWidget": self.create_widget, - "app": self.app, - "onMessage": self.run_on_message - }, - "config": { - "get": self.app.config_handler.get, - "defineSetting": self.create_setting - } - } + lua_runtime_stuff = { + "ui": { + "notify": self.fake_notify, + "runAction": self.fake_run_action, + "addBind": self.add_bind, + "createAction": self.create_action, + "createWindow": self.create_window, + "createWidget": self.create_widget, + "app": self.app, + "onMessage": self.run_on_message + }, + "config": { + "get": self.app.config_handler.get, + "defineSetting": self.create_setting + } + } - log.write("[b]Finding plugins...[/]") + log.write("[b]Finding plugins...[/]") - # Find all plugins (they're just in the plugins folder for now) - folders = [ - os.path.join(os.path.dirname(__file__), "plugins") - ] - # path to the folder of all correctly formatted plugins - plugin_paths = [] + # Find all plugins (they're just in the plugins folder for now) + folders = [ + os.path.join(os.path.dirname(__file__), "plugins") + ] + # path to the folder of all correctly formatted plugins + plugin_paths = [] - for folder in folders: - log.write(f"Searching {folder}...") - plugin_folders = os.listdir(folder) + for folder in folders: + log.write(f"Searching {folder}...") + plugin_folders = os.listdir(folder) - for plugin_folder in plugin_folders: - plugin_folder = os.path.join(folder, plugin_folder) + for plugin_folder in plugin_folders: + plugin_folder = os.path.join(folder, plugin_folder) - if not os.path.isdir(plugin_folder): - log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]") - no_errors = False - continue + if not os.path.isdir(plugin_folder): + log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]") + no_errors = False + continue - if not os.path.isdir(os.path.join(plugin_folder, "lua")): - log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]") - no_errors = False - continue + if not os.path.isdir(os.path.join(plugin_folder, "lua")): + log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]") + no_errors = False + continue - if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")): - log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]") - no_errors = False - continue + if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")): + log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]") + no_errors = False + continue - with open(os.path.join(plugin_folder, "plugin.json"), "r") as f: - try: - plugin_json = json.loads(f.read()) - plugin_json["name"] - plugin_json["version"] - plugin_json["author"] - plugin_json["dependencies"] - except UnicodeDecodeError: - log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]") - no_errors = False - continue - except json.JSONDecodeError: - log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]") - no_errors = False - continue - except KeyError as e: - log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.") - no_errors = False - continue - except Exception as e: - log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]") - no_errors = False - continue + with open(os.path.join(plugin_folder, "plugin.json"), "r") as f: + try: + plugin_json = json.loads(f.read()) + plugin_json["name"] + plugin_json["version"] + plugin_json["author"] + plugin_json["dependencies"] + except UnicodeDecodeError: + log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]") + no_errors = False + continue + except json.JSONDecodeError: + log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]") + no_errors = False + continue + except KeyError as e: + log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.") + no_errors = False + continue + except Exception as e: + log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]") + no_errors = False + continue - log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})") - for lua_file in os.listdir(os.path.join(plugin_folder, "lua")): - lua_file_path = os.path.join(plugin_folder, f"lua/{lua_file}") - - with open(lua_file_path, "r") as f: - code = f.read() - sandbox = self.lua_runtime.eval("{}") - setfenv = self.lua_runtime.eval("setfenv") + log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})") + for lua_file in os.listdir(os.path.join(plugin_folder, "lua")): + lua_file_path = os.path.join(plugin_folder, f"lua/{lua_file}") + + with open(lua_file_path, "r") as f: + code = f.read() + sandbox = self.lua_runtime.eval("{}") + setfenv = self.lua_runtime.eval("setfenv") - sandbox.print = self.log #self.lua_runtime.globals().print - sandbox.math = self.lua_runtime.globals().math - sandbox.string = self.lua_runtime.globals().string - sandbox.tostring = self.lua_runtime.globals().tostring - sandbox.tonumber = self.lua_runtime.globals().tonumber - sandbox.berry = lua_runtime_stuff + sandbox.print = self.log #self.lua_runtime.globals().print + sandbox.math = self.lua_runtime.globals().math + sandbox.string = self.lua_runtime.globals().string + sandbox.tostring = self.lua_runtime.globals().tostring + sandbox.tonumber = self.lua_runtime.globals().tonumber + sandbox.berry = lua_runtime_stuff - setfenv(0, sandbox) - try: - executed_code = self.lua_runtime.execute(code) - except lua51.LuaError as e: - log.write(f"[b red]Error in {lua_file_path}:\n" + '\n'.join(format_exception(e)) + "[/]") - self.notify("There was Lua error while loading one your installed plugins. Check the Plugin Loader window for more details.", title="Lua Error", severity="error", timeout=10) - no_errors = False - continue - - if executed_code.run: - try: - executed_code.run() - except Exception as e: - log.write(f"[b red]Runtime error in {lua_file_path}:\n" + "\n".join(format_exception(e)) + "[/]") - self.notify("A plugin has created an error. Check the log for more details.", title="Lua Error", severity="error", timeout=10) - no_errors = False - continue - - plugin_paths.append(plugin_folder) + setfenv(0, sandbox) + try: + executed_code = self.lua_runtime.execute(code) + except lua51.LuaError as e: + log.write(f"[b red]Error in {lua_file_path}:\n" + '\n'.join(format_exception(e)) + "[/]") + self.notify("There was Lua error while loading one your installed plugins. Check the Plugin Loader window for more details.", title="Lua Error", severity="error", timeout=10) + no_errors = False + continue + + try: + if executed_code.run: + try: + executed_code.run() + except Exception as e: + log.write(f"[b red]Runtime error in {lua_file_path}:\n" + "\n".join(format_exception(e)) + "[/]") + self.notify("A plugin has created an error. Check the log for more details.", title="Lua Error", severity="error", timeout=10) + no_errors = False + continue + except: + log.write(f"[b red]Error in {lua_file_path}: file returned invalid value, you should only return a table.") + self.notify("A plugin has created an error. Check the log for more details.", title="Lua Error", severity="error", timeout=10) + no_errors = False + continue + + plugin_paths.append(plugin_folder) - log.write("\n[b]Done loading plugins![/]") - if no_errors and int(self.app.config_handler.get("plugins", "log_timeout")) != -1: - log.write(f'[d]Window will automatically close in {self.app.config_handler.get("plugins", "log_timeout")} seconds.[/]') - await asyncio.sleep(int(self.app.config_handler.get("plugins", "log_timeout"))) - self.close_window() + log.write("\n[b]Done loading plugins![/]") + if no_errors and int(self.app.config_handler.get("plugins", "log_timeout")) != -1: + log.write(f'[d]Window will automatically close in {self.app.config_handler.get("plugins", "log_timeout")} seconds.[/]') + await asyncio.sleep(int(self.app.config_handler.get("plugins", "log_timeout"))) + self.close_window() - async def on_mount(self): - if bool(int(self.app.config_handler.get("plugins", "log"))) == False: - self.display = "none" + async def on_mount(self): + if bool(int(self.app.config_handler.get("plugins", "log"))) == False: + self.display = "none" - self.find_plugins() - + self.find_plugins() + - def compose(self): - yield RichLog(markup=True, id="plugins-log", wrap=True) \ No newline at end of file + def compose(self): + yield RichLog(markup=True, id="plugins-log", wrap=True) \ No newline at end of file diff --git a/plugins/my plugin/lua/main.lua b/plugins/my plugin/lua/main.lua new file mode 100644 index 0000000..86211cf --- /dev/null +++ b/plugins/my plugin/lua/main.lua @@ -0,0 +1,7 @@ + local plugin = {} + +function plugin.run() + berry.config.defineSetting("my plugin", "test setting", "description", "boolean", "1") +end + +return plugin \ No newline at end of file diff --git a/plugins/my plugin/plugin.json b/plugins/my plugin/plugin.json new file mode 100644 index 0000000..6bdf85c --- /dev/null +++ b/plugins/my plugin/plugin.json @@ -0,0 +1,6 @@ +{ + "name": "my plugin", + "author": "SpookyDervish", + "version": "1.0.0", + "dependencies": [] +} \ No newline at end of file