From bf84f95a94e6a01891247b6a2b5fa152ff19fa76 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 7 Jan 2022 13:52:13 +0530 Subject: [PATCH] macOS: When launching a shell script use its shebang, if present --- kitty/boss.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 15661cdab..2a45d2a34 100755 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -2206,6 +2206,18 @@ class Boss: with suppress(OSError): is_dir = os.path.isdir(path) + def parse_macos_shebang(path: str) -> List[str]: + # The macos kernel splits the shebang line on spaces + try: + f = open(path) + except OSError: + return [] + with f: + if f.read(2) != '#!': + return [] + line = f.readline().strip() + return line.split(' ') + needs_new_os_window = self.cocoa_application_launched or not self.os_window_map or self.active_tab is None launch_cmd = [] if needs_new_os_window: @@ -2219,9 +2231,9 @@ class Boss: mt = guess_type(path) or '' ext = os.path.splitext(path)[1].lower() if ext in ('.sh', '.command', '.tool'): - launch_cmd += ['--hold', shell_path, path] + launch_cmd += ['--hold'] + (parse_macos_shebang(path) or [shell_path]) + [path] elif ext in ('.zsh', '.bash', '.fish'): - launch_cmd += ['--hold', ext[1:], path] + launch_cmd += ['--hold'] + (parse_macos_shebang(path) or [ext[1:]]) + [path] elif mt.startswith('text/'): launch_cmd += get_editor() + [path] elif mt.startswith('image/'):