When using the include directive in kitty.conf make the environment variable KITTY_OS available for OS specific config

This commit is contained in:
Kovid Goyal 2022-12-04 21:03:24 +05:30
parent fda4aa21a1
commit d7d96c2e7b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 25 additions and 5 deletions

View File

@ -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 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] 0.26.5 [2022-11-07]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -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 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 location of the current config file. Note that environment variables are
expanded, so :code:`${USER}.conf` becomes :file:`name.conf` if 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 matching a shell glob pattern and :code:`envinclude` to include configuration
from environment variables. For example:: from environment variables. For example::

View File

@ -212,3 +212,8 @@ Variables that kitty sets when running child programs
Set when enabling :ref:`shell_integration` with :program:`bash`, allowing Set when enabling :ref:`shell_integration` with :program:`bash`, allowing
:program:`bash` to automatically load the integration script. :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``.

View File

@ -7,13 +7,14 @@ import shlex
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
from typing import ( from typing import (
Any, Callable, Dict, Generator, Generic, Iterable, Iterator, List, Any, Callable, Dict, Generator, Generic, Iterable, Iterator, List, NamedTuple,
NamedTuple, Optional, Sequence, Set, Tuple, TypeVar, Union Optional, Sequence, Set, Tuple, TypeVar, Union,
) )
from ..constants import _plat, is_macos
from ..fast_data_types import Color from ..fast_data_types import Color
from ..rgb import to_color as as_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 ..typing import Protocol
from ..utils import expandvars, log_error from ..utils import expandvars, log_error
@ -159,6 +160,17 @@ class CurrentlyParsing:
currently_parsing = 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: class NamedLineIterator:
def __init__(self, name: str, lines: Iterator[str]): def __init__(self, name: str, lines: Iterator[str]):
@ -185,7 +197,7 @@ def parse_line(
return return
key, val = m.groups() key, val = m.groups()
if key in ('include', 'globinclude', 'envinclude'): 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': if key == 'globinclude':
from pathlib import Path from pathlib import Path
vals = tuple(map(lambda x: str(os.fspath(x)), sorted(Path(base_path_for_includes).glob(val)))) vals = tuple(map(lambda x: str(os.fspath(x)), sorted(Path(base_path_for_includes).glob(val))))