#!/usr/bin/env python # License: GPLv3 Copyright: 2022, Kovid Goyal import os import shutil import tempfile import unittest from contextlib import contextmanager from kitty.constants import kitty_base_dir, terminfo_dir from kitty.fast_data_types import CURSOR_BEAM from kitty.shell_integration import rc_inset, setup_zsh_env from . import BaseTest def safe_env_for_running_shell(home_dir, rc='', shell='zsh'): ans = { 'PATH': os.environ['PATH'], 'HOME': home_dir, 'TERM': 'xterm-kitty', 'TERMINFO': terminfo_dir, 'KITTY_SHELL_INTEGRATION': 'enabled', 'KITTY_INSTALLATION_DIR': kitty_base_dir, } for x in ('USER', 'LANG'): if os.environ.get(x): ans[x] = os.environ[x] if shell == 'zsh': ans['ZLE_RPROMPT_INDENT'] = '0' with open(os.path.join(home_dir, '.zshenv'), 'w') as f: print('unset GLOBAL_RCS', file=f) with open(os.path.join(home_dir, '.zshrc'), 'w') as f: print(rc + '\n', file=f) setup_zsh_env(ans) elif shell == 'bash': with open(os.path.join(home_dir, '.bash_profile'), 'w') as f: print('if [ -f ~/.bashrc ]; then . ~/.bashrc; fi', file=f) with open(os.path.join(home_dir, '.bashrc'), 'w') as f: # there is no way to prevent bash from reading the system # /etc/profile and some distros set a PROMPT_COMMAND in it. print('unset PROMPT_COMMAND', file=f) # ensure LINES and COLUMNS are kept up to date print('shopt -s checkwinsize', file=f) # Not sure why bash turns off echo in this scenario print('stty echo', file=f) if rc: print(rc, file=f) print(rc_inset('bash'), file=f) return ans class ShellIntegration(BaseTest): @contextmanager def run_shell(self, shell='zsh', rc='', cmd='{shell} -il'): home_dir = os.path.realpath(tempfile.mkdtemp()) try: pty = self.create_pty(cmd.format(**locals()), cwd=home_dir, env=safe_env_for_running_shell(home_dir, rc=rc, shell=shell)) i = 10 while i > 0 and not pty.screen_contents().strip(): pty.process_input_from_child() i -= 1 yield pty finally: if os.path.exists(home_dir): shutil.rmtree(home_dir) @unittest.skipUnless(shutil.which('zsh'), 'zsh not installed') def test_zsh_integration(self): ps1, rps1 = 'left>', '