Files
krowlog/src/ui/bigtext/highlighting.py
Andreas Huber 66d6a728cc 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.
2024-03-25 19:23:24 +01:00

70 lines
3.0 KiB
Python

import logging
from src.settings.settings import Settings
from src.ui.bigtext.highlight_regex import HighlightRegex
log = logging.getLogger("highlighting")
class Highlighting:
@staticmethod
def read_config(settings: Settings) -> [HighlightRegex]:
result = []
session = settings.session
for section in session.sections():
if not section.startswith("highlighting."):
continue
active = session.getboolean(section, "active", fallback=True)
query = session.get(section, "query", fallback="")
if len(query) == 0:
continue
ignore_case = session.getboolean(section, "ignore-case", fallback=True)
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(
query=query,
ignore_case=ignore_case,
is_regex=is_regex,
hit_background_color=hit_background_color,
line_background_color=line_background_color,
active=active,
activated_for_file_type=activated_for_file_type
)
result.append(highlight)
except:
log.exception("failed to parse query for highlighter: %s" % section)
continue
return result
@staticmethod
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.session.add_section(section)
settings.session.set(section, "active", str(highlighter.active))
settings.session.set(section, "query", highlighter.query)
settings.session.set(section, "ignore-case", str(highlighter.ignore_case))
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):
for section in settings.session.sections():
if not section.startswith("highlighting."):
continue
settings.session.remove_section(section)