From ca2c419c9b76e9ac751097aad291eb5ffdb73289 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 29 May 2019 09:13:55 +0530 Subject: [PATCH] Fix colors not being preserved when using the pipe command with the pager history buffer Fixes #1657 --- docs/changelog.rst | 3 +++ kitty/window.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 2dcb44fea..bf16f75e3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -13,6 +13,9 @@ To update |kitty|, :doc:`follow the instructions `. - Fix a missing newline when using the pipe command between the scrollback and screen contents (:iss:`1642`) +- Fix colors not being preserved when using the pipe command with + the pager history buffer (:pull:`1657`) + - macOS: Fix a regression that could cause rendering of a kitty window to occasionally freeze in certain situations, such as moving it between monitors or transitioning from/to fullscreen (:iss:`1641`) diff --git a/kitty/window.py b/kitty/window.py index a0cdc2b98..09d9aee30 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -111,8 +111,12 @@ def setup_colors(screen, opts): def text_sanitizer(as_ansi, add_wrap_markers): - import re - pat = re.compile(r'\033\[.+?m') + pat = getattr(text_sanitizer, 'pat', None) + if pat is None: + import re + pat = text_sanitizer.pat = re.compile(r'\033\[.+?m') + + ansi, wrap_markers = not as_ansi, not add_wrap_markers def remove_wrap_markers(line): return line.replace('\r', '') @@ -123,8 +127,8 @@ def remove_sgr(line): def remove_both(line): return pat.sub('', line.replace('\r', '')) - if as_ansi: - return remove_both if add_wrap_markers else remove_sgr + if ansi: + return remove_both if wrap_markers else remove_sgr return remove_wrap_markers @@ -483,7 +487,7 @@ def as_text(self, as_ansi=False, add_history=False, add_wrap_markers=False, alte if add_history: h = [] self.screen.historybuf.pagerhist_as_text(h.append) - if not as_ansi or not add_wrap_markers: + if h and (not as_ansi or not add_wrap_markers): sanitizer = text_sanitizer(as_ansi, add_wrap_markers) h = list(map(sanitizer, h)) self.screen.historybuf.as_text(h.append, as_ansi, add_wrap_markers)