Compare commits
2 Commits
5f30862a83
...
66d6a728cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 66d6a728cc | |||
| 56189f4094 |
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ class NewHighlightingDialog(QDialog):
|
||||
|
||||
def _new_highlighter(self):
|
||||
highlight_regex = HighlightRegex("", ignore_case=True, is_regex=True, hit_background_color="ccb400",
|
||||
line_background_color="fff080")
|
||||
line_background_color="None")
|
||||
self._add_highlight_regex_to_list(highlight_regex, select=True)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user