From f1bed9dd6a4a44aedbf634a6e7627cdb6d1b2366 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 29 Dec 2025 22:24:37 +0100 Subject: [PATCH 1/5] Inline trivial method --- dist/linux/ghostty_nautilus.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index 7e6c6e302..965e8107e 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -21,13 +21,10 @@ from gi.repository import Nautilus, GObject, Gio class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): - def _open_terminal(self, path): - cmd = ['ghostty', f'--working-directory={path}', '--gtk-single-instance=false'] - Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE) - def _menu_item_activated(self, _menu, paths): for path in paths: - self._open_terminal(path) + cmd = ['ghostty', f'--working-directory={path}', '--gtk-single-instance=false'] + Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE) def _make_item(self, name, paths): item = Nautilus.MenuItem(name=name, label='Open in Ghostty', From 09d6a1ee2ee8c79f0690d271935d201cf4694ac9 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 29 Dec 2025 22:25:33 +0100 Subject: [PATCH 2/5] Lift functions out of class Neither of these used self. --- dist/linux/ghostty_nautilus.py | 48 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index 965e8107e..e993d0aaa 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -20,37 +20,39 @@ from gi.repository import Nautilus, GObject, Gio -class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): - def _menu_item_activated(self, _menu, paths): - for path in paths: - cmd = ['ghostty', f'--working-directory={path}', '--gtk-single-instance=false'] - Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE) +def open_in_ghostty_activated(_menu, paths): + for path in paths: + cmd = ['ghostty', f'--working-directory={path}', '--gtk-single-instance=false'] + Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE) + +def get_paths_to_open(files): + paths = [] + for file in files: + location = file.get_location() if file.is_directory() else file.get_parent_location() + path = location.get_path() + if path and path not in paths: + paths.append(path) + if 10 < len(paths): + # Let's not open anything if the user selected a lot of directories, + # to avoid accidentally spamming their desktop with dozends of + # new windows or tabs. Ten is a totally arbitrary limit :) + return [] + else: + return paths + + +class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): def _make_item(self, name, paths): item = Nautilus.MenuItem(name=name, label='Open in Ghostty', icon='com.mitchellh.ghostty') - item.connect('activate', self._menu_item_activated, paths) + item.connect('activate', open_in_ghostty_activated, paths) return item - def _paths_to_open(self, files): - paths = [] - for file in files: - location = file.get_location() if file.is_directory() else file.get_parent_location() - path = location.get_path() - if path and path not in paths: - paths.append(path) - if 10 < len(paths): - # Let's not open anything if the user selected a lot of directories, - # to avoid accidentally spamming their desktop with dozends of - # new windows or tabs. Ten is a totally arbitrary limit :) - return [] - else: - return paths - def get_file_items(self, *args): # Nautilus 3.0 API passes args (window, files), 4.0 API just passes files files = args[0] if len(args) == 1 else args[1] - paths = self._paths_to_open(files) + paths = get_paths_to_open(files) if paths: return [self._make_item(name='GhosttyNautilus::open_in_ghostty', paths=paths)] else: @@ -59,7 +61,7 @@ class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): def get_background_items(self, *args): # Nautilus 3.0 API passes args (window, file), 4.0 API just passes file file = args[0] if len(args) == 1 else args[1] - paths = self._paths_to_open([file]) + paths = get_paths_to_open([file]) if paths: return [self._make_item(name='GhosttyNautilus::open_folder_in_ghostty', paths=paths)] else: From dee093db579c1e28c4695351c0e16eda968291a2 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 29 Dec 2025 22:33:57 +0100 Subject: [PATCH 3/5] Extract duplicated code into single helper Extract duplicated code from the two different menu item hooks into a single helper function, and then inline _make_item, as it's only used once now. --- dist/linux/ghostty_nautilus.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index e993d0aaa..80ec84ac3 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -42,27 +42,24 @@ def get_paths_to_open(files): return paths -class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): - def _make_item(self, name, paths): +def get_items_for_files(name, files): + paths = get_paths_to_open(files) + if paths: item = Nautilus.MenuItem(name=name, label='Open in Ghostty', icon='com.mitchellh.ghostty') item.connect('activate', open_in_ghostty_activated, paths) - return item + return [item] + else: + return [] + +class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): def get_file_items(self, *args): # Nautilus 3.0 API passes args (window, files), 4.0 API just passes files files = args[0] if len(args) == 1 else args[1] - paths = get_paths_to_open(files) - if paths: - return [self._make_item(name='GhosttyNautilus::open_in_ghostty', paths=paths)] - else: - return [] + return get_items_for_files('GhosttyNautilus::open_in_ghostty', files) def get_background_items(self, *args): # Nautilus 3.0 API passes args (window, file), 4.0 API just passes file file = args[0] if len(args) == 1 else args[1] - paths = get_paths_to_open([file]) - if paths: - return [self._make_item(name='GhosttyNautilus::open_folder_in_ghostty', paths=paths)] - else: - return [] + return get_items_for_files('GhosttyNautilus::open_folder_in_ghostty', [file]) From 5c35a4710d91ec5d1eadb84c13f395dbd2ed9387 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 29 Dec 2025 22:51:07 +0100 Subject: [PATCH 4/5] Rename class It's a menu provider not a single action. --- dist/linux/ghostty_nautilus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index 80ec84ac3..cd5011b9f 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -53,7 +53,7 @@ def get_items_for_files(name, files): return [] -class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): +class GhosttyMenuProvider(GObject.GObject, Nautilus.MenuProvider): def get_file_items(self, *args): # Nautilus 3.0 API passes args (window, files), 4.0 API just passes files files = args[0] if len(args) == 1 else args[1] From ab4b54e2a45e949a857468a2d218f6ec10194d4a Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Mon, 29 Dec 2025 23:07:01 +0100 Subject: [PATCH 5/5] Drop GNOME 42 compatibility GNOME 43 is from 2022-09, i.e. almost as old as Ghostty, so not longer worth supporting here. --- dist/linux/ghostty_nautilus.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index cd5011b9f..01202031c 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -54,12 +54,8 @@ def get_items_for_files(name, files): class GhosttyMenuProvider(GObject.GObject, Nautilus.MenuProvider): - def get_file_items(self, *args): - # Nautilus 3.0 API passes args (window, files), 4.0 API just passes files - files = args[0] if len(args) == 1 else args[1] + def get_file_items(self, files): return get_items_for_files('GhosttyNautilus::open_in_ghostty', files) - def get_background_items(self, *args): - # Nautilus 3.0 API passes args (window, file), 4.0 API just passes file - file = args[0] if len(args) == 1 else args[1] + def get_background_items(self, file): return get_items_for_files('GhosttyNautilus::open_folder_in_ghostty', [file])