diff --git a/docs/changelog.rst b/docs/changelog.rst index 411558d79..881016781 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -61,6 +61,7 @@ Detailed list of changes - When drawing the tab bar have the default left and right margins drawn in a color matching the neighboring tab (:iss:`5719`) +- When using the :code:`include` directive in :file:`kitty.conf` make the environment variable :envvar:`KITTY_OS` available for OS specific config. 0.26.5 [2022-11-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/conf.rst b/docs/conf.rst index 348b6e29d..87d3517d0 100644 --- a/docs/conf.rst +++ b/docs/conf.rst @@ -31,7 +31,9 @@ You can include secondary config files via the :code:`include` directive. If you use a relative path for :code:`include`, it is resolved with respect to the location of the current config file. Note that environment variables are expanded, so :code:`${USER}.conf` becomes :file:`name.conf` if -:code:`USER=name`. Also, you can use :code:`globinclude` to include files +:code:`USER=name`. A special environment variable :envvar:`KITTY_OS` is available, +to detect the operating system. It is ``linux``, ``macos`` or ``bsd``. +Also, you can use :code:`globinclude` to include files matching a shell glob pattern and :code:`envinclude` to include configuration from environment variables. For example:: diff --git a/docs/glossary.rst b/docs/glossary.rst index f8af4a0ae..7bc0ff684 100644 --- a/docs/glossary.rst +++ b/docs/glossary.rst @@ -212,3 +212,8 @@ Variables that kitty sets when running child programs Set when enabling :ref:`shell_integration` with :program:`bash`, allowing :program:`bash` to automatically load the integration script. + +.. envvar:: KITTY_OS + + Set when using the include directive in kitty.conf. Can take values: + ``linux``, ``macos``, ``darwin``. diff --git a/kitty/conf/utils.py b/kitty/conf/utils.py index 553892aaa..993711a0c 100644 --- a/kitty/conf/utils.py +++ b/kitty/conf/utils.py @@ -7,13 +7,14 @@ import shlex import sys from contextlib import contextmanager from typing import ( - Any, Callable, Dict, Generator, Generic, Iterable, Iterator, List, - NamedTuple, Optional, Sequence, Set, Tuple, TypeVar, Union + Any, Callable, Dict, Generator, Generic, Iterable, Iterator, List, NamedTuple, + Optional, Sequence, Set, Tuple, TypeVar, Union, ) +from ..constants import _plat, is_macos from ..fast_data_types import Color from ..rgb import to_color as as_color -from ..types import ConvertibleToNumbers, ParsedShortcut +from ..types import ConvertibleToNumbers, ParsedShortcut, run_once from ..typing import Protocol from ..utils import expandvars, log_error @@ -159,6 +160,17 @@ class CurrentlyParsing: currently_parsing = CurrentlyParsing() +@run_once +def os_name() -> str: + if is_macos: + return 'macos' + if 'bsd' in _plat: + return 'bsd' + if 'linux' in _plat: + return 'linux' + return 'unknown' + + class NamedLineIterator: def __init__(self, name: str, lines: Iterator[str]): @@ -185,7 +197,7 @@ def parse_line( return key, val = m.groups() if key in ('include', 'globinclude', 'envinclude'): - val = os.path.expandvars(os.path.expanduser(val.strip())) + val = expandvars(os.path.expanduser(val.strip()), {'KITTY_OS': os_name()}) if key == 'globinclude': from pathlib import Path vals = tuple(map(lambda x: str(os.fspath(x)), sorted(Path(base_path_for_includes).glob(val))))