Improve SWWW scripts

This commit is contained in:
Michael Bradley 2024-05-12 20:39:28 -04:00
parent abd24916e8
commit 5e90cde04a
3 changed files with 18 additions and 7 deletions

View file

@ -1,7 +1,8 @@
"""Basic functions for interacting with swww""" """Basic functions for interacting with swww"""
from json import loads
from pathlib import Path from pathlib import Path
from random import sample from random import sample
from shlex import split
from socket import gethostname from socket import gethostname
from subprocess import run from subprocess import run
@ -19,6 +20,14 @@ IMMEDIATE_TRANSITION_ARGS = [
] ]
def _run_cmd(arg0: str, *args: str) -> str:
return run([arg0, *args] if args else split(arg0), capture_output=True).stdout.decode("utf-8")
def monitors() -> list[str]:
return [monitor["name"] for monitor in loads(_run_cmd("hyprctl monitors -j"))]
def run_swww(monitor: str, image: Path, extra_args: list[str] | None = None) -> None: def run_swww(monitor: str, image: Path, extra_args: list[str] | None = None) -> None:
run(["swww", "img", "-o", monitor, image, *(extra_args or [])]) run(["swww", "img", "-o", monitor, image, *(extra_args or [])])
@ -27,6 +36,8 @@ def get_wallpapers() -> list[Path]:
return list(Path("/usr/share/backgrounds/Wallpapers/").glob("*")) return list(Path("/usr/share/backgrounds/Wallpapers/").glob("*"))
def sample_wallpapers(num: int = len(MONITORS)) -> list[Path]: def sample_wallpapers(displays: list[str] | None = None) -> "zip[tuple[str, Path]]":
return sample(get_wallpapers(), num) if displays is None:
displays = monitors()
return zip(displays, sample(get_wallpapers(), len(displays)))

View file

@ -1,9 +1,9 @@
#!/bin/env python #!/bin/env python
"""Selects new backgrounds for the monitors""" """Selects new backgrounds for the monitors"""
from lib.swww import ANGLE_TRANSITION_ARGS, MONITORS, run_swww, sample_wallpapers from lib.swww import ANGLE_TRANSITION_ARGS, run_swww, sample_wallpapers
for monitor, wallpaper in zip(MONITORS, sample_wallpapers()): for monitor, wallpaper in sample_wallpapers():
run_swww(monitor, wallpaper, ANGLE_TRANSITION_ARGS) run_swww(monitor, wallpaper, ANGLE_TRANSITION_ARGS)

View file

@ -1,9 +1,9 @@
#!/bin/env python #!/bin/env python
"""Sets a new background for the monitors immediately""" """Sets a new background for the monitors immediately"""
from lib.swww import IMMEDIATE_TRANSITION_ARGS, MONITORS, run_swww, sample_wallpapers from lib.swww import IMMEDIATE_TRANSITION_ARGS, run_swww, sample_wallpapers
for monitor, wallpaper in zip(MONITORS, sample_wallpapers()): for monitor, wallpaper in sample_wallpapers():
run_swww(monitor, wallpaper, IMMEDIATE_TRANSITION_ARGS) run_swww(monitor, wallpaper, IMMEDIATE_TRANSITION_ARGS)