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 27, 2023
1 parent 25c5c26 commit 5ee70b5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions khal/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .ui.colors import register_color_theme # noqa
35 changes: 28 additions & 7 deletions khal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import click
import click_log
import xdg

from . import __version__, controllers, khalendar
from .exceptions import FatalError
Expand Down Expand Up @@ -252,6 +253,7 @@ def cli(ctx, config):
logger = logging.getLogger('khal')
logger.handlers = [logging.FileHandler(ctx.logfilepath)]
prepare_context(ctx, config)
find_load_plugins()

@cli.command()
@multi_calendar_option
Expand Down Expand Up @@ -512,15 +514,21 @@ def interactive(ctx, include_calendar, exclude_calendar, mouse):
def interactive_cli(ctx, config, include_calendar, exclude_calendar, mouse):
'''Interactive UI. Also launchable via `khal interactive`.'''
prepare_context(ctx, config)
find_load_plugins()
if mouse is not None:
ctx.obj['conf']['default']['enable_mouse'] = mouse
controllers.interactive(
build_collection(
ctx.obj['conf'],
multi_calendar_select(ctx, include_calendar, exclude_calendar)
),
ctx.obj['conf']
)
try:
controllers.interactive(
build_collection(
ctx.obj['conf'],
multi_calendar_select(ctx, include_calendar, exclude_calendar)
),
ctx.obj['conf']
)
except FatalError as error:
logger.debug(error, exc_info=True)
logger.fatal(error)
sys.exit(1)

@cli.command()
@multi_calendar_option
Expand Down Expand Up @@ -698,5 +706,18 @@ def configure(ctx):

return cli, interactive_cli

def find_load_plugins():
"""Find and load all plugins in the plugin directory."""
# check all folders in $XDG_DATA_HOME/khal/plugins if they contain a
# __init__.py file. If so, import them.
plugin_dir = os.path.join(xdg.BaseDirectory.xdg_data_home, 'khal', 'plugins')
if not os.path.isdir(plugin_dir):
return
sys.path.append(plugin_dir)
for plugin in os.listdir(plugin_dir):
if os.path.isfile(os.path.join(plugin_dir, plugin, '__init__.py')):
logger.debug(f'loading plugin {plugin}')
__import__(plugin)


main_khal, main_ikhal = _get_cli()
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
14 changes: 12 additions & 2 deletions khal/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from .. import utils
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 +1356,17 @@ def start_pane(
color_mode: Literal['rgb', '256colors']='rgb',
):
"""Open the user interface with the given initial pane."""

# we can't use configobj to do validation of theme for us, because when the
# config is passed, we haven't loaded plugins yet. We could load plugins
# earlier, but then logging wouldn't work at plugin loading time.
# We do this early, so that logger messages still get shown in a regular way
theme = colors.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 +1417,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
4 changes: 4 additions & 0 deletions khal/ui/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@
]

themes: Dict[str, List[Tuple[str, ...]]] = {'light': light, 'dark': dark}


def register_color_theme(name: str, theme: List[Tuple[str, ...]]):
themes[name] = theme

0 comments on commit 5ee70b5

Please sign in to comment.