read highlighters from settings.ini
This commit is contained in:
57
highlighting.py
Normal file
57
highlighting.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import logging
|
||||
import re
|
||||
|
||||
from PyQt6.QtGui import QBrush, QColor
|
||||
|
||||
from highlight import Highlight
|
||||
from highlight_regex import HighlightRegex
|
||||
from settings import Settings
|
||||
|
||||
log = logging.getLogger("highlighting")
|
||||
|
||||
|
||||
class Highlighting:
|
||||
|
||||
@staticmethod
|
||||
def read_config(settings: Settings) -> [Highlight]:
|
||||
result = []
|
||||
config = settings.config
|
||||
|
||||
for section in config.sections():
|
||||
if not section.startswith("highlighting."):
|
||||
continue
|
||||
|
||||
query = config.get(section, "query", fallback="")
|
||||
if len(query) == 0:
|
||||
continue
|
||||
ignore_case = config.getboolean(section, "ignore-case", fallback=True)
|
||||
is_regex = config.getboolean(section, "is-regex", fallback=False)
|
||||
line_background_color = Highlighting.brush(config.get(section, "line.background.color", fallback="None"))
|
||||
hit_background_color = Highlighting.brush(config.get(section, "hit.background.color", fallback="None"))
|
||||
try:
|
||||
flags = re.IGNORECASE if ignore_case else 0
|
||||
if is_regex:
|
||||
regex = re.compile(query, flags=flags)
|
||||
else:
|
||||
regex = re.compile(re.escape(query), flags=flags)
|
||||
except:
|
||||
log.exception("failed to parse query for highlighter: %s" % section)
|
||||
continue
|
||||
|
||||
highlight = HighlightRegex(
|
||||
regex=regex,
|
||||
brush=hit_background_color,
|
||||
brush_full_line=line_background_color
|
||||
)
|
||||
result.append(highlight)
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def brush(color: str) -> QBrush:
|
||||
if re.match("[0-9a-f]{6}", color, flags=re.IGNORECASE):
|
||||
red = int(color[0:2], 16)
|
||||
green = int(color[2:4], 16)
|
||||
blue = int(color[4:6], 16)
|
||||
return QBrush(QColor(red, green, blue))
|
||||
return QBrush()
|
||||
Reference in New Issue
Block a user