From 1e84cbe2ab6494f0a14f76764d5f6aa20e30bd01 Mon Sep 17 00:00:00 2001 From: pagedown Date: Thu, 17 Mar 2022 22:23:37 +0800 Subject: [PATCH] Check macOS less version --- kitty/boss.py | 23 ++++++++++++++--------- kitty/utils.py | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 4bd92a05d..2c6b78064 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -22,8 +22,8 @@ from .cli_stub import CLIOptions from .conf.utils import BadLine, KeyAction, to_cmdline from .config import common_opts_as_dict, prepare_config_file_for_editing from .constants import ( - appname, config_dir, is_macos, is_wayland, kitty_exe, logo_png_file, - supports_primary_selection, website_url + appname, cache_dir, config_dir, is_macos, is_wayland, kitty_exe, + logo_png_file, supports_primary_selection, website_url ) from .fast_data_types import ( CLOSE_BEING_CONFIRMED, GLFW_MOD_ALT, GLFW_MOD_CONTROL, GLFW_MOD_SHIFT, @@ -57,10 +57,10 @@ from .types import _T, AsyncResponse, SingleKey, WindowSystemMouseEvent, ac from .typing import PopenType, TypedDict from .utils import ( cleanup_ssh_control_masters, 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, remove_socket_file, - safe_print, set_primary_selection, single_instance, - startup_notification_handler, which + get_primary_selection, is_path_in_temp_dir, less_version, log_error, + macos_version, open_url, parse_address_spec, parse_uri_list, + platform_window_id, remove_socket_file, safe_print, set_primary_selection, + single_instance, startup_notification_handler, which ) from .window import CommandOutput, CwdRequest, MatchPatternType, Window @@ -1339,9 +1339,14 @@ class Boss: tab = self.active_tab if tab is not None: bdata = data.encode('utf-8') if isinstance(data, str) else data - if is_macos and cmd[0] == '/usr/bin/less': - # the system less on macOS barfs up OSC codes, so sanitize them ourselves - bdata = re.sub(br'\x1b\].*?\x1b\\', b'', bdata) + if is_macos and cmd[0] == '/usr/bin/less' and macos_version()[:2] < (12, 3): + # the system less before macOS 12.3 barfs up OSC codes, so sanitize them ourselves + sentinel = os.path.join(cache_dir(), 'less-is-new-enough') + if not os.path.exists(sentinel): + if less_version() >= 581: + open(sentinel, 'w').close() + else: + bdata = re.sub(br'\x1b\].*?\x1b\\', b'', bdata) tab.new_special_window( SpecialWindow(cmd, bdata, title or _('History'), overlay_for=window.id, cwd=window.cwd_of_child), diff --git a/kitty/utils.py b/kitty/utils.py index 016950b06..e93378e28 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -945,3 +945,19 @@ def path_from_osc7_url(url: str) -> str: from urllib.parse import urlparse, unquote return unquote(urlparse(url).path) return '' + + +@run_once +def macos_version() -> Tuple[int, ...]: + import platform + return tuple(map(int, platform.mac_ver()[0].split('.'))) + + +@run_once +def less_version(less_exe: str = 'less') -> int: + import subprocess + o = subprocess.check_output([less_exe, '-V'], stderr=subprocess.STDOUT).decode() + m = re.match(r'less (\d+)', o) + if m is None: + raise ValueError(f'Invalid version string for less: {o}') + return int(m.group(1))