implemented more right click options

This commit is contained in:
2025-10-31 14:44:53 +11:00
parent 3ad310622a
commit bcd3c228de
4 changed files with 267 additions and 204 deletions

View File

@@ -9,52 +9,96 @@ import os, shutil
class CustomDirectoryTree(DirectoryTree): class CustomDirectoryTree(DirectoryTree):
def __init__(self, path, *, name = None, id = None, classes = None, disabled = False): def __init__(self, path, *, name = None, id = None, classes = None, disabled = False):
super().__init__(path, name=name, id=id, classes=classes, disabled=disabled) super().__init__(path, name=name, id=id, classes=classes, disabled=disabled)
self.right_clicked_node: TreeNode | None = None self.right_clicked_node: TreeNode | None = None
def context_menu_chosen(self, result): def context_menu_chosen(self, result):
if result == "Delete": if result == "Delete":
def delete_confirm(will_delete: bool | None): def delete_confirm(will_delete: bool | None):
if will_delete == True: if will_delete == True:
shutil.rmtree(self.right_clicked_node.data.path) if os.path.isfile(self.right_clicked_node.data.path):
self.reload() 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.notify(f"Deleted \"{self.right_clicked_node.data.path}\".")
self.right_clicked_node = None 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) 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": elif result == "Rename":
def rename_confirm(new_name: str | None): def rename_confirm(new_name: str | None):
if new_name == None: return 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)) 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.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): new_folder_path = os.path.join(self.right_clicked_node.data.path, folder_name)
if event.button != 3 or not "line" in event.style.meta: try:
return os.mkdir(new_folder_path)
selected_node = self.get_node_at_line(event.style.meta["line"]) self.reload()
self.right_clicked_node = selected_node except Exception as e:
self.notify(str(e), title="Failed to create folder", severity="error")
return
options = None self.app.push_screen(Prompt("Enter the name of the new folder.", "string", "Create New Folder"), new_folder)
if self._safe_is_dir(self.right_clicked_node.data.path): elif result == "New File":
options = ["New Folder", "New File", NoSelectStatic(f'[d]{"-" * 17}[/]'), "Delete", "Rename"] def new_file(file_name: str | None):
else: if file_name == None: return
options = ["Delete", "Rename"] if file_name.strip() == "":
self.notify("File name can't be empty.", title="Failed to create file", severity="error")
return
file_name = str(self.right_clicked_node.label) if len(self.right_clicked_node.label) <= 17 else self.right_clicked_node.label[:14] + "..." new_file_path = os.path.join(self.right_clicked_node.data.path, file_name)
self.app.push_screen(ContextMenu( if os.path.isfile(new_file_path):
[NoSelectStatic(f"[b]{file_name}[/]"), NoSelectStatic(f'[d]{"-" * 17}[/]')] + options, self.notify("That file already exists.", title="Failed to create file", severity="error")
event.screen_offset return
), self.context_menu_chosen)
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)

View File

@@ -11,200 +11,206 @@ import os, json, asyncio
class PluginLoader(Window): class PluginLoader(Window):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
id="Plugin Loader", id="Plugin Loader",
mode="permanent", mode="permanent",
icon="⚙️", icon="⚙️",
starting_horizontal="right", starting_horizontal="right",
starting_vertical="bottom", starting_vertical="bottom",
start_open=True, start_open=True,
allow_maximize=True allow_maximize=True
) )
# region Lua functions # region Lua functions
def fake_notify(self, message: str, title: str = None, severity: str = "information"): def fake_notify(self, message: str, title: str = None, severity: str = "information"):
self.app.notify(message=message, title=title, severity=severity) self.app.notify(message=message, title=title, severity=severity)
def create_sidebar_button(self, icon: str): def create_sidebar_button(self, icon: str):
new_button = Button(icon) new_button = Button(icon)
self.app.query_one("#sidebar-buttons").mount(new_button) self.app.query_one("#sidebar-buttons").mount(new_button)
def set_theme(self, theme_name: str): def set_theme(self, theme_name: str):
self.app.theme = theme_name self.app.theme = theme_name
def add_bind(self, action_name: str, key: str, description: str, show: bool = True): def add_bind(self, action_name: str, key: str, description: str, show: bool = True):
# a bit of a sneaky way of doing things # a bit of a sneaky way of doing things
#self.app.bind(key, action_name, description=description, show=show) #self.app.bind(key, action_name, description=description, show=show)
self.app._bindings.bind(key, action_name, description, show, priority=True) self.app._bindings.bind(key, action_name, description, show, priority=True)
self.app.refresh_bindings() self.app.refresh_bindings()
def fake_run_action(self, action_name: str): def fake_run_action(self, action_name: str):
getattr(self.app, f"action_{action_name}")() getattr(self.app, f"action_{action_name}")()
def create_action(self, action_name: str, function): def create_action(self, action_name: str, function):
def wrapper(): def wrapper():
function() function()
setattr(self.app, f"action_{action_name}", wrapper) setattr(self.app, f"action_{action_name}", wrapper)
def create_widget(self, widget_type: str, *args): def create_widget(self, widget_type: str, *args):
return getattr(textual.widgets, widget_type)(*args) return getattr(textual.widgets, widget_type)(*args)
def run_on_message(self, event, function): def run_on_message(self, event, function):
raise NotImplementedError("onMessage is not implemented yet.") 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"): 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( new_window = Window(
id=title, id=title,
mode="temporary" if can_close else 'permanent', mode="temporary" if can_close else 'permanent',
allow_maximize=can_maximize, allow_maximize=can_maximize,
allow_resize=can_resize, allow_resize=can_resize,
start_open=True, start_open=True,
icon=icon, icon=icon,
show_title=show_title, show_title=show_title,
starting_horizontal=start_horizontal, starting_horizontal=start_horizontal,
starting_vertical=start_vertical starting_vertical=start_vertical
) )
#self.app.mount(new_window) #self.app.mount(new_window)
return 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): 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) self.app.config_handler.define_plugin_setting(section_name, option_name, description, option_type, default_value, on_changed_func)
# endregion # endregion
@work @work
async def find_plugins(self): async def find_plugins(self):
log = self.query_one(RichLog) log = self.query_one(RichLog)
no_errors = True no_errors = True
log.write("[b]Setting up LUA runtime..[/]") log.write("[b]Setting up LUA runtime..[/]")
self.lua_runtime = lua51.LuaRuntime() self.lua_runtime = lua51.LuaRuntime()
lua_runtime_stuff = { lua_runtime_stuff = {
"ui": { "ui": {
"notify": self.fake_notify, "notify": self.fake_notify,
"runAction": self.fake_run_action, "runAction": self.fake_run_action,
"addBind": self.add_bind, "addBind": self.add_bind,
"createAction": self.create_action, "createAction": self.create_action,
"createWindow": self.create_window, "createWindow": self.create_window,
"createWidget": self.create_widget, "createWidget": self.create_widget,
"app": self.app, "app": self.app,
"onMessage": self.run_on_message "onMessage": self.run_on_message
}, },
"config": { "config": {
"get": self.app.config_handler.get, "get": self.app.config_handler.get,
"defineSetting": self.create_setting "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) # Find all plugins (they're just in the plugins folder for now)
folders = [ folders = [
os.path.join(os.path.dirname(__file__), "plugins") os.path.join(os.path.dirname(__file__), "plugins")
] ]
# path to the folder of all correctly formatted plugins # path to the folder of all correctly formatted plugins
plugin_paths = [] plugin_paths = []
for folder in folders: for folder in folders:
log.write(f"Searching {folder}...") log.write(f"Searching {folder}...")
plugin_folders = os.listdir(folder) plugin_folders = os.listdir(folder)
for plugin_folder in plugin_folders: for plugin_folder in plugin_folders:
plugin_folder = os.path.join(folder, plugin_folder) plugin_folder = os.path.join(folder, plugin_folder)
if not os.path.isdir(plugin_folder): if not os.path.isdir(plugin_folder):
log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]") log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]")
no_errors = False no_errors = False
continue continue
if not os.path.isdir(os.path.join(plugin_folder, "lua")): if not os.path.isdir(os.path.join(plugin_folder, "lua")):
log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]") log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]")
no_errors = False no_errors = False
continue continue
if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")): 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.[/]") log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]")
no_errors = False no_errors = False
continue continue
with open(os.path.join(plugin_folder, "plugin.json"), "r") as f: with open(os.path.join(plugin_folder, "plugin.json"), "r") as f:
try: try:
plugin_json = json.loads(f.read()) plugin_json = json.loads(f.read())
plugin_json["name"] plugin_json["name"]
plugin_json["version"] plugin_json["version"]
plugin_json["author"] plugin_json["author"]
plugin_json["dependencies"] plugin_json["dependencies"]
except UnicodeDecodeError: except UnicodeDecodeError:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]")
no_errors = False no_errors = False
continue continue
except json.JSONDecodeError: except json.JSONDecodeError:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]")
no_errors = False no_errors = False
continue continue
except KeyError as e: except KeyError as e:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.")
no_errors = False no_errors = False
continue continue
except Exception as e: except Exception as e:
log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]") log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]")
no_errors = False no_errors = False
continue continue
log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})") log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})")
for lua_file in os.listdir(os.path.join(plugin_folder, "lua")): for lua_file in os.listdir(os.path.join(plugin_folder, "lua")):
lua_file_path = os.path.join(plugin_folder, f"lua/{lua_file}") lua_file_path = os.path.join(plugin_folder, f"lua/{lua_file}")
with open(lua_file_path, "r") as f: with open(lua_file_path, "r") as f:
code = f.read() code = f.read()
sandbox = self.lua_runtime.eval("{}") sandbox = self.lua_runtime.eval("{}")
setfenv = self.lua_runtime.eval("setfenv") setfenv = self.lua_runtime.eval("setfenv")
sandbox.print = self.log #self.lua_runtime.globals().print sandbox.print = self.log #self.lua_runtime.globals().print
sandbox.math = self.lua_runtime.globals().math sandbox.math = self.lua_runtime.globals().math
sandbox.string = self.lua_runtime.globals().string sandbox.string = self.lua_runtime.globals().string
sandbox.tostring = self.lua_runtime.globals().tostring sandbox.tostring = self.lua_runtime.globals().tostring
sandbox.tonumber = self.lua_runtime.globals().tonumber sandbox.tonumber = self.lua_runtime.globals().tonumber
sandbox.berry = lua_runtime_stuff sandbox.berry = lua_runtime_stuff
setfenv(0, sandbox) setfenv(0, sandbox)
try: try:
executed_code = self.lua_runtime.execute(code) executed_code = self.lua_runtime.execute(code)
except lua51.LuaError as e: except lua51.LuaError as e:
log.write(f"[b red]Error in {lua_file_path}:\n" + '\n'.join(format_exception(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) 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 no_errors = False
continue continue
if executed_code.run: try:
try: if executed_code.run:
executed_code.run() try:
except Exception as e: executed_code.run()
log.write(f"[b red]Runtime error in {lua_file_path}:\n" + "\n".join(format_exception(e)) + "[/]") except Exception as e:
self.notify("A plugin has created an error. Check the log for more details.", title="Lua Error", severity="error", timeout=10) log.write(f"[b red]Runtime error in {lua_file_path}:\n" + "\n".join(format_exception(e)) + "[/]")
no_errors = False self.notify("A plugin has created an error. Check the log for more details.", title="Lua Error", severity="error", timeout=10)
continue 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) plugin_paths.append(plugin_folder)
log.write("\n[b]Done loading plugins![/]") log.write("\n[b]Done loading plugins![/]")
if no_errors and int(self.app.config_handler.get("plugins", "log_timeout")) != -1: 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.[/]') 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"))) await asyncio.sleep(int(self.app.config_handler.get("plugins", "log_timeout")))
self.close_window() self.close_window()
async def on_mount(self): async def on_mount(self):
if bool(int(self.app.config_handler.get("plugins", "log"))) == False: if bool(int(self.app.config_handler.get("plugins", "log"))) == False:
self.display = "none" self.display = "none"
self.find_plugins() self.find_plugins()
def compose(self): def compose(self):
yield RichLog(markup=True, id="plugins-log", wrap=True) yield RichLog(markup=True, id="plugins-log", wrap=True)

View File

@@ -0,0 +1,7 @@
local plugin = {}
function plugin.run()
berry.config.defineSetting("my plugin", "test setting", "description", "boolean", "1")
end
return plugin

View File

@@ -0,0 +1,6 @@
{
"name": "my plugin",
"author": "SpookyDervish",
"version": "1.0.0",
"dependencies": []
}