Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom terminal support #162

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ is an extension for nautilus, which adds an context-entry for opening other term

## Supported Terminal Emulators

Right now the plugin is limited to these terminal emulators. If one is missing please open an issue.
The following terminal emulators are fully supported. PRs for other terminals
are welcome!

- `alacritty`
- `blackbox`
Expand Down Expand Up @@ -48,6 +49,9 @@ Right now the plugin is limited to these terminal emulators. If one is missing p
- `xfce4-terminal`
- `xterm`/`uxterm`

Additionally, the terminal can be set to `custom`, which allows you to set
custom commands for opening a local or remote terminal via dconf.

## Installing

### From the AUR (Arch Linux) [![AUR package](https://repology.org/badge/version-for-repo/aur/nautilus-open-any-terminal.svg)](https://repology.org/project/nautilus-open-any-terminal/versions)
Expand Down
61 changes: 43 additions & 18 deletions nautilus_open_any_terminal/nautilus_open_any_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class Terminal:
flatpak_package: Optional[str] = None


_ = gettext
for localedir in [expanduser("~/.local/share/locale"), "/usr/share/locale"]:
try:
trans = translation("nautilus-open-any-terminal", localedir)
trans.install()
_ = trans.gettext
break
except FileNotFoundError:
continue

TERMINALS = {
"alacritty": Terminal("Alacritty"),
"blackbox": Terminal(
Expand All @@ -58,6 +68,7 @@ class Terminal:
flatpak_package="com.raggesilver.BlackBox",
),
"cool-retro-term": Terminal("cool-retro-term", workdir_arguments=["--workdir"]),
"custom": Terminal(_("Terminal"), command_arguments=[]),
"contour": Terminal(
"Contour",
workdir_arguments=["--working-directory"],
Expand Down Expand Up @@ -122,6 +133,8 @@ class Terminal:
terminal_data: Terminal = TERMINALS["gnome-terminal"]
new_tab = False
flatpak = FLATPAK_PARMS[0]
custom_local_command: str
custom_remote_command: str

GSETTINGS_PATH = "com.github.stunkymonkey.nautilus-open-any-terminal"
GSETTINGS_KEYBINDINGS = "keybindings"
Expand All @@ -130,18 +143,10 @@ class Terminal:
GSETTINGS_NEW_TAB = "new-tab"
GSETTINGS_FLATPAK = "flatpak"
GSETTINGS_USE_GENERIC_TERMINAL_NAME = "use-generic-terminal-name"
GSETTINGS_CUSTOM_LOCAL_COMMAND = "custom-local-command"
GSETTINGS_CUSTOM_REMOTE_COMMAND = "custom-remote-command"
REMOTE_URI_SCHEME = ["ftp", "sftp"]

_ = gettext
for localedir in [expanduser("~/.local/share/locale"), "/usr/share/locale"]:
try:
trans = translation("nautilus-open-any-terminal", localedir)
trans.install()
_ = trans.gettext
break
except FileNotFoundError:
continue


# Adapted from https://www.freedesktop.org/software/systemd/man/latest/os-release.html
def read_os_release():
Expand Down Expand Up @@ -179,6 +184,14 @@ def distro_id() -> set[str]:
return set(ids)


def parse_custom_command(command: str, data: str | list[str]) -> list[str]:
"""Substitute every '%s' in the command with data and split it into arguments"""
if isinstance(data, str):
data = [data]

return shlex.split(command.replace("%s", shlex.join(data)))


def open_remote_terminal_in_uri(uri: str):
"""Open a new remote terminal"""
result = urlparse(uri)
Expand All @@ -189,14 +202,17 @@ def open_remote_terminal_in_uri(uri: str):
if result.username:
cmd.append(f"{result.username}@{result.hostname}")
else:
cmd.append(result.hostname)
cmd.append(result.hostname) # type: ignore

if result.port:
cmd.append("-p")
cmd.append(str(result.port))

cmd.extend(["cd", shlex.quote(unquote(result.path)), ";", "exec", "$SHELL", "-l"])

if terminal == "custom":
cmd = parse_custom_command(custom_remote_command, cmd)

Popen(cmd) # pylint: disable=consider-using-with


Expand All @@ -211,14 +227,17 @@ def open_local_terminal_in_uri(uri: str):
return

filename = unquote(result.path)
if new_tab and terminal_data.new_tab_arguments:
cmd.extend(terminal_data.new_tab_arguments)
elif terminal_data.new_window_arguments:
cmd.extend(terminal_data.new_window_arguments)
if terminal == "custom":
cmd = parse_custom_command(custom_local_command, filename)
else:
if new_tab and terminal_data.new_tab_arguments:
cmd.extend(terminal_data.new_tab_arguments)
elif terminal_data.new_window_arguments:
cmd.extend(terminal_data.new_window_arguments)

if filename and terminal_data.workdir_arguments:
cmd.extend(terminal_data.workdir_arguments)
cmd.append(filename)
if filename and terminal_data.workdir_arguments:
cmd.extend(terminal_data.workdir_arguments)
cmd.append(filename)

Popen(cmd, cwd=filename) # pylint: disable=consider-using-with

Expand Down Expand Up @@ -285,6 +304,8 @@ def set_terminal_args(*_args):
global flatpak
global terminal_cmd
global terminal_data
global custom_local_command
global custom_remote_command
value = _gsettings.get_string(GSETTINGS_TERMINAL)
newer_tab = _gsettings.get_boolean(GSETTINGS_NEW_TAB)
flatpak = FLATPAK_PARMS[_gsettings.get_enum(GSETTINGS_FLATPAK)]
Expand All @@ -306,6 +327,10 @@ def set_terminal_args(*_args):
if flatpak != FLATPAK_PARMS[0] and terminal_data.flatpak_package is not None:
terminal_cmd = ["flatpak", "run", "--" + flatpak, terminal_data.flatpak_package]
flatpak_text = f"with flatpak as {flatpak}"
elif terminal == "custom":
terminal_cmd = []
custom_local_command = _gsettings.get_string(GSETTINGS_CUSTOM_LOCAL_COMMAND)
custom_remote_command = _gsettings.get_string(GSETTINGS_CUSTOM_REMOTE_COMMAND)
else:
terminal_cmd = [terminal]
if terminal == "blackbox" and "fedora" in distro_id():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
<summary>Terminal used in Nautilus for Open Here extension</summary>
<description></description>
</key>
<key name="custom-local-command" type="s">
<default>""</default>
<summary>Command that opens a local terminal if terminal is set to "custom"</summary>
<description>
Every occurence of "%s" in the command will be replaced with the path where the terminal should be opened.
Any number of "%s" in the command are valid, including 0.
</description>
</key>
<key name="custom-remote-command" type="s">
<default>""</default>
<summary>Command that opens a remote terminal if terminal is set to "custom"</summary>
<description>
Every occurence of "%s" in the command will be replaced with an ssh command that connects to the remote server.
There should be at least 1 "%s" in the command.
</description>
</key>
<key name="use-generic-terminal-name" type="b">
<default>false</default>
<summary>Whether to display specific or generic terminal name.</summary>
Expand Down