fork/wezterm

git clone https://git.julia.blue/fork/wezterm

Gabriele Muscofeat: support Natilus 4.0 API4634946d2

termwiz-0.23.0
4.1 KiB95 linesraw
1# Copyright (C) 2022 Sebastian Wiesner <sebastian@swsnr.de>
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17import os.path
18
19from gi import require_version
20from gi.repository import Nautilus, GObject, Gio, GLib
21
22
23class OpenInWezTermAction(GObject.GObject, Nautilus.MenuProvider):
24    def __init__(self):
25        super().__init__()
26        session = Gio.bus_get_sync(Gio.BusType.SESSION, None)
27        self._systemd = None
28        # Check if the this system runs under systemd, per sd_booted(3)
29        if os.path.isdir('/run/systemd/system/'):
30            self._systemd = Gio.DBusProxy.new_sync(session,
31                    Gio.DBusProxyFlags.NONE,
32                    None,
33                    "org.freedesktop.systemd1",
34                    "/org/freedesktop/systemd1",
35                    "org.freedesktop.systemd1.Manager", None)
36
37    def _open_terminal(self, path):
38        cmd = ['wezterm', 'start', '--cwd', path]
39        child = Gio.Subprocess.new(cmd, Gio.SubprocessFlags.NONE)
40        if self._systemd:
41            # Move new terminal into a dedicated systemd scope to make systemd
42            # track the terminal separately; in particular this makes systemd
43            # keep a separate CPU and memory account for Wezterm which in turn
44            # ensures that oomd doesn't take nautilus down if a process in
45            # wezterm consumes a lot of memory.
46            pid = int(child.get_identifier())
47            props = [("PIDs", GLib.Variant('au', [pid])),
48                ('CollectMode', GLib.Variant('s', 'inactive-or-failed'))]
49            name = 'app-nautilus-org.wezfurlong.wezterm-{}.scope'.format(pid)
50            args = GLib.Variant('(ssa(sv)a(sa(sv)))', (name, 'fail', props, []))
51            self._systemd.call_sync('StartTransientUnit', args,
52                    Gio.DBusCallFlags.NO_AUTO_START, 500, None)
53
54    def _menu_item_activated(self, _menu, paths):
55        for path in paths:
56            self._open_terminal(path)
57
58    def _make_item(self, name, paths):
59        item = Nautilus.MenuItem(name=name, label='Open in WezTerm',
60            icon='org.wezfurlong.wezterm')
61        item.connect('activate', self._menu_item_activated, paths)
62        return item
63
64    def _paths_to_open(self, files):
65        paths = []
66        for file in files:
67            location = file.get_location() if file.is_directory() else file.get_parent_location()
68            path = location.get_path()
69            if path and path not in paths:
70                paths.append(path)
71        if 10 < len(paths):
72            # Let's not open anything if the user selected a lot of directories,
73            # to avoid accidentally spamming their desktop with dozends of
74            # new windows or tabs.  Ten is a totally arbitrary limit :)
75            return []
76        else:
77            return paths
78
79    def get_file_items(self, *args):
80        # Nautilus 3.0 API passes args (window, files), 4.0 API just passes files
81        files = args[0] if len(args) == 1 else args[1]
82        paths = self._paths_to_open(files)
83        if paths:
84            return [self._make_item(name='WezTermNautilus::open_in_wezterm', paths=paths)]
85        else:
86            return []
87
88    def get_background_items(self, *args):
89        # Nautilus 3.0 API passes args (window, file), 4.0 API just passes file
90        file = args[0] if len(args) == 1 else args[1]
91        paths = self._paths_to_open([file])
92        if paths:
93            return [self._make_item(name='WezTermNautilus::open_folder_in_wezterm', paths=paths)]
94        else:
95            return []