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.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import fnmatch
|
||||||
|
import glob
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from src.ui.bigtext.highlight import Highlight
|
from src.ui.bigtext.highlight import Highlight
|
||||||
@@ -12,7 +14,7 @@ import re
|
|||||||
class HighlightRegex(Highlight):
|
class HighlightRegex(Highlight):
|
||||||
|
|
||||||
def __init__(self, query: str, ignore_case: bool, is_regex: bool, hit_background_color: str = "None",
|
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.active = active
|
||||||
self.query = query
|
self.query = query
|
||||||
self.ignore_case = ignore_case
|
self.ignore_case = ignore_case
|
||||||
@@ -20,6 +22,7 @@ class HighlightRegex(Highlight):
|
|||||||
self.regex = self._get_regex()
|
self.regex = self._get_regex()
|
||||||
self.hit_background_color = hit_background_color
|
self.hit_background_color = hit_background_color
|
||||||
self.line_background_color = line_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_hit = self.brush(self.hit_background_color)
|
||||||
self._brush_line = self.brush(self.line_background_color)
|
self._brush_line = self.brush(self.line_background_color)
|
||||||
|
|
||||||
@@ -99,3 +102,15 @@ class HighlightRegex(Highlight):
|
|||||||
alpha = int(color[6:8], 16)
|
alpha = int(color[6:8], 16)
|
||||||
return QBrush(QColor(red, green, blue, alpha))
|
return QBrush(QColor(red, green, blue, alpha))
|
||||||
return QBrush()
|
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
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class Highlighting:
|
|||||||
is_regex = session.getboolean(section, "is-regex", fallback=False)
|
is_regex = session.getboolean(section, "is-regex", fallback=False)
|
||||||
line_background_color = session.get(section, "line.background.color", fallback="None")
|
line_background_color = session.get(section, "line.background.color", fallback="None")
|
||||||
hit_background_color = session.get(section, "hit.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:
|
try:
|
||||||
highlight = HighlightRegex(
|
highlight = HighlightRegex(
|
||||||
@@ -33,7 +34,8 @@ class Highlighting:
|
|||||||
is_regex=is_regex,
|
is_regex=is_regex,
|
||||||
hit_background_color=hit_background_color,
|
hit_background_color=hit_background_color,
|
||||||
line_background_color=line_background_color,
|
line_background_color=line_background_color,
|
||||||
active=active
|
active=active,
|
||||||
|
activated_for_file_type=activated_for_file_type
|
||||||
)
|
)
|
||||||
result.append(highlight)
|
result.append(highlight)
|
||||||
except:
|
except:
|
||||||
@@ -57,6 +59,7 @@ class Highlighting:
|
|||||||
settings.session.set(section, "is-regex", str(highlighter.is_regex))
|
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, "line.background.color", highlighter.line_background_color)
|
||||||
settings.session.set(section, "hit.background.color", highlighter.hit_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
|
@staticmethod
|
||||||
def remove_highlighting_sections(settings: Settings):
|
def remove_highlighting_sections(settings: Settings):
|
||||||
|
|||||||
@@ -24,12 +24,13 @@ class LogFileModel:
|
|||||||
def __init__(self, file: str, settings: Settings):
|
def __init__(self, file: str, settings: Settings):
|
||||||
self.settings = settings
|
self.settings = settings
|
||||||
self._file = os.path.realpath(file)
|
self._file = os.path.realpath(file)
|
||||||
|
self._file_name = os.path.basename(self._file)
|
||||||
|
|
||||||
def highlighters(self):
|
def highlighters(self):
|
||||||
all_highlighters = Highlighting.read_config(self.settings)
|
all_highlighters = Highlighting.read_config(self.settings)
|
||||||
active_highlighters = []
|
active_highlighters = []
|
||||||
for h in all_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)
|
active_highlighters.append(h)
|
||||||
return active_highlighters
|
return active_highlighters
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ class HighlightListItemWidget(QWidget):
|
|||||||
self.active = QCheckBox("")
|
self.active = QCheckBox("")
|
||||||
self.active.setChecked(highlight_regex.is_active())
|
self.active.setChecked(highlight_regex.is_active())
|
||||||
self.active.stateChanged.connect(self._change_active_state)
|
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 = QLineEdit(self)
|
||||||
query.setText(highlight_regex.query)
|
query.setText(highlight_regex.query)
|
||||||
@@ -181,6 +181,18 @@ class HighlightListItemWidget(QWidget):
|
|||||||
is_regex.setEnabled(highlight_regex.is_active())
|
is_regex.setEnabled(highlight_regex.is_active())
|
||||||
self.layout.addWidget(is_regex, row, 3)
|
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):
|
def _change_active_state(self):
|
||||||
active = self.active.isChecked()
|
active = self.active.isChecked()
|
||||||
self.highlight_regex.set_active(active)
|
self.highlight_regex.set_active(active)
|
||||||
|
|||||||
Reference in New Issue
Block a user