diff --git a/dist/linux/ghostty_nautilus.py b/dist/linux/ghostty_nautilus.py index 7e6c6e302..01202031c 100644 --- a/dist/linux/ghostty_nautilus.py +++ b/dist/linux/ghostty_nautilus.py @@ -20,50 +20,42 @@ from gi.repository import Nautilus, GObject, Gio -class OpenInGhosttyAction(GObject.GObject, Nautilus.MenuProvider): - def _open_terminal(self, path): +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 _menu_item_activated(self, _menu, paths): - for path in paths: - self._open_terminal(path) - def _make_item(self, name, paths): +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 + + +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', self._menu_item_activated, paths) - return item + item.connect('activate', open_in_ghostty_activated, paths) + return [item] + else: + return [] - 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) - if paths: - return [self._make_item(name='GhosttyNautilus::open_in_ghostty', paths=paths)] - else: - return [] +class GhosttyMenuProvider(GObject.GObject, Nautilus.MenuProvider): + 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] - paths = self._paths_to_open([file]) - if paths: - return [self._make_item(name='GhosttyNautilus::open_folder_in_ghostty', paths=paths)] - else: - return [] + def get_background_items(self, file): + return get_items_for_files('GhosttyNautilus::open_folder_in_ghostty', [file])