From 02a7316342142e990f00f3169f6e710f2fa3d4b5 Mon Sep 17 00:00:00 2001 From: pagedown Date: Thu, 9 Dec 2021 11:32:30 +0800 Subject: [PATCH 1/2] Use the unified PATH search function --- kittens/tui/images.py | 3 +-- kitty/boss.py | 15 ++++----------- kitty/tabs.py | 4 ++-- kitty/utils.py | 2 +- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/kittens/tui/images.py b/kittens/tui/images.py index 6e9b2779c..3c6fccca3 100644 --- a/kittens/tui/images.py +++ b/kittens/tui/images.py @@ -294,10 +294,9 @@ def render_as_single_image( def can_display_images() -> bool: - import shutil ans: Optional[bool] = getattr(can_display_images, 'ans', None) if ans is None: - ans = shutil.which('convert') is not None + ans = which('convert') is not None setattr(can_display_images, 'ans', ans) return ans diff --git a/kitty/boss.py b/kitty/boss.py index 1f6df2b2d..bb1807666 100755 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -58,9 +58,8 @@ from .typing import PopenType, TypedDict from .utils import ( func_name, get_editor, get_new_os_window_size, get_primary_selection, is_path_in_temp_dir, log_error, open_url, parse_address_spec, - parse_uri_list, platform_window_id, read_shell_environment, - remove_socket_file, safe_print, set_primary_selection, single_instance, - startup_notification_handler + parse_uri_list, platform_window_id, remove_socket_file, safe_print, + set_primary_selection, single_instance, startup_notification_handler ) from .window import CommandOutput, MatchPatternType, Window @@ -1282,14 +1281,8 @@ class Boss: cmd = list(map(prepare_arg, get_options().scrollback_pager)) if not os.path.isabs(cmd[0]): - import shutil - exe = shutil.which(cmd[0]) - if not exe: - env = read_shell_environment(get_options()) - if env and 'PATH' in env: - exe = shutil.which(cmd[0], path=env['PATH']) - if exe: - cmd[0] = exe + from .utils import which + cmd[0] = which(cmd[0]) or cmd[0] if os.path.basename(cmd[0]) == 'less': cmd.append('-+F') # reset --quit-if-one-screen diff --git a/kitty/tabs.py b/kitty/tabs.py index b1cf19960..0ff9ac09e 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -364,8 +364,8 @@ class Tab: # {{{ if check_for_suitability: old_exe = cmd[0] if not os.path.isabs(old_exe): - import shutil - actual_exe = shutil.which(old_exe) + from .utils import which + actual_exe = which(old_exe) old_exe = actual_exe if actual_exe else os.path.abspath(old_exe) try: is_executable = os.access(old_exe, os.X_OK) diff --git a/kitty/utils.py b/kitty/utils.py index c0e2a44d1..625310c0a 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -543,7 +543,7 @@ def get_editor_from_env_vars(opts: Optional[Options] = None) -> List[str]: editor = get_editor_from_env(shell_env) for ans in (editor, 'vim', 'nvim', 'vi', 'emacs', 'kak', 'micro', 'nano', 'vis'): - if ans and shutil.which(shlex.split(ans)[0]): + if ans and which(shlex.split(ans)[0], only_system=True): break else: ans = 'vim' From 5d1f58427ea8f8aef78979fc3b03986d443d2568 Mon Sep 17 00:00:00 2001 From: pagedown Date: Thu, 9 Dec 2021 11:57:17 +0800 Subject: [PATCH 2/2] ... --- kitty/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kitty/utils.py b/kitty/utils.py index 625310c0a..e4e0f4a67 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -535,7 +535,6 @@ def get_editor_from_env(env: Mapping[str, str]) -> Optional[str]: def get_editor_from_env_vars(opts: Optional[Options] = None) -> List[str]: import shlex - import shutil editor = get_editor_from_env(os.environ) if not editor: