fork/wezterm
git clone https://git.julia.blue/fork/wezterm
4634946d2
termwiz-0.23.3
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 ): 24def __init__ (self ): 25super ().__init__ () 26session = Gio .bus_get_sync (Gio .BusType .SESSION ,None ) 27self ._systemd = None 28# Check if the this system runs under systemd, per sd_booted(3) 29if os .path .isdir ('/run/systemd/system/' ): 30self ._systemd = Gio .DBusProxy .new_sync (session , 31Gio .DBusProxyFlags .NONE , 32None , 33"org.freedesktop.systemd1" , 34"/org/freedesktop/systemd1" , 35"org.freedesktop.systemd1.Manager" ,None ) 36 37def _open_terminal (self ,path ): 38cmd = ['wezterm' ,'start' ,'--cwd' ,path ] 39child = Gio .Subprocess .new (cmd ,Gio .SubprocessFlags .NONE ) 40if 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. 46pid = int (child .get_identifier ()) 47props = [("PIDs" ,GLib .Variant ('au' , [pid ])), 48 ('CollectMode' ,GLib .Variant ('s' ,'inactive-or-failed' ))] 49name = 'app-nautilus-org.wezfurlong.wezterm-{}.scope' .format (pid ) 50args = GLib .Variant ('(ssa(sv)a(sa(sv)))' , (name ,'fail' ,props , [])) 51self ._systemd .call_sync ('StartTransientUnit' ,args , 52Gio .DBusCallFlags .NO_AUTO_START ,500 ,None ) 53 54def _menu_item_activated (self ,_menu ,paths ): 55for path in paths : 56self ._open_terminal (path ) 57 58def _make_item (self ,name ,paths ): 59item = Nautilus .MenuItem (name = name ,label = 'Open in WezTerm' , 60icon = 'org.wezfurlong.wezterm' ) 61item .connect ('activate' ,self ._menu_item_activated ,paths ) 62return item 63 64def _paths_to_open (self ,files ): 65paths = [] 66for file in files : 67location = file .get_location ()if file .is_directory ()else file .get_parent_location () 68path = location .get_path () 69if path and path not in paths : 70paths .append (path ) 71if 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 :) 75return [] 76else : 77return paths 78 79def get_file_items (self ,* args ): 80# Nautilus 3.0 API passes args (window, files), 4.0 API just passes files 81files = args [0 ]if len (args )== 1 else args [1 ] 82paths = self ._paths_to_open (files ) 83if paths : 84return [self ._make_item (name = 'WezTermNautilus::open_in_wezterm' ,paths = paths )] 85else : 86return [] 87 88def get_background_items (self ,* args ): 89# Nautilus 3.0 API passes args (window, file), 4.0 API just passes file 90file = args [0 ]if len (args )== 1 else args [1 ] 91paths = self ._paths_to_open ([file ]) 92if paths : 93return [self ._make_item (name = 'WezTermNautilus::open_folder_in_wezterm' ,paths = paths )] 94else : 95return []