From 664d94b0a4210faab7d304d0e5c67651f3f513cd Mon Sep 17 00:00:00 2001 From: lvxnull2 <184518908+lvxnull2@users.noreply.github.com> Date: Fri, 11 Oct 2024 23:13:43 +0200 Subject: [PATCH] Implement admin:// support Currently requires sudo --- .../nautilus_open_any_terminal.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/nautilus_open_any_terminal/nautilus_open_any_terminal.py b/nautilus_open_any_terminal/nautilus_open_any_terminal.py index 71b8dee..a80a1b0 100644 --- a/nautilus_open_any_terminal/nautilus_open_any_terminal.py +++ b/nautilus_open_any_terminal/nautilus_open_any_terminal.py @@ -11,7 +11,7 @@ from os.path import expanduser from subprocess import Popen from typing import Optional -from urllib.parse import quote, unquote, urlparse +from urllib.parse import unquote, urlparse from gi import get_required_version, require_version @@ -72,7 +72,7 @@ class Terminal: "foot": Terminal("Foot"), "footclient": Terminal("FootClient"), "ghostty": Terminal("Ghostty"), - "gnome-terminal": Terminal("Terminal", new_tab_arguments=["--tab"]), + "gnome-terminal": Terminal("Terminal", new_tab_arguments=["--tab"], command_arguments=["--"]), "guake": Terminal("Guake", workdir_arguments=["--show", "--new-tab"]), "kermit": Terminal("Kermit"), "kgx": Terminal("Console", new_tab_arguments=["--tab"]), @@ -187,15 +187,18 @@ def parse_custom_command(command: str, data: str | list[str]) -> list[str]: return shlex.split(command.replace("%s", shlex.join(data))) -def run_command_in_terminal(command: list[str]): +def run_command_in_terminal(command: list[str], *, cwd: str | None = None): if terminal == "custom": cmd = parse_custom_command(custom_remote_command, command) else: cmd = terminal_cmd.copy() + if cwd and terminal_data.workdir_arguments: + cmd.extend(terminal_data.workdir_arguments) + cmd.append(cwd) cmd.extend(terminal_data.command_arguments) cmd.extend(command) - Popen(cmd) # pylint: disable=consider-using-with + Popen(cmd, cwd=cwd) # pylint: disable=consider-using-with def ssh_command_from_uri(uri: str, *, is_directory: bool): @@ -228,15 +231,18 @@ def open_remote_terminal_in_uri(uri: str): def open_local_terminal_in_uri(uri: str): """open the new terminal with correct path""" result = urlparse(uri) - cmd = terminal_cmd.copy() + filename = unquote(result.path) + if result.scheme == "admin": + run_command_in_terminal(["sudo", "-s"], cwd=filename) + return + if terminal == "warp": Popen( # pylint: disable=consider-using-with ["xdg-open", f'warp://action/new_{"tab" if new_tab else "window"}?path={result.path}'] ) return - filename = unquote(result.path) - + cmd = terminal_cmd.copy() if terminal == "custom": cmd = parse_custom_command(custom_local_command, filename) elif filename and terminal_data.workdir_arguments: @@ -458,19 +464,21 @@ def _menu_dir_activate_cb(self, menu, file_, remote: bool): if remote: open_remote_terminal_in_uri(file_.get_uri()) else: - open_local_terminal_in_uri("file://" + quote(file_.get_location().get_path())) + open_local_terminal_in_uri(file_.get_uri()) def _menu_exe_activate_cb(self, menu, file_, remote: bool): if remote: cmd = ssh_command_from_uri(file_.get_uri(), is_directory=False) else: - file = file_.get_location().get_path() + result = urlparse(file_.get_uri()) + file = unquote(result.path) - # Passing a single string to xterm -e will execute it in a shell - if terminal in ["xterm", "uxterm"]: - file = f"exec {shlex.quote(file)}" - - cmd = [file] + if result.scheme == "admin": + cmd = ["sudo", file] + elif terminal in ["xterm", "uxterm"]: + cmd = [f"exec {shlex.quote(file)}"] + else: + cmd = [file] run_command_in_terminal(cmd) def get_file_items(self, *args):