feat(config): add named configuration profiles

Profiles declared as `profile <name> { ... }` in kitty.conf are now collected
while the configuration is read and applied only when selected, so switching
between projects no longer requires duplicating or editing kitty.conf.

The selected profile is layered over the global configuration, meaning it only
overrides the options it declares. Selection happens with --profile or
KITTY_PROFILE, reloading keeps the active profile, and an unknown or malformed
profile is reported without blocking startup.
This commit is contained in:
BlackAngelTV committed 2026-11-07 15:53:05 +01:00
1 parent 154e91b906
commit 1cfe7217f9
9 files changed
+303 -9

No files matched your search

+68 -4
View File
@@ -3,12 +3,12 @@
import json
import os
from collections.abc import Callable, Generator, Iterable
from collections.abc import Callable, Generator, Iterable, Iterator, Sequence
from contextlib import contextmanager, suppress
from functools import partial
from typing import Any
from .conf.utils import BadLine, parse_config_base
from .conf.utils import BadLine, NamedLineIterator, ProfileLines, currently_parsing, parse_config_base
from .conf.utils import load_config as _load_config
from .constants import cache_dir, defconf
from .options.types import Options, defaults, option_names
@@ -162,6 +162,7 @@ def parse_config(
accumulate_bad_lines: list[BadLine] | None = None,
effective_config_lines: Callable[[str, str], None] | None = None,
allow_geninclude: bool = True,
profiles: dict[str, ProfileLines] | None = None,
) -> dict[str, Any]:
from .options.parse import create_result_dict, parse_conf_item
@@ -173,14 +174,66 @@ def parse_config(
accumulate_bad_lines=accumulate_bad_lines,
effective_config_lines=effective_config_lines,
allow_geninclude=allow_geninclude,
profiles=profiles,
)
return ans
def resolve_profile_name(profile: str | None = None) -> str:
"""The explicitly requested profile wins over the environment variable."""
ans = profile if profile else os.environ.get('KITTY_PROFILE', '')
return ans.strip()
def _lines_by_file(lines: Sequence[tuple[str, int, str]]) -> Iterator[tuple[str, list[str]]]:
"""Group consecutive lines read from the same file, padding them so that
their line numbers stay the ones of the original configuration file."""
group: list[str] = []
current = ''
for file, line_num, line in lines:
if group and file != current:
yield current, group
group = []
current = file
while len(group) < line_num - 1:
group.append('')
group.append(line)
if group:
yield current, group
def parse_profile_lines(
lines: Sequence[tuple[str, int, str]],
accumulate_bad_lines: list[BadLine] | None = None,
effective_config_lines: Callable[[str, str], None] | None = None,
) -> dict[str, Any]:
from .options.parse import actions, create_result_dict
ans = create_result_dict()
for file, group in _lines_by_file(lines):
# Lines read from STDIN or from the command line have no directory to
# resolve includes against, so let the parser fall back to config_dir.
name = file if file and file != '-' and not file.startswith('<') else ''
with currently_parsing.set_file(name):
vals = parse_config(
NamedLineIterator(name, iter(group)), accumulate_bad_lines=accumulate_bad_lines, effective_config_lines=effective_config_lines
)
for k, v in vals.items():
if isinstance(v, dict):
ans.setdefault(k, {}).update(v)
elif k in actions:
ans.setdefault(k, []).extend(v)
else:
ans[k] = v
return ans
effective_config_lines: list[str] = []
def load_config(*paths: str, overrides: Iterable[str] | None = None, accumulate_bad_lines: list[BadLine] | None = None) -> Options:
def load_config(
*paths: str, overrides: Iterable[str] | None = None, accumulate_bad_lines: list[BadLine] | None = None, profile: str | None = None
) -> Options:
from .options.parse import merge_result_dicts
from .options.types import secret_options
@@ -191,14 +244,24 @@ def load_config(*paths: str, overrides: Iterable[str] | None = None, accumulate_
effective_config_lines.append(line)
overrides = tuple(overrides) if overrides is not None else ()
profiles: dict[str, ProfileLines] = {}
opts_dict, found_paths = _load_config(
defaults,
partial(parse_config, accumulate_bad_lines=accumulate_bad_lines, effective_config_lines=add_effective_config_line),
partial(parse_config, accumulate_bad_lines=accumulate_bad_lines, effective_config_lines=add_effective_config_line, profiles=profiles),
merge_result_dicts,
*paths,
overrides=overrides,
)
active_profile = resolve_profile_name(profile)
if active_profile:
profile_lines = profiles.get(active_profile)
if profile_lines is None:
log_error(f'The configuration profile {active_profile!r} is not defined, ignoring it')
else:
# Parsed after the global configuration so that it is layered on top
# of it and only needs to list the options it changes.
opts_dict = merge_result_dicts(opts_dict, parse_profile_lines(profile_lines, accumulate_bad_lines, add_effective_config_line))
opts = Options(opts_dict)
opts.alias_map = build_action_aliases(opts.kitten_alias, 'kitten')
@@ -213,6 +276,7 @@ def load_config(*paths: str, overrides: Iterable[str] | None = None, accumulate_
opts.config_paths = found_paths
opts.all_config_paths = paths
opts.config_overrides = overrides
opts.profile_name = active_profile
return opts