From 63614988f263764dc6b693b3559ac8deb2161afc Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Mon, 27 Oct 2025 21:27:50 +1100 Subject: [PATCH] implementing more plugin functionality --- plugin_loader.py | 37 ++++++++++++++++++++++---------- plugins/test-plugin/lua/main.lua | 12 ++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/plugin_loader.py b/plugin_loader.py index a2a018e..4054498 100644 --- a/plugin_loader.py +++ b/plugin_loader.py @@ -1,7 +1,9 @@ from textual_window import Window from textual.widgets import RichLog, Button -from textual import work -from textual.binding import Binding +from textual import work, on +import textual.widgets +from traceback import format_exception + from lupa import lua51 import os, json, asyncio @@ -45,6 +47,12 @@ class PluginLoader(Window): 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_event(self, event, function): + raise NotImplementedError("runOnEvent 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( @@ -58,7 +66,7 @@ class PluginLoader(Window): starting_horizontal=start_horizontal, starting_vertical=start_vertical ) - self.app.mount(new_window) + #self.app.mount(new_window) return new_window # endregion @@ -76,12 +84,15 @@ class PluginLoader(Window): "ui": { "notify": self.fake_notify, #"createSidebarButton": self.create_sidebar_button, - "setTheme": self.set_theme, + #"setTheme": self.set_theme, "runAction": self.fake_run_action, "addBind": self.add_bind, "createAction": self.create_action, - "createWindow": self.create_window - } + "createWindow": self.create_window, + "createWidget": self.create_widget, + "app": self.app, + "runOnEvent": self.run_on_event + }, } log.write("[b]Finding plugins...[/]") @@ -160,15 +171,19 @@ class PluginLoader(Window): try: executed_code = self.lua_runtime.execute(code) except lua51.LuaError as e: - log.write(f"[b red]Error in {lua_file_path}: {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.init: - executed_code.init() - if executed_code.main: - self.run_worker(thread=True, work=executed_code.main) + 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) diff --git a/plugins/test-plugin/lua/main.lua b/plugins/test-plugin/lua/main.lua index e9f07c5..fed5872 100644 --- a/plugins/test-plugin/lua/main.lua +++ b/plugins/test-plugin/lua/main.lua @@ -1,12 +1,14 @@ local plugin = {} -function testAction() +function plugin.run() local window = berry.ui.createWindow("test window") -end + berry.ui.app.mount(window) + berry.ui.runOnEvent(window.Initialized, function() + local test = berry.ui.createWidget("Static", "the absolute minimal experience") + window.mount_in_window(test) + end) -function plugin.init() - berry.ui.createAction("test", testAction) - berry.ui.addBind("test", "ctrl+d", "Test") + end return plugin \ No newline at end of file