When serializing foreground process allow serializing shell builtins as well

This commit is contained in:
Kovid Goyal
2025-08-19 16:19:26 +05:30
parent 03792a1717
commit e7b3fb0197
5 changed files with 32 additions and 23 deletions

View File

@@ -230,7 +230,7 @@ class Child:
pass_fds: tuple[int, ...] = (),
remote_control_fd: int = -1,
hold_after_ssh: bool = False,
startup_command_via_shell_integration: Sequence[str] = (),
startup_command_via_shell_integration: Sequence[str] | str = (),
):
self.is_clone_launch = is_clone_launch
self.id = next(child_counter)
@@ -298,12 +298,15 @@ class Child:
env.pop('KITTY_IS_CLONE_LAUNCH', None)
must_run_startup_command_via_kitten = False
if self.startup_command_via_shell_integration:
from .shell_integration import join
scmd = self.argv or resolved_shell(fast_data_types.get_options())
try:
env['KITTY_SI_RUN_COMMAND_AT_STARTUP'] = join(scmd[0], self.startup_command_via_shell_integration)
except Exception:
must_run_startup_command_via_kitten = True # unknown shell
if isinstance(self.startup_command_via_shell_integration, str):
env['KITTY_SI_RUN_COMMAND_AT_STARTUP'] = self.startup_command_via_shell_integration
else:
from .shell_integration import join
scmd = self.argv or resolved_shell(fast_data_types.get_options())
try:
env['KITTY_SI_RUN_COMMAND_AT_STARTUP'] = join(scmd[0], self.startup_command_via_shell_integration)
except Exception:
must_run_startup_command_via_kitten = True # unknown shell
return env, must_run_startup_command_via_kitten
def fork(self) -> int | None:

View File

@@ -621,7 +621,7 @@ def _launch(
rc_from_window: Window | None = None,
base_env: dict[str, str] | None = None,
child_death_callback: Callable[[int, Exception | None], None] | None = None,
startup_command_via_shell_integration: Sequence[str] = (),
startup_command_via_shell_integration: Sequence[str] | str = (),
) -> Window | None:
source_window = boss.active_window_for_cwd
if opts.source_window:
@@ -837,7 +837,7 @@ def launch(
rc_from_window: Window | None = None,
base_env: dict[str, str] | None = None,
child_death_callback: Callable[[int, Exception | None], None] | None = None,
startup_command_via_shell_integration: Sequence[str] = (),
startup_command_via_shell_integration: Sequence[str] | str = (),
) -> Window | None:
active = boss.active_window
if opts.keep_focus and active:

View File

@@ -41,7 +41,10 @@ ResizeSpec = tuple[str, int]
class WindowSpec:
def __init__(self, launch_spec: Union['LaunchSpec', 'SpecialWindowInstance'], serialized_id: int = 0, run_command_at_shell_startup: Sequence[str] = ()):
def __init__(
self, launch_spec: Union['LaunchSpec', 'SpecialWindowInstance'], serialized_id: int = 0,
run_command_at_shell_startup: Sequence[str] | str = ()
):
self.launch_spec = launch_spec
self.resize_spec: ResizeSpec | None = None
self.focus_matching_window_spec: str = ''

View File

@@ -537,7 +537,7 @@ class Tab: # {{{
pass_fds: tuple[int, ...] = (),
remote_control_fd: int = -1,
hold_after_ssh: bool = False,
startup_command_via_shell_integration: Sequence[str] = (),
startup_command_via_shell_integration: Sequence[str] | str = (),
) -> Child:
check_for_suitability = True
if cmd is None:
@@ -629,7 +629,7 @@ class Tab: # {{{
remote_control_fd: int = -1,
next_to: Window | None = None,
hold_after_ssh: bool = False,
startup_command_via_shell_integration: Sequence[str] = (),
startup_command_via_shell_integration: Sequence[str] | str = (),
) -> Window:
cs = WindowCreationSpec(
use_shell=use_shell, cmd=cmd, has_stdin=bool(stdin), override_title=override_title, cwd_from=cwd_from,

View File

@@ -2009,17 +2009,20 @@ class Window:
if self.creation_spec and self.creation_spec.cmd:
if self.creation_spec.cmd != resolved_shell(get_options()):
cmd = self.creation_spec.cmd
unserialize_data: dict[str, int | list[str]] = {'id': self.id}
if not cmd and ser_opts.use_foreground_process and self.child.pid != (pid := self.child.pid_for_cwd) and pid is not None:
# we have a shell running some command
with suppress(Exception):
fcmd = self.child.cmdline_of_pid(pid)
if fcmd:
unserialize_data['cmd_at_shell_startup'] = fcmd
if not os.path.isabs(fcmd[0]):
with suppress(Exception):
from .child import abspath_of_exe
fcmd[0] = abspath_of_exe(pid)
unserialize_data: dict[str, int | list[str] | str] = {'id': self.id}
if not cmd and ser_opts.use_foreground_process:
if not self.at_prompt and self.last_cmd_cmdline:
unserialize_data['cmd_at_shell_startup'] = self.last_cmd_cmdline
elif self.child.pid != (pid := self.child.pid_for_cwd) and pid is not None:
# we have a shell running some command
with suppress(Exception):
fcmd = self.child.cmdline_of_pid(pid)
if fcmd:
unserialize_data['cmd_at_shell_startup'] = fcmd
if not os.path.isabs(fcmd[0]):
with suppress(Exception):
from .child import abspath_of_exe
fcmd[0] = abspath_of_exe(pid)
ans.insert(1, unserialize_launch_flag + json.dumps(unserialize_data))
ans.extend(cmd)
return ans