Skip to content

Commit

Permalink
support for color theme plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
geier committed Oct 29, 2023
1 parent 080a39f commit 11d85bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions khal/plugins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable, Mapping
from typing import Tuple

from khal._compat import importlib_metadata

Expand All @@ -18,6 +19,12 @@ def _load_formatters() -> dict[str, Callable[[str, str], str]]:

FORMATTERS: Mapping[str, Callable[[str, str], str]] = _load_formatters()

def _load_color_themes() -> dict[str, Mapping[str, Tuple[str, str, str, str, str]]]:
color_theme_entrypoints = importlib_metadata.entry_points(group="khal.color_theme")
return {ep.name: ep.load() for ep in color_theme_entrypoints}

THEMES: Mapping[str, Mapping[str, dict[str, Tuple[str, str, str, str, str]]]] = _load_color_themes()

# class ParserExtensionInterface(Protocol):
# """An interface for parser extension plugins."""

Expand Down
2 changes: 1 addition & 1 deletion khal/settings/khal.spec
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ blank_line_before_day = boolean(default=False)
#
# __ http://urwid.org/manual/displayattributes.html
# .. _github: https://github.com/pimutils/khal/issues
theme = option('dark', 'light', default='dark')
theme = string(default='dark')

# Whether to show a visible frame (with *box drawing* characters) around some
# (groups of) elements or not. There are currently several different frame
Expand Down
15 changes: 13 additions & 2 deletions khal/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import urwid

from .. import utils
from .. import plugins
from ..khalendar import CalendarCollection
from ..khalendar.exceptions import ReadOnlyCalendarError
from ..khalendar.exceptions import FatalError, ReadOnlyCalendarError
from . import colors
from .base import Pane, Window
from .editor import EventEditor, ExportDialog
Expand Down Expand Up @@ -1356,6 +1357,17 @@ def start_pane(
color_mode: Literal['rgb', '256colors']='rgb',
):
"""Open the user interface with the given initial pane."""
# We don't validate the themes in settings.spec but instead here
# first try to load built-in themes, then try to load themes from
# plugins
theme = colors.themes.get(pane._conf['view']['theme'])
if theme is None:
theme = plugins.THEMES.get(pane._conf['view']['theme'])
if theme is None:
logger.fatal(f'Invalid theme {pane._conf["view"]["theme"]} configured')
logger.fatal(f'Available themes are: {", ".join(colors.themes.keys())}')
raise FatalError

quit_keys = quit_keys or ['q']

frame = Window(
Expand Down Expand Up @@ -1406,7 +1418,6 @@ def emit(self, record):
logger.addHandler(header_handler)

frame.open(pane, callback)
theme = getattr(colors, pane._conf['view']['theme'])
palette = _add_calendar_colors(
theme, pane.collection, color_mode=color_mode,
base='calendar', attr_template='calendar {}',
Expand Down

0 comments on commit 11d85bd

Please sign in to comment.