Skip to content

Commit fe0a0a6

Browse files
committed
Merge branch 'kitten_ssh_exit_shell' of https://github.com/barr-israel/kitty
2 parents 7fb0350 + ca2232e commit fe0a0a6

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

kitty/child.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def __init__(
221221
hold: bool = False,
222222
pass_fds: tuple[int, ...] = (),
223223
remote_control_fd: int = -1,
224+
hold_after_ssh: bool = False
224225
):
225226
self.is_clone_launch = is_clone_launch
226227
self.id = next(child_counter)
@@ -230,7 +231,7 @@ def __init__(
230231
self.remote_control_fd = remote_control_fd
231232
if cwd_from:
232233
try:
233-
cwd = cwd_from.modify_argv_for_launch_with_cwd(self.argv, env) or cwd
234+
cwd = cwd_from.modify_argv_for_launch_with_cwd(self.argv, env, hold_after_ssh=hold_after_ssh) or cwd
234235
except Exception as err:
235236
log_error(f'Failed to read cwd of {cwd_from} with error: {err}')
236237
else:

kitty/launch.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ def options_spec() -> str:
140140
oldest foreground process associated with the currently active window rather
141141
than the newest foreground process. Finally, the special value :code:`root`
142142
refers to the process that was originally started when the window was created.
143+
When running :code:`kitten ssh`, using :option:`--hold-after-ssh` will cause a shell
144+
in the working directory that started the :code:`kitten ssh` to be opened after disconnecting.
143145
144146
145147
--env
@@ -393,6 +395,11 @@ def options_spec() -> str:
393395
For example, to create a desktop panel at the bottom of the screen two lines high::
394396
395397
launch --type os-panel --os-panel lines=2 --os-panel edge=bottom sh -c "echo; echo; echo hello; sleep 5s"
398+
399+
400+
--hold-after-ssh
401+
type=bool-set
402+
When using :option:`--cwd=current` from a kitten ssh session, after disconnecting from the session on the new window, a shell will spawn.
396403
"""
397404

398405

@@ -536,6 +543,7 @@ class LaunchKwds(TypedDict):
536543
stdin: bytes | None
537544
hold: bool
538545
bias: float | None
546+
hold_after_ssh: bool
539547

540548

541549
def apply_colors(window: Window, spec: Sequence[str]) -> None:
@@ -636,6 +644,7 @@ def _launch(
636644
'stdin': None,
637645
'hold': False,
638646
'bias': None,
647+
'hold_after_ssh': False
639648
}
640649
spacing = {}
641650
if opts.spacing:
@@ -658,6 +667,11 @@ def _launch(
658667
kw['cwd_from'] = CwdRequest(source_window, CwdRequestType.root)
659668
else:
660669
kw['cwd'] = opts.cwd
670+
if opts.hold_after_ssh:
671+
if opts.cwd != 'current':
672+
raise ValueError("--hold_after_ssh can only be supplied if --cwd=current is also supplied")
673+
kw['hold_after_ssh'] = True
674+
661675
if opts.location != 'default':
662676
kw['location'] = opts.location
663677
if opts.copy_colors and source_window:
@@ -804,7 +818,7 @@ def clone_safe_opts() -> frozenset[str]:
804818
return frozenset((
805819
'window_title', 'tab_title', 'type', 'keep_focus', 'cwd', 'env', 'var', 'hold',
806820
'location', 'os_window_class', 'os_window_name', 'os_window_title', 'os_window_state',
807-
'logo', 'logo_position', 'logo_alpha', 'color', 'spacing', 'next_to',
821+
'logo', 'logo_position', 'logo_alpha', 'color', 'spacing', 'next_to', 'hold_after_ssh'
808822
))
809823

810824

kitty/rc/launch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Launch(RemoteCommand):
5757
watcher/list.str: list of paths to watcher files
5858
bias/float: The bias with which to create the new window in the current layout
5959
wait_for_child_to_exit/bool: Boolean indicating whether to wait and return child exit code
60+
hold_after_ssh/bool: Boolean indicating whether to spawn a shell after exiting a kitten ssh opened by this launch, requires --cwd=current to also be supplied
6061
'''
6162

6263
short_desc = 'Run an arbitrary process in a new window/tab'

kitty/tabs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ def launch_child(
480480
hold: bool = False,
481481
pass_fds: tuple[int, ...] = (),
482482
remote_control_fd: int = -1,
483+
hold_after_ssh: bool = False
483484
) -> Child:
484485
check_for_suitability = True
485486
if cmd is None:
@@ -529,7 +530,7 @@ def launch_child(
529530
fenv['WINDOWID'] = str(pwid)
530531
ans = Child(
531532
cmd, cwd or self.cwd, stdin, fenv, cwd_from, is_clone_launch=is_clone_launch,
532-
add_listen_on_env_var=add_listen_on_env_var, hold=hold, pass_fds=pass_fds, remote_control_fd=remote_control_fd)
533+
add_listen_on_env_var=add_listen_on_env_var, hold=hold, pass_fds=pass_fds, remote_control_fd=remote_control_fd, hold_after_ssh=hold_after_ssh)
533534
ans.fork()
534535
return ans
535536

@@ -568,11 +569,12 @@ def new_window(
568569
pass_fds: tuple[int, ...] = (),
569570
remote_control_fd: int = -1,
570571
next_to: Window | None = None,
572+
hold_after_ssh: bool = False
571573
) -> Window:
572574
child = self.launch_child(
573575
use_shell=use_shell, cmd=cmd, stdin=stdin, cwd_from=cwd_from, cwd=cwd, env=env,
574576
is_clone_launch=is_clone_launch, add_listen_on_env_var=False if allow_remote_control and remote_control_passwords else True,
575-
hold=hold, pass_fds=pass_fds, remote_control_fd=remote_control_fd,
577+
hold=hold, pass_fds=pass_fds, remote_control_fd=remote_control_fd, hold_after_ssh=hold_after_ssh
576578
)
577579
window = Window(
578580
self, child, self.args, override_title=override_title,

kitty/window.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def cwd_of_child(self) -> str:
155155
return window.get_cwd_of_root_child() or ''
156156
return window.get_cwd_of_child(oldest=self.request_type is CwdRequestType.oldest) or ''
157157

158-
def modify_argv_for_launch_with_cwd(self, argv: list[str], env: dict[str, str] | None=None) -> str:
158+
def modify_argv_for_launch_with_cwd(self, argv: list[str], env: dict[str, str] | None=None, hold_after_ssh: bool = False) -> str:
159159
window = self.window
160160
if not window:
161161
return ''
@@ -166,9 +166,13 @@ def modify_argv_for_launch_with_cwd(self, argv: list[str], env: dict[str, str] |
166166
run_shell = argv[0] == resolved_shell(get_options())[0]
167167
server_args = [] if run_shell else list(argv)
168168
from kittens.ssh.utils import set_cwd_in_cmdline, set_env_in_cmdline, set_server_args_in_cmdline
169-
argv[:] = ssh_kitten_cmdline
170-
if argv and argv[0] == 'kitten':
171-
argv[0] = kitten_exe()
169+
if ssh_kitten_cmdline and ssh_kitten_cmdline[0] == 'kitten':
170+
if hold_after_ssh:
171+
argv[:] = [kitten_exe(), "run-shell", kitten_exe()] +ssh_kitten_cmdline[1:]
172+
else:
173+
argv[:] = [kitten_exe()]+ssh_kitten_cmdline[1:]
174+
else:
175+
argv[:] = ssh_kitten_cmdline
172176
set_cwd_in_cmdline(reported_cwd, argv)
173177
set_server_args_in_cmdline(server_args, argv, allocate_tty=not run_shell)
174178
if env is not None:

0 commit comments

Comments
 (0)