add dialog to manage highlighters
- no support for "no color" - settings not saved to disk
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from PyQt6.QtGui import QBrush, QColor
|
||||
|
||||
@@ -9,11 +10,10 @@ from settings import Settings
|
||||
|
||||
log = logging.getLogger("highlighting")
|
||||
|
||||
|
||||
class Highlighting:
|
||||
|
||||
@staticmethod
|
||||
def read_config(settings: Settings) -> [Highlight]:
|
||||
def read_config(settings: Settings) -> [HighlightRegex]:
|
||||
result = []
|
||||
config = settings.config
|
||||
|
||||
@@ -26,32 +26,42 @@ class Highlighting:
|
||||
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"))
|
||||
line_background_color = config.get(section, "line.background.color", fallback="None")
|
||||
hit_background_color = 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)
|
||||
highlight = HighlightRegex(
|
||||
query=query,
|
||||
ignore_case=ignore_case,
|
||||
is_regex=is_regex,
|
||||
hit_background_color=hit_background_color,
|
||||
line_background_color=line_background_color
|
||||
)
|
||||
result.append(highlight)
|
||||
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()
|
||||
def write_config(settings: Settings, highlighters: [HighlightRegex]):
|
||||
Highlighting.remove_highlighting_sections(settings)
|
||||
section_counter = 0
|
||||
for highlighter in highlighters:
|
||||
highlighter: HighlightRegex = highlighter
|
||||
section = "highlighting.%d" % section_counter
|
||||
section_counter = section_counter + 1
|
||||
settings.config.add_section(section)
|
||||
settings.config.set(section, "query", highlighter.query)
|
||||
settings.config.set(section, "ignore-case", str(highlighter.ignore_case))
|
||||
settings.config.set(section, "is-regex", str(highlighter.is_regex))
|
||||
settings.config.set(section, "line.background.color", highlighter.line_background_color)
|
||||
settings.config.set(section, "hit.background.color", highlighter.hit_background_color)
|
||||
|
||||
@staticmethod
|
||||
def remove_highlighting_sections(settings: Settings):
|
||||
for section in settings.config.sections():
|
||||
if not section.startswith("highlighting."):
|
||||
continue
|
||||
settings.config.remove_section(section)
|
||||
|
||||
Reference in New Issue
Block a user