From cdd382858ad05b6f3e8677befd43987bfcd9b7ee Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Thu, 28 Oct 2021 10:22:15 +0200 Subject: [PATCH] highlight matches --- bigtext.py | 5 ++++- filterwidget.py | 11 ++++++++++- highlight_regex.py | 5 ++--- logFileModel.py | 21 ++++++++++++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/bigtext.py b/bigtext.py index c6b9936..d1c88e4 100644 --- a/bigtext.py +++ b/bigtext.py @@ -250,7 +250,10 @@ class InnerBigText(QWidget): for l in self.lines: self.update_longest_line(len(l.line())) - highlighters = self.model.highlights + [self.selection_highlight] + highlighters = self.model.highlights + if self.model.get_query_highlight(): + highlighters = highlighters + [self.model.get_query_highlight()] + highlighters = highlighters + [self.selection_highlight] # selection highlight should be last # draw hightlights first - some characters may overlap to the next line # by drawing the background hightlights first we prevent that the hightlight diff --git a/filterwidget.py b/filterwidget.py index f2f88bb..94dd437 100644 --- a/filterwidget.py +++ b/filterwidget.py @@ -119,11 +119,17 @@ class FilterWidget(QWidget): # print("cancel started ", time.time()) self.filter_task.aborted = True + def reset_filter(self): + self.filter_model.truncate() + self.source_model.set_query_highlight() + self.filter_model.set_query_highlight() + self.source_model.settings.callback_update_ui() + def filter_changed(self): query = self.query_field.text() ignore_case = self.ignore_case.isChecked() if len(query) == 0: - self.filter_model.truncate() + self.reset_filter() return # cancel previous search @@ -140,6 +146,9 @@ class FilterWidget(QWidget): self.filter_model.truncate() return + self.source_model.set_query_highlight(regex) + self.filter_model.set_query_highlight(regex) + self.filter_task = FilterTask( self.source_model, self.filter_model, diff --git a/highlight_regex.py b/highlight_regex.py index 9e02d49..cc2e4dd 100644 --- a/highlight_regex.py +++ b/highlight_regex.py @@ -14,7 +14,8 @@ import re class HighlightRegex(Highlight): - def __init__(self, regex: str, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen, brush_full_line=QBrush()): + def __init__(self, regex: re.Pattern, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen, + brush_full_line=QBrush()): self.regex = regex self.brush = brush self.pen = pen @@ -26,10 +27,8 @@ class HighlightRegex(Highlight): match_iter = re.finditer(self.regex, line.line()) for match in match_iter: #print("%s" % match) - group0 = match.group(0) start = match.start(0) end = match.end(0) - #print("regex: %s" % (group0)) result.append(HighlightedRange( start, end-start, diff --git a/logFileModel.py b/logFileModel.py index 5ee5cc4..6ab6ab4 100644 --- a/logFileModel.py +++ b/logFileModel.py @@ -1,9 +1,10 @@ import math +import re import threading import time -from typing import List +from typing import List, Optional -from PyQt6.QtGui import QBrush, QColor +from PyQt6.QtGui import QBrush, QColor, QPen from highlight_regex import HighlightRegex from line import Line @@ -12,6 +13,7 @@ from settings import Settings class LogFileModel: + _query_highlight: Optional[HighlightRegex] = None def __init__(self, file: str, settings: Settings): self.settings = settings @@ -20,8 +22,9 @@ class LogFileModel: self.highlights = [ HighlightRegex( - r"ERROR", + re.compile("ERROR"), brush=QBrush(QColor(220, 112, 122)), + pen=QPen(QColor(0, 0, 0)), brush_full_line=QBrush(QColor(255, 112, 122)) ) ] @@ -32,6 +35,18 @@ class LogFileModel: def __str__(self): return self._file + def get_query_highlight(self): + return self._query_highlight + + def set_query_highlight(self, regex: Optional[re.Pattern] = None): + if regex: + self._query_highlight = HighlightRegex( + regex, + brush=QBrush(QColor(255, 255, 0)) + ) + else: + self._query_highlight = None + def get_tab_name(self): file_name = os.path.basename(self._file) if len(file_name) > 35: