From ee514dc3053b3c1aa37b62aa6c8fa10cde817693 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 29 Jan 2023 09:26:04 +0100 Subject: [PATCH] add support for regex groups If a filter regex contains groups, then we only highlight the group matches. --- src/ui/bigtext/highlight_regex.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ui/bigtext/highlight_regex.py b/src/ui/bigtext/highlight_regex.py index b2322e6..40a91e8 100644 --- a/src/ui/bigtext/highlight_regex.py +++ b/src/ui/bigtext/highlight_regex.py @@ -64,19 +64,21 @@ class HighlightRegex(Highlight): match_iter = re.finditer(self.regex, line.line()) for match in match_iter: # print("%s" % match) - start_char = match.start(0) - end_char = match.end(0) - start_column = line.char_to_column(start_char) - end_column = line.char_to_column(end_char) - - result.append(HighlightedRange( - start_column, - end_column - start_column, - highlight_full_line=True, - brush=self._brush_hit, - brush_full_line=self._brush_line - )) + # if the regex has groups then skip the first, it represents the whole match, + # but we only want to highlight the groups + first_group = 1 if len(match.groups()) > 0 else 0 + for i in range(first_group, len(match.groups()) + 1): + start_column = line.char_to_column(match.start(i)) + end_column = line.char_to_column(match.end(i)) + # print(f"highlight: {start_column}:{end_column} - {match.group(i)}") + result.append(HighlightedRange( + start_column, + end_column - start_column, + highlight_full_line=True, + brush=self._brush_hit, + brush_full_line=self._brush_line + )) return result