From 66d6a728ccb06823c4cfd66a2319b7d867771c24 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Mon, 25 Mar 2024 19:23:24 +0100 Subject: [PATCH] make it possible to activate highlighter only for specific file types In stage 1 we use a glob pattern matching the file name. Stage 2 (which I will maybe implement some day) might use some additional magic byte sequence for file type detection. --- src/ui/bigtext/highlight_regex.py | 17 ++++++++++++++++- src/ui/bigtext/highlighting.py | 5 ++++- src/ui/bigtext/logFileModel.py | 3 ++- src/ui/bigtext/newhighlightingdialog.py | 14 +++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/ui/bigtext/highlight_regex.py b/src/ui/bigtext/highlight_regex.py index 40a91e8..607b04f 100644 --- a/src/ui/bigtext/highlight_regex.py +++ b/src/ui/bigtext/highlight_regex.py @@ -1,3 +1,5 @@ +import fnmatch +import glob from typing import Optional from src.ui.bigtext.highlight import Highlight @@ -12,7 +14,7 @@ import re class HighlightRegex(Highlight): def __init__(self, query: str, ignore_case: bool, is_regex: bool, hit_background_color: str = "None", - line_background_color: str = "None", active: bool = True): + line_background_color: str = "None", active: bool = True, activated_for_file_type: str = "*"): self.active = active self.query = query self.ignore_case = ignore_case @@ -20,6 +22,7 @@ class HighlightRegex(Highlight): self.regex = self._get_regex() self.hit_background_color = hit_background_color self.line_background_color = line_background_color + self.activated_for_file_type = activated_for_file_type self._brush_hit = self.brush(self.hit_background_color) self._brush_line = self.brush(self.line_background_color) @@ -99,3 +102,15 @@ class HighlightRegex(Highlight): alpha = int(color[6:8], 16) return QBrush(QColor(red, green, blue, alpha)) return QBrush() + + def set_activated_for_file_type(self, activated_for_file_type: str): + self.activated_for_file_type = activated_for_file_type + + def file_type_matches(self, file_name: str) -> bool: + if self.activated_for_file_type is None or len(self.activated_for_file_type) == 0: + return True + glob_patterns: [str] = self.activated_for_file_type.split(",") # support multiple globs like: "*.txt, *.csv" + for glob_pattern in glob_patterns: + if fnmatch.fnmatch(file_name, glob_pattern.strip()): + return True + return False diff --git a/src/ui/bigtext/highlighting.py b/src/ui/bigtext/highlighting.py index cb1940e..4267f43 100644 --- a/src/ui/bigtext/highlighting.py +++ b/src/ui/bigtext/highlighting.py @@ -25,6 +25,7 @@ class Highlighting: is_regex = session.getboolean(section, "is-regex", fallback=False) line_background_color = session.get(section, "line.background.color", fallback="None") hit_background_color = session.get(section, "hit.background.color", fallback="None") + activated_for_file_type = session.get(section, "activated-for-file-type", fallback="*") try: highlight = HighlightRegex( @@ -33,7 +34,8 @@ class Highlighting: is_regex=is_regex, hit_background_color=hit_background_color, line_background_color=line_background_color, - active=active + active=active, + activated_for_file_type=activated_for_file_type ) result.append(highlight) except: @@ -57,6 +59,7 @@ class Highlighting: settings.session.set(section, "is-regex", str(highlighter.is_regex)) settings.session.set(section, "line.background.color", highlighter.line_background_color) settings.session.set(section, "hit.background.color", highlighter.hit_background_color) + settings.session.set(section, "activated-for-file-type", highlighter.activated_for_file_type) @staticmethod def remove_highlighting_sections(settings: Settings): diff --git a/src/ui/bigtext/logFileModel.py b/src/ui/bigtext/logFileModel.py index 1915a62..45e8e4e 100644 --- a/src/ui/bigtext/logFileModel.py +++ b/src/ui/bigtext/logFileModel.py @@ -24,12 +24,13 @@ class LogFileModel: def __init__(self, file: str, settings: Settings): self.settings = settings self._file = os.path.realpath(file) + self._file_name = os.path.basename(self._file) def highlighters(self): all_highlighters = Highlighting.read_config(self.settings) active_highlighters = [] for h in all_highlighters: - if h.is_active(): + if h.is_active() and h.file_type_matches(self._file_name): active_highlighters.append(h) return active_highlighters diff --git a/src/ui/bigtext/newhighlightingdialog.py b/src/ui/bigtext/newhighlightingdialog.py index 6aca94e..7de03f8 100644 --- a/src/ui/bigtext/newhighlightingdialog.py +++ b/src/ui/bigtext/newhighlightingdialog.py @@ -139,7 +139,7 @@ class HighlightListItemWidget(QWidget): self.active = QCheckBox("") self.active.setChecked(highlight_regex.is_active()) self.active.stateChanged.connect(self._change_active_state) - self.layout.addWidget(self.active, row, 0, 3, 1, alignment=QtCore.Qt.AlignmentFlag.AlignVCenter) + self.layout.addWidget(self.active, row, 0, 4, 1, alignment=QtCore.Qt.AlignmentFlag.AlignVCenter) query = QLineEdit(self) query.setText(highlight_regex.query) @@ -181,6 +181,18 @@ class HighlightListItemWidget(QWidget): is_regex.setEnabled(highlight_regex.is_active()) self.layout.addWidget(is_regex, row, 3) + row = row + 1 + activated_for_file_type_label = QLabel(_("File Type:"), self) + activated_for_file_type_label.setEnabled(highlight_regex.is_active()) + self.layout.addWidget(activated_for_file_type_label, row, 1) + activated_for_file_type = QLineEdit(self) + activated_for_file_type.setEnabled(highlight_regex.is_active()) + activated_for_file_type.setText(highlight_regex.activated_for_file_type) + activated_for_file_type.textChanged[str].connect( + lambda: highlight_regex.set_activated_for_file_type(activated_for_file_type.text())) + self.layout.addWidget(activated_for_file_type, row, 2) + + def _change_active_state(self): active = self.active.isChecked() self.highlight_regex.set_active(active)