browson/nodeview.py
def _highlight_matches(self, line: str) -> str:
        """Apply search highlight to the given rendered line.

        Return 'line' with its original terminal escapes, as well as with
        'self.search' styled with black text on yellow background.
        """
        # This is largely an exercise in proper handling of terminal escapes.
        # We must search as if the terminal escapes are not there, but then
        # prepare the result by combining the existing terminal escapes with
        # new ones (from the search highlighting).
        haystack = self.term.strip_seqs(line)
        assert self.term.length(line) == len(haystack)  # sanity
        needle = self.search

        def term_escapes_before(index):
            """Return terminal escapes that occur before 'index' in line."""
            letters = []
            escapes = []
            for fragment in self.term.split_seqs(line):
                fraglen = self.term.length(fragment)
                assert fraglen in [0, 1]
                [escapes, letters][fraglen].append(fragment)
                if len(letters) > index:
                    break
            return "".join(escapes)

        def highlight_needle(match):
            pre_escapes = term_escapes_before(match.end())
            substr = match.string[match.start() : match.end()]
            return self.term.black_on_bright_yellow(substr) + pre_escapes

        def combine_escapes(line1: str, line2: str) -> Iterator[str]:
            assert self.term.strip_seqs(line1) == self.term.strip_seqs(line2)
            seq1 = self.term.split_seqs(line1)
            seq2 = self.term.split_seqs(line2)
            while seq1 and seq2:
                if self.term.length(seq1[0]) == 0:  # term escape in line1
                    yield seq1.pop(0)
                elif self.term.length(seq2[0]) == 0:  # term escape in line2
                    yield seq2.pop(0)
                else:  # letter in both strings
                    assert seq1[0] == seq2[0]
                    yield seq1.pop(0)
                    seq2.pop(0)
            # Yield remainder of whichever string has escapes left in it
            yield from seq1
            yield from seq2

        highlighted = re.sub(needle, highlight_needle, haystack)
        # Re-combine original terminal escapes from line with the highlights
        return "".join(combine_escapes(line, highlighted))
Apply search highlight to the given rendered line.

        Return 'line' with its original terminal escapes, as well as with
        'self.search' styled with black text on yellow background.
        
Apply search highlight to the given rendered line.
Return 'line' with its original terminal escapes, as well as with
'self.search' styled with black text on yellow background.
Apply search highlight to the given rendered line . Return ' line ' with its original terminal escapes as well as with ' self . search ' styled with black text on yellow background .
def _highlight_matches(self, line: str) -> str:
        haystack = self.term.strip_seqs(line)
        assert self.term.length(line) == len(haystack)  
        needle = self.search
        def term_escapes_before(index):
            letters = []
            escapes = []
            for fragment in self.term.split_seqs(line):
                fraglen = self.term.length(fragment)
                assert fraglen in [0, 1]
                [escapes, letters][fraglen].append(fragment)
                if len(letters) > index:
                    break
            return "".join(escapes)
        def highlight_needle(match):
            pre_escapes = term_escapes_before(match.end())
            substr = match.string[match.start() : match.end()]
            return self.term.black_on_bright_yellow(substr) + pre_escapes
        def combine_escapes(line1: str, line2: str) -> Iterator[str]:
            assert self.term.strip_seqs(line1) == self.term.strip_seqs(line2)
            seq1 = self.term.split_seqs(line1)
            seq2 = self.term.split_seqs(line2)
            while seq1 and seq2:
                if self.term.length(seq1[0]) == 0:  
                    yield seq1.pop(0)
                elif self.term.length(seq2[0]) == 0:  
                    yield seq2.pop(0)
                else:  
                    assert seq1[0] == seq2[0]
                    yield seq1.pop(0)
                    seq2.pop(0)
            yield from seq1
            yield from seq2
        highlighted = re.sub(needle, highlight_needle, haystack)
        return "".join(combine_escapes(line, highlighted))
def _highlight_matches ( self , line : str ) -> str : haystack = self . term . strip_seqs ( line ) assert self . term . length ( line ) == len ( haystack ) needle = self . search def term_escapes_before ( index ) : """Return terminal escapes that occur before 'index' in line.""" letters = [ ] escapes = [ ] for fragment in self . term . split_seqs ( line ) : fraglen = self . term . length ( fragment ) assert fraglen in [ 0 , 1 ] [ escapes , letters ] [ fraglen ] . append ( fragment ) if len ( letters ) > index : break return "" . join ( escapes ) def highlight_needle ( match ) : pre_escapes = term_escapes_before ( match . end ( ) ) substr = match . string [ match . start ( ) : match . end ( ) ] return self . term . black_on_bright_yellow ( substr ) + pre_escapes def combine_escapes ( line1 : str , line2 : str ) -> Iterator [ str ] : assert self . term . strip_seqs ( line1 ) == self . term . strip_seqs ( line2 ) seq1 = self . term . split_seqs ( line1 ) seq2 = self . term . split_seqs ( line2 ) while seq1 and seq2 : if self . term . length ( seq1 [ 0 ] ) == 0 : yield seq1 . pop ( 0 ) elif self . term . length ( seq2 [ 0 ] ) == 0 : yield seq2 . pop ( 0 ) else : assert seq1 [ 0 ] == seq2 [ 0 ] yield seq1 . pop ( 0 ) seq2 . pop ( 0 ) yield from seq1 yield from seq2 highlighted = re . sub ( needle , highlight_needle , haystack ) return "" . join ( combine_escapes ( line , highlighted ) )
Apply search highlight to the given rendered line.
Apply search highlight to the given rendered line .
"""Apply search highlight to the given rendered line.

        Return 'line' with its original terminal escapes, as well as with
        'self.search' styled with black text on yellow background.
        """ # This is largely an exercise in proper handling of terminal escapes. # We must search as if the terminal escapes are not there, but then # prepare the result by combining the existing terminal escapes with # new ones (from the search highlighting). # sanity """Return terminal escapes that occur before 'index' in line.""" # term escape in line1 # term escape in line2 # letter in both strings # Yield remainder of whichever string has escapes left in it # Re-combine original terminal escapes from line with the highlights
self
line
str