mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-20 23:25:46 +00:00
Fix #10694
This commit is contained in:
parent
56deb9e86d
commit
a2b7721bfb
4 changed files with 122 additions and 53 deletions
|
|
@ -235,6 +235,11 @@ Detailed list of changes
|
|||
and :code:`cursor-trail-pixiedust` shaders that make particles fly off the
|
||||
cursor as it moves, in the style of neovide (:iss:`10587`)
|
||||
|
||||
- Custom shaders: Fix shaders specified by absolute paths or paths starting
|
||||
with :file:`~` not being found in some cases, allow specifying names with the
|
||||
:file:`.slang` extension and look for shaders in the :file:`shaders`
|
||||
sub-directory of the config directory as documented (:iss:`10694`)
|
||||
|
||||
- Allow docking kitty windows to the edges of other windows or of the tab, for
|
||||
status bars, file trees, etc. Create them with :code:`launch --type=window-dock`
|
||||
or :code:`launch --type=tab-dock`, see :ref:`docks`
|
||||
|
|
|
|||
|
|
@ -183,15 +183,21 @@ tries to load a pipeline file with that name and if no pipeline file is found
|
|||
but a shader with that name is found instead, it is loaded with a default
|
||||
pipeline file. Loading of pipeline files takes place in the following order:
|
||||
|
||||
#. If the specified pipeline file is an absolute path, it is loaded directly
|
||||
#. If the specified pipeline file is an absolute path, it is loaded directly.
|
||||
A leading :file:`~` and environment variables are expanded.
|
||||
#. If a relative path it is loaded from the :file:`shaders` sub-directory
|
||||
inside the kitty config directory
|
||||
#. It is looked for amongst the custom shaders shipped with kitty
|
||||
#. A default pipeline file is constructed
|
||||
|
||||
The :file:`.pipeline` extension is added automatically if not present. If the
|
||||
name ends with :file:`.slang`, no pipeline file is looked for and the shader is
|
||||
loaded directly, which is useful when a pipeline and a shader have the same name.
|
||||
|
||||
The shaders that a pipeline references are first looked for in the directory
|
||||
from which the pipeline file is loaded, then they follow the same order as for
|
||||
pipeline files.
|
||||
pipeline files. For shaders, the name is first tried exactly as specified and
|
||||
then with the :file:`.slang` extension added.
|
||||
|
||||
The shader part
|
||||
^^^^^^^^^^^^^^^^^
|
||||
|
|
@ -300,8 +306,10 @@ Directives inside a group:
|
|||
|
||||
``shaders <name> [name …]``
|
||||
One or more shader names to run in order. Shaders are searched for first
|
||||
in the :file:`shaders/` subdirectory of the kitty config directory, then
|
||||
among the shaders shipped with kitty.
|
||||
in the directory containing the pipeline file, then in the :file:`shaders/`
|
||||
subdirectory of the kitty config directory, then among the shaders shipped
|
||||
with kitty. Absolute paths, including ones starting with :file:`~`, are
|
||||
loaded directly.
|
||||
|
||||
``var <type> <name> <value>``
|
||||
Same as the top-level ``var`` but scoped to this group. Merged with
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ from itertools import chain, product
|
|||
from types import MappingProxyType
|
||||
from typing import Any, Callable, Iterable, Iterator, Literal, NamedTuple, TypedDict, TypeGuard
|
||||
|
||||
from kitty.constants import read_kitty_resource, shaders_dir, slangc
|
||||
from kitty.constants import config_dir, read_kitty_resource, shaders_dir, slangc
|
||||
from kitty.fast_data_types import (
|
||||
ANIMATION_SAMPLE_WAIT,
|
||||
BGIMAGE_PROGRAM,
|
||||
|
|
@ -66,7 +66,7 @@ from kitty.fast_data_types import (
|
|||
from kitty.options.types import Options, defaults
|
||||
from kitty.options.utils import USER_ANIMATION_EVENT_PREFIX, EasingFunction, is_valid_user_animation_event_name
|
||||
from kitty.types import run_once
|
||||
from kitty.utils import lock_with_file, log_error, resolve_custom_file
|
||||
from kitty.utils import expandvars, lock_with_file, log_error
|
||||
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
|
|
@ -1422,65 +1422,75 @@ def key(*items: str | bytes) -> bytes:
|
|||
return h.hexdigest().encode()
|
||||
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
def custom_shader(name: str = '', pipeline_dir: str = '') -> tuple[str, str, bytes, bytes]:
|
||||
import_dir = ''
|
||||
if not name:
|
||||
src = get_custom_shader_src('types')
|
||||
return name, import_dir, src, key(src)
|
||||
def _expand_custom_path(name: str) -> str:
|
||||
opts: Options | None = None
|
||||
with suppress(RuntimeError):
|
||||
opts = get_options()
|
||||
return expandvars(os.path.expanduser(name), opts.env if opts else {})
|
||||
|
||||
found_path = ''
|
||||
src = b''
|
||||
|
||||
if pipeline_dir:
|
||||
candidate = os.path.join(pipeline_dir, f'{name}.slang')
|
||||
def _custom_file_candidates(name: str, ext: str, search_dirs: Iterable[str]) -> Iterator[str]:
|
||||
# Exact name first, then with the extension added, so that names that
|
||||
# already have the extension or are absolute paths work as expected
|
||||
name = _expand_custom_path(name)
|
||||
names = (name,) if name.endswith(ext) else (name, name + ext)
|
||||
if os.path.isabs(name):
|
||||
yield from names
|
||||
return
|
||||
seen: set[str] = set()
|
||||
for d in search_dirs:
|
||||
if d:
|
||||
d = os.path.normpath(os.path.abspath(d))
|
||||
if d not in seen:
|
||||
seen.add(d)
|
||||
for n in names:
|
||||
yield os.path.join(d, n)
|
||||
|
||||
|
||||
def _read_first(candidates: Iterable[str], tried: list[str]) -> tuple[str, bytes]:
|
||||
for candidate in candidates:
|
||||
tried.append(candidate)
|
||||
try:
|
||||
with open(candidate, 'rb') as f:
|
||||
src = f.read()
|
||||
found_path = os.path.abspath(candidate)
|
||||
import_dir = os.path.dirname(found_path)
|
||||
except FileNotFoundError:
|
||||
return os.path.abspath(candidate), f.read()
|
||||
except (FileNotFoundError, IsADirectoryError, NotADirectoryError):
|
||||
pass
|
||||
return '', b''
|
||||
|
||||
if not found_path:
|
||||
config_path = resolve_custom_file(f'{name}.slang')
|
||||
config_shader_dir = os.path.dirname(os.path.abspath(config_path))
|
||||
if not pipeline_dir or os.path.normpath(os.path.abspath(pipeline_dir)) != os.path.normpath(config_shader_dir):
|
||||
try:
|
||||
with open(config_path, 'rb') as f:
|
||||
src = f.read()
|
||||
found_path = os.path.abspath(config_path)
|
||||
import_dir = os.path.dirname(found_path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
if not found_path:
|
||||
src = get_custom_shader_src(name)
|
||||
return name, import_dir, src, key(src)
|
||||
def _builtin_src(reader: Callable[[str], bytes], name: str, tried: list[str]) -> bytes:
|
||||
try:
|
||||
return reader(name)
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(f'Could not find {name!r} amongst the builtins or at any of: {", ".join(tried)}') from None
|
||||
|
||||
return found_path, import_dir, src, key(src)
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
def custom_shader(name: str = '', pipeline_dir: str = '') -> tuple[str, str, bytes, bytes]:
|
||||
if not name:
|
||||
src = get_custom_shader_src('types')
|
||||
return name, '', src, key(src)
|
||||
|
||||
tried: list[str] = []
|
||||
found_path, src = _read_first(_custom_file_candidates(name, '.slang', (pipeline_dir, os.path.join(config_dir, 'shaders'))), tried)
|
||||
if found_path:
|
||||
return found_path, os.path.dirname(found_path), src, key(src)
|
||||
src = _builtin_src(get_custom_shader_src, name.removesuffix('.slang'), tried)
|
||||
return name, '', src, key(src)
|
||||
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
def pipeline_definition(name: str) -> tuple[tuple[str, ...], str]:
|
||||
name = os.path.expanduser(name)
|
||||
if os.path.isabs(name):
|
||||
pipeline_path = name if name.endswith('.pipeline') else name + '.pipeline'
|
||||
with open(pipeline_path, 'rb') as f:
|
||||
src = f.read()
|
||||
return tuple(src.decode().splitlines()), os.path.dirname(os.path.abspath(pipeline_path))
|
||||
|
||||
filename = name if name.endswith('.pipeline') else f'{name}.pipeline'
|
||||
path = resolve_custom_file(f'shaders/{filename}')
|
||||
try:
|
||||
with open(path, 'rb') as f:
|
||||
src = f.read()
|
||||
pipeline_dir = os.path.dirname(os.path.abspath(path))
|
||||
except FileNotFoundError:
|
||||
base_name = name[: -len('.pipeline')] if name.endswith('.pipeline') else name
|
||||
src = get_custom_pipeline_src(base_name)
|
||||
pipeline_dir = ''
|
||||
return tuple(src.decode().splitlines()), pipeline_dir
|
||||
if name.endswith('.slang'):
|
||||
raise FileNotFoundError(f'{name} is a shader not a pipeline')
|
||||
tried: list[str] = []
|
||||
found_path, src = _read_first(
|
||||
(c for c in _custom_file_candidates(name, '.pipeline', (os.path.join(config_dir, 'shaders'),)) if c.endswith('.pipeline')), tried
|
||||
)
|
||||
if found_path:
|
||||
return tuple(src.decode().splitlines()), os.path.dirname(found_path)
|
||||
src = _builtin_src(get_custom_pipeline_src, name.removesuffix('.pipeline'), tried)
|
||||
return tuple(src.decode().splitlines()), ''
|
||||
|
||||
|
||||
class NamedTexture(StrEnum):
|
||||
|
|
|
|||
|
|
@ -837,3 +837,49 @@ float f() { static const float SCAN = 2; return SCAN; }
|
|||
self.assertEqual(import_dir, tmpdir)
|
||||
finally:
|
||||
clear_caches()
|
||||
|
||||
def test_custom_shader_discovery(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pdir = os.path.join(tmpdir, 'pipelines')
|
||||
os.mkdir(pdir)
|
||||
|
||||
def w(path: str, data: str) -> str:
|
||||
path = os.path.join(tmpdir, path)
|
||||
with open(path, 'w') as f:
|
||||
f.write(data)
|
||||
return path
|
||||
|
||||
effect = w('effect.slang', '// effect shader\n')
|
||||
w('effect.pipeline', 'startgroup\n shaders effect\nendgroup\n')
|
||||
notch = w('notch.slang', '// notch shader\n')
|
||||
w('pipelines/other.slang', '// other\n')
|
||||
orig_home = os.environ.get('HOME')
|
||||
os.environ['HOME'] = tmpdir
|
||||
clear_caches()
|
||||
try:
|
||||
# absolute paths with the extension are not extended again
|
||||
self.assertEqual(custom_shader(effect)[0], effect)
|
||||
self.assertEqual(custom_shader(effect[: -len('.slang')])[0], effect)
|
||||
# ~ is expanded both at top level and inside a pipeline, even
|
||||
# when the pipeline is in the same directory as the shader
|
||||
self.assertEqual(custom_shader('~/effect.slang')[0], effect)
|
||||
self.assertEqual(custom_shader('~/notch', tmpdir)[0], notch)
|
||||
self.assertEqual(custom_shader('~/notch', pdir)[0], notch)
|
||||
self.assertEqual(custom_shader('other', pdir)[0], os.path.join(pdir, 'other.slang'))
|
||||
lines, pipeline_dir = pipeline_definition('~/effect')
|
||||
self.assertEqual(pipeline_dir, tmpdir)
|
||||
self.assertIn(' shaders effect', lines)
|
||||
# names ending with .slang never load pipelines
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
pipeline_definition('~/effect.slang')
|
||||
# builtins work with or without the extension
|
||||
self.assertEqual(custom_shader('crt.slang')[2], custom_shader('crt')[2])
|
||||
with self.assertRaises(FileNotFoundError) as cm:
|
||||
custom_shader('~/does-not-exist')
|
||||
self.assertIn(os.path.join(tmpdir, 'does-not-exist.slang'), str(cm.exception))
|
||||
finally:
|
||||
if orig_home is None:
|
||||
os.environ.pop('HOME', None)
|
||||
else:
|
||||
os.environ['HOME'] = orig_home
|
||||
clear_caches()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue