add view that only shows matches

This commit is contained in:
2023-05-29 19:50:48 +02:00
parent 1950e4a1c4
commit 20f78a983b

View File

@@ -7,7 +7,7 @@ from typing import Optional, Callable
from PySide6.QtCore import QRunnable, QThreadPool, Signal from PySide6.QtCore import QRunnable, QThreadPool, Signal
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QCheckBox, QPushButton, QComboBox, \ from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QCheckBox, QPushButton, QComboBox, \
QSizePolicy, QProgressBar QSizePolicy, QProgressBar, QLineEdit
from src.plugins.domain.raction import RAction from src.plugins.domain.raction import RAction
from src.plugins.logfile.preprocesslineshook import PreProcessLinesHook from src.plugins.logfile.preprocesslineshook import PreProcessLinesHook
@@ -36,7 +36,9 @@ class FilterTask(QRunnable):
progress_handler: Callable[[float], None], progress_handler: Callable[[float], None],
update_hits_handler: Callable[[int], None], update_hits_handler: Callable[[int], None],
on_before: Callable[[], None], on_before: Callable[[], None],
on_finish: Callable[[], None] on_finish: Callable[[], None],
show_only_matches: bool,
matches_separator: str
): ):
super(FilterTask, self).__init__() super(FilterTask, self).__init__()
self.source_model = source_model self.source_model = source_model
@@ -49,6 +51,26 @@ class FilterTask(QRunnable):
self.on_finish = on_finish self.on_finish = on_finish
self.lock = lock self.lock = lock
self.filter_match_found_listeners = filter_match_found_listeners self.filter_match_found_listeners = filter_match_found_listeners
self.show_only_matches = show_only_matches
self.matches_separator = matches_separator
def only_matches(self, line: str, regex: re.Pattern):
result = ""
first = True
match_iter = re.finditer(regex, line)
for match in match_iter:
# print("%s" % match)
# 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):
if not first:
result = result + self.matches_separator
first = False
result = result + match.group(i)
return result + "\n" # todo preserve original line ending
def run(self): def run(self):
# print("writing to tmp file", self.filter_model.get_file()) # print("writing to tmp file", self.filter_model.get_file())
@@ -93,6 +115,9 @@ class FilterTask(QRunnable):
h: PreProcessLinesHook = hook h: PreProcessLinesHook = hook
line = h.pre_process_line(line, target) line = h.pre_process_line(line, target)
if self.show_only_matches:
target.write(self.only_matches(line, self.regex).encode("utf8"))
else:
target.write(line.encode("utf8")) target.write(line.encode("utf8"))
hits_count = hits_count + 1 hits_count = hits_count + 1
@@ -172,6 +197,18 @@ class FilterWidget(QWidget):
self.is_regex.setChecked(True) self.is_regex.setChecked(True)
self.is_regex.stateChanged.connect(self.filter_changed) self.is_regex.stateChanged.connect(self.filter_changed)
self.matches_separator = QLineEdit(",")
self.matches_separator.setMaximumWidth(15)
self.matches_separator.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.matches_separator.setVisible(False)
self.matches_separator.textChanged.connect(self.filter_changed)
self.show_only_matches = QCheckBox(_("only matches"))
self.show_only_matches.setChecked(False)
self.show_only_matches.stateChanged.connect(self.filter_changed)
self.show_only_matches.stateChanged.connect(
lambda: self.matches_separator.setVisible(self.show_only_matches.isChecked()))
filter_bar = QWidget() filter_bar = QWidget()
filter_bar.layout = QHBoxLayout(filter_bar) filter_bar.layout = QHBoxLayout(filter_bar)
filter_bar.layout.setContentsMargins(0, 0, 0, 0) filter_bar.layout.setContentsMargins(0, 0, 0, 0)
@@ -183,6 +220,8 @@ class FilterWidget(QWidget):
filter_bar.layout.addWidget(self.menu) filter_bar.layout.addWidget(self.menu)
filter_bar.layout.addWidget(self.ignore_case) filter_bar.layout.addWidget(self.ignore_case)
filter_bar.layout.addWidget(self.is_regex) filter_bar.layout.addWidget(self.is_regex)
filter_bar.layout.addWidget(self.show_only_matches)
filter_bar.layout.addWidget(self.matches_separator)
(handle, self.tmp_filename) = tempfile.mkstemp() (handle, self.tmp_filename) = tempfile.mkstemp()
os.close(handle) os.close(handle)
@@ -270,6 +309,7 @@ class FilterWidget(QWidget):
query = self.query_field.currentText() query = self.query_field.currentText()
ignore_case = self.ignore_case.isChecked() ignore_case = self.ignore_case.isChecked()
is_regex = self.is_regex.isChecked() is_regex = self.is_regex.isChecked()
show_only_matches = self.show_only_matches.isChecked()
# cancel previous search # cancel previous search
self._cancel_search() self._cancel_search()
@@ -308,6 +348,8 @@ class FilterWidget(QWidget):
self.progress_handler, self.progress_handler,
self.update_hits_handler, self.update_hits_handler,
lambda: self.search_is_running.emit(True), lambda: self.search_is_running.emit(True),
lambda: self.search_is_running.emit(False) lambda: self.search_is_running.emit(False),
show_only_matches,
self.matches_separator.text()
) )
QThreadPool.globalInstance().start(self.filter_task) QThreadPool.globalInstance().start(self.filter_task)