Skip to content

Commit

Permalink
launch: When using --cwd=current for a remote system support running …
Browse files Browse the repository at this point in the history
…non shell commands as well
  • Loading branch information
kovidgoyal committed Feb 8, 2023
1 parent 2445073 commit 7c8c7fe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Detailed list of changes

- Fix a regression in 0.27.0 that broke kitty @ set-font-size 0 (:iss:`5992`)

- launch: When using ``--cwd=current`` for a remote system support running non shell commands as well (:disc:`5987`)


0.27.1 [2023-02-07]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 9 additions & 1 deletion kittens/ssh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ def is_extra_arg(arg: str, extra_args: Tuple[str, ...]) -> str:
passthrough_args = {f'-{x}' for x in 'NnfGT'}


def set_server_args_in_cmdline(server_args: List[str], argv: List[str], extra_args: Tuple[str, ...] = ('--kitten',)) -> None:
def set_server_args_in_cmdline(
server_args: List[str], argv: List[str],
extra_args: Tuple[str, ...] = ('--kitten',),
allocate_tty: bool = False
) -> None:
boolean_ssh_args, other_ssh_args = get_ssh_cli()
ssh_args = []
expecting_option_val = False
Expand All @@ -136,6 +140,8 @@ def set_server_args_in_cmdline(server_args: List[str], argv: List[str], extra_ar
if argument.startswith('-') and not expecting_option_val:
if argument == '--':
del ans[i+2:]
if allocate_tty and ans[i-1] != '-t':
ans.insert(i, '-t')
break
if extra_args:
matching_ex = is_extra_arg(argument, extra_args)
Expand Down Expand Up @@ -173,5 +179,7 @@ def set_server_args_in_cmdline(server_args: List[str], argv: List[str], extra_ar
expecting_option_val = False
continue
del ans[i+1:]
if allocate_tty and ans[i] != '-t':
ans.insert(i, '-t')
break
argv[:] = ans + server_args
10 changes: 7 additions & 3 deletions kitty/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ def launch(
force_target_tab: bool = False,
active: Optional[Window] = None,
is_clone_launch: str = '',
rc_from_window: Optional[Window] = None,
) -> Optional[Window]:
active = active or boss.active_window_for_cwd
if active:
Expand Down Expand Up @@ -486,6 +487,8 @@ def launch(
kw['cwd_from'] = CwdRequest(active, CwdRequestType.root)
else:
kw['cwd'] = opts.cwd
if kw['cwd_from'] is not None and rc_from_window is not None:
kw['cwd_from'].rc_from_window_id = rc_from_window.id
if opts.location != 'default':
kw['location'] = opts.location
if opts.copy_colors and active:
Expand Down Expand Up @@ -538,9 +541,10 @@ def launch(
elif x == '@last-line-on-screen':
x = str(screen.visual_line(screen.lines - 1) or '')
final_cmd.append(x)
exe = which(final_cmd[0])
if exe:
final_cmd[0] = exe
if rc_from_window is None and final_cmd:
exe = which(final_cmd[0])
if exe:
final_cmd[0] = exe
kw['cmd'] = final_cmd
if force_window_launch and opts.type not in non_window_launch_types:
opts.type = 'window'
Expand Down
2 changes: 1 addition & 1 deletion kitty/rc/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get:
target_tab = tabs[0]
elif payload_get('type') not in ('background', 'os-window', 'tab', 'window'):
return None
w = do_launch(boss, opts, payload_get('args') or [], target_tab=target_tab)
w = do_launch(boss, opts, payload_get('args') or [], target_tab=target_tab, rc_from_window=window)
return None if payload_get('no_response') else str(getattr(w, 'id', 0))


Expand Down
18 changes: 10 additions & 8 deletions kitty/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class CwdRequest:
def __init__(self, window: Optional['Window'] = None, request_type: CwdRequestType = CwdRequestType.current) -> None:
self.window_id = -1 if window is None else window.id
self.request_type = request_type
self.rc_from_window_id = 0

def __bool__(self) -> bool:
return self.window_id > -1
Expand All @@ -170,14 +171,15 @@ def modify_argv_for_launch_with_cwd(self, argv: List[str]) -> str:
return ''
reported_cwd = path_from_osc7_url(window.screen.last_reported_cwd) if window.screen.last_reported_cwd else ''
if reported_cwd and (self.request_type is not CwdRequestType.root or window.root_in_foreground_processes):
# First check if we are running ssh kitten, and trying to open the configured login shell
if argv[0] == resolved_shell(get_options())[0]:
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
if ssh_kitten_cmdline:
from kittens.ssh.utils import set_cwd_in_cmdline
argv[:] = ssh_kitten_cmdline
set_cwd_in_cmdline(reported_cwd, argv)
return ''
ssh_kitten_cmdline = window.ssh_kitten_cmdline()
if ssh_kitten_cmdline:
run_shell = argv[0] == resolved_shell(get_options())[0]
server_args = [] if run_shell else list(argv)
from kittens.ssh.utils import set_cwd_in_cmdline, set_server_args_in_cmdline
argv[:] = ssh_kitten_cmdline
set_cwd_in_cmdline(reported_cwd, argv)
set_server_args_in_cmdline(server_args, argv, allocate_tty=not run_shell)
return ''
if not window.child_is_remote and (self.request_type is CwdRequestType.last_reported or window.at_prompt):
return reported_cwd
return window.get_cwd_of_child(oldest=self.request_type is CwdRequestType.oldest) or ''
Expand Down

0 comments on commit 7c8c7fe

Please sign in to comment.