Lift functions out of class

Neither of these used self.
This commit is contained in:
Sebastian Wiesner
2025-12-29 22:25:33 +01:00
parent f1bed9dd6a
commit 09d6a1ee2e

View File

@@ -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: