1
1
from __future__ import division
2
2
3
+ import json
3
4
import base64
4
5
import errno
5
6
import os
@@ -450,13 +451,11 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
450
451
log .debug ("Launching a new terminal: %r" % argv )
451
452
452
453
stdin = stdout = stderr = open (os .devnull , 'r+b' )
453
- if terminal == 'tmux' or terminal == 'kitty' :
454
+ if terminal == 'tmux' or terminal in ( 'kitty' , 'kitten' ) :
454
455
stdout = subprocess .PIPE
455
456
456
457
p = subprocess .Popen (argv , stdin = stdin , stdout = stdout , stderr = stderr , preexec_fn = preexec_fn )
457
458
458
- kittyid = None
459
-
460
459
if terminal == 'tmux' :
461
460
out , _ = p .communicate ()
462
461
try :
@@ -469,16 +468,24 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
469
468
with subprocess .Popen ((qdbus , konsole_dbus_service , '/Sessions/{}' .format (last_konsole_session ),
470
469
'org.kde.konsole.Session.processId' ), stdout = subprocess .PIPE ) as proc :
471
470
pid = int (proc .communicate ()[0 ].decode ())
472
- elif terminal == 'kitty' :
473
- pid = p .pid
474
-
471
+ elif terminal in ('kitty' , 'kitten' ):
472
+ pid = None
475
473
out , _ = p .communicate ()
476
474
try :
477
475
kittyid = int (out )
478
476
except ValueError :
479
477
kittyid = None
480
478
if kittyid is None :
481
479
log .error ("Could not parse kitty window ID from output (%r)" , out )
480
+ else :
481
+ lsout , _ = subprocess .Popen (["kitten" , "@" , "ls" , "--match" , "id:%d" % kittyid ], stdin = stdin , stdout = stdout , stderr = stderr ).communicate ()
482
+ try :
483
+ lsj = json .loads (lsout )
484
+ pid = int (lsj [0 ]["tabs" ][0 ]["windows" ][0 ]["pid" ])
485
+ except json .JSONDecodeError as e :
486
+ pid = None
487
+ log .error ("Json decode failed while parsing 'kitten @ ls' output (%r) (error: %r)" , lsout , e )
488
+
482
489
elif terminal == 'cmd.exe' :
483
490
# p.pid is cmd.exe's pid instead of the WSL process we want to start eventually.
484
491
# I don't know how to trace the execution through Windows and back into the WSL2 VM.
@@ -503,8 +510,6 @@ def kill():
503
510
try :
504
511
if terminal == 'qdbus' :
505
512
os .kill (pid , signal .SIGHUP )
506
- elif terminal == 'kitty' :
507
- subprocess .Popen (["kitten" , "@" , "close-window" , "--match" , "id:{}" .format (kittyid )], stderr = stderr )
508
513
else :
509
514
os .kill (pid , signal .SIGTERM )
510
515
except OSError :
0 commit comments