diff --git a/src/hyprshade/cli/ls.py b/src/hyprshade/cli/ls.py index d42ecc9..41a2f93 100644 --- a/src/hyprshade/cli/ls.py +++ b/src/hyprshade/cli/ls.py @@ -32,6 +32,20 @@ def is_in_shader_paths(self): raise RuntimeError("ShaderWithMeta.is_in_shader_paths is not set") return is_in_shader_paths + @classmethod + def get_shaders_list(cls) -> list[ShaderWithMeta]: + current = cls._current() + shaders = list(map(cls, ls_dirs(Shader.dirs.all()))) + if current: + i = cls._bisect(shaders, current) + if shaders[i] == current: + shaders[i]._is_current = True + shaders[i]._is_in_shader_paths = True + else: + current._is_in_shader_paths = False + shaders.insert(i, current) + return shaders + @classmethod def _current(cls) -> ShaderWithMeta | None: shader = super().current() @@ -52,20 +66,6 @@ def _bisect(a: list[ShaderWithMeta], x: ShaderWithMeta) -> int: return i return first_index - @staticmethod - def get_shaders_list() -> list[ShaderWithMeta]: - current = ShaderWithMeta._current() - shaders = list(map(ShaderWithMeta, ls_dirs(Shader.dirs.all()))) - if current: - i = ShaderWithMeta._bisect(shaders, current) - if shaders[i] == current: - shaders[i]._is_current = True - shaders[i]._is_in_shader_paths = True - else: - current._is_in_shader_paths = False - shaders.insert(i, current) - return shaders - @click.command(short_help="List available screen shaders") @click.option("-l", "--long", is_flag=True, help="Long listing format") diff --git a/src/hyprshade/cli/utils.py b/src/hyprshade/cli/utils.py index 227fa60..0088acf 100644 --- a/src/hyprshade/cli/utils.py +++ b/src/hyprshade/cli/utils.py @@ -77,12 +77,6 @@ def convert( ): return Shader(value) - @staticmethod - def _shader_names() -> Iterable[str]: - return unique_justseen( - sorted(map(stripped_basename, ls_dirs(Shader.dirs.all()))) - ) - def shell_complete( self, ctx: click.Context, param: click.Parameter, incomplete: str ): @@ -93,3 +87,9 @@ def shell_complete( return click.Path().shell_complete(ctx, param, incomplete) return [CompletionItem(name) for name in ShaderParamType._shader_names()] + + @staticmethod + def _shader_names() -> Iterable[str]: + return unique_justseen( + sorted(map(stripped_basename, ls_dirs(Shader.dirs.all()))) + ) diff --git a/src/hyprshade/config/core.py b/src/hyprshade/config/core.py index c66f9ef..0746322 100644 --- a/src/hyprshade/config/core.py +++ b/src/hyprshade/config/core.py @@ -25,19 +25,16 @@ def __init__(self, path_: str | None = None): self._dict = Config._load(path_) self._validate() - @staticmethod - def _load(path_: str) -> ConfigDict: - with open(path_, "rb") as f: - return cast(ConfigDict, tomllib.load(f)) + def partition(self) -> tuple[list[ShaderConfig], DefaultShadeConfig | None]: + no_default, yes_default = partition( + lambda s: s.get("default", False), self._dict["shades"] + ) + rest = cast(list[ShaderConfig], list(no_default)) + default = cast(DefaultShadeConfig, nth(yes_default, 0)) - @staticmethod - def get_path() -> str | None: - candidates = [ - os.getenv("HYPRSHADE_CONFIG"), - path.join(hypr_config_home(), "hyprshade.toml"), - path.join(hyprshade_config_home(), "config.toml"), - ] - return first_true((c for c in candidates if c is not None), pred=path.isfile) + assert nth(yes_default, 0) is None, "There should be only one default shade" + + return rest, default def _validate(self) -> None: shaders = self._dict.get("shades", []) @@ -54,16 +51,19 @@ def _validate(self) -> None: f"Non-default shader '{shader['name']}' must define `start_time`", ) - def partition(self) -> tuple[list[ShaderConfig], DefaultShadeConfig | None]: - no_default, yes_default = partition( - lambda s: s.get("default", False), self._dict["shades"] - ) - rest = cast(list[ShaderConfig], list(no_default)) - default = cast(DefaultShadeConfig, nth(yes_default, 0)) - - assert nth(yes_default, 0) is None, "There should be only one default shade" + @staticmethod + def _load(path_: str) -> ConfigDict: + with open(path_, "rb") as f: + return cast(ConfigDict, tomllib.load(f)) - return rest, default + @staticmethod + def get_path() -> str | None: + candidates = [ + os.getenv("HYPRSHADE_CONFIG"), + path.join(hypr_config_home(), "hyprshade.toml"), + path.join(hyprshade_config_home(), "config.toml"), + ] + return first_true((c for c in candidates if c is not None), pred=path.isfile) class ConfigError(Exception): diff --git a/src/hyprshade/config/schedule.py b/src/hyprshade/config/schedule.py index b91f88a..65ae336 100644 --- a/src/hyprshade/config/schedule.py +++ b/src/hyprshade/config/schedule.py @@ -47,6 +47,12 @@ def __init__(self, config: Config): else None ) + @classmethod + def from_config(cls, path_: str | None = None) -> Schedule: + from .core import Config + + return cls(Config(path_)) + def scheduled_shader(self, t: time) -> Shader | None: for shader, (start_time, end_time) in self._resolved_entries(): if is_time_between(t, start_time, end_time): @@ -65,9 +71,3 @@ def _resolved_entries(self) -> Iterator[tuple[Shader, TimeRange]]: start_time = entry.start_time end_time = entry.end_time or next_entry.start_time yield entry.shader, (start_time, end_time) - - @staticmethod - def from_config(path_: str | None = None) -> Schedule: - from .core import Config - - return Schedule(Config(path_))