mirror of
https://github.com/dylanaraps/pywal.git
synced 2024-11-25 17:33:09 +01:00
images: Added --iterative option. Closes 227
This commit is contained in:
parent
ff2ad9bd35
commit
d14821694a
@ -48,6 +48,11 @@ def get_args():
|
|||||||
Use 'wal --theme' to list builtin themes.",
|
Use 'wal --theme' to list builtin themes.",
|
||||||
const="list_themes", nargs="?")
|
const="list_themes", nargs="?")
|
||||||
|
|
||||||
|
arg.add_argument("--iterative", action="store_true",
|
||||||
|
help="When pywal is given a directory as input and this "
|
||||||
|
"flag is used: Go through the images in order "
|
||||||
|
"instead of shuffled.")
|
||||||
|
|
||||||
arg.add_argument("-c", action="store_true",
|
arg.add_argument("-c", action="store_true",
|
||||||
help="Delete all cached colorschemes.")
|
help="Delete all cached colorschemes.")
|
||||||
|
|
||||||
@ -141,7 +146,7 @@ def parse_args(parser):
|
|||||||
shutil.rmtree(scheme_dir, ignore_errors=True)
|
shutil.rmtree(scheme_dir, ignore_errors=True)
|
||||||
|
|
||||||
if args.i:
|
if args.i:
|
||||||
image_file = image.get(args.i)
|
image_file = image.get(args.i, iterative=args.iterative)
|
||||||
colors_plain = colors.get(image_file, args.l, args.backend)
|
colors_plain = colors.get(image_file, args.l, args.backend)
|
||||||
|
|
||||||
if args.theme:
|
if args.theme:
|
||||||
|
@ -113,7 +113,12 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR):
|
|||||||
cache_file = os.path.join(*cache_name)
|
cache_file = os.path.join(*cache_name)
|
||||||
|
|
||||||
if os.path.isfile(cache_file):
|
if os.path.isfile(cache_file):
|
||||||
|
# Disable logging in theme.file().
|
||||||
|
logger = logging.getLogger()
|
||||||
|
logger.disabled = True
|
||||||
colors = theme.file(cache_file)
|
colors = theme.file(cache_file)
|
||||||
|
logger.disabled = False
|
||||||
|
|
||||||
util.Color.alpha_num = colors["alpha"]
|
util.Color.alpha_num = colors["alpha"]
|
||||||
logging.info("Found cached colorscheme.")
|
logging.info("Found cached colorscheme.")
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ Get the image file.
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .settings import CACHE_DIR
|
from .settings import CACHE_DIR
|
||||||
@ -11,15 +12,20 @@ from . import util
|
|||||||
from . import wallpaper
|
from . import wallpaper
|
||||||
|
|
||||||
|
|
||||||
def get_random_image(img_dir):
|
def get_image_dir(img_dir):
|
||||||
"""Pick a random image file from a directory."""
|
"""Get all images in a directory."""
|
||||||
current_wall = wallpaper.get()
|
current_wall = wallpaper.get()
|
||||||
current_wall = os.path.basename(current_wall)
|
current_wall = os.path.basename(current_wall)
|
||||||
|
|
||||||
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
|
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
|
||||||
|
|
||||||
images = [img for img in os.scandir(img_dir)
|
return [img.name for img in os.scandir(img_dir)
|
||||||
if img.name.lower().endswith(file_types)]
|
if img.name.lower().endswith(file_types)], current_wall
|
||||||
|
|
||||||
|
|
||||||
|
def get_random_image(img_dir):
|
||||||
|
"""Pick a random image file from a directory."""
|
||||||
|
images, current_wall = get_image_dir(img_dir)
|
||||||
|
|
||||||
if len(images) > 2 and current_wall in images:
|
if len(images) > 2 and current_wall in images:
|
||||||
images.remove(current_wall)
|
images.remove(current_wall)
|
||||||
@ -29,17 +35,41 @@ def get_random_image(img_dir):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
random.shuffle(images)
|
random.shuffle(images)
|
||||||
|
return os.path.join(img_dir, images[0])
|
||||||
return os.path.join(img_dir, images[0].name)
|
|
||||||
|
|
||||||
|
|
||||||
def get(img, cache_dir=CACHE_DIR):
|
def get_next_image(img_dir):
|
||||||
|
"""Get the next image in a dir."""
|
||||||
|
images, current_wall = get_image_dir(img_dir)
|
||||||
|
images.sort(key=lambda img: [int(x) if x.isdigit() else x
|
||||||
|
for x in re.split('([0-9]+)', img)])
|
||||||
|
|
||||||
|
try:
|
||||||
|
next_index = images.index(current_wall) + 1
|
||||||
|
|
||||||
|
except ValueError:
|
||||||
|
next_index = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
image = images[next_index]
|
||||||
|
|
||||||
|
except IndexError:
|
||||||
|
image = images[0]
|
||||||
|
|
||||||
|
return os.path.join(img_dir, image)
|
||||||
|
|
||||||
|
|
||||||
|
def get(img, cache_dir=CACHE_DIR, iterative=False):
|
||||||
"""Validate image input."""
|
"""Validate image input."""
|
||||||
if os.path.isfile(img):
|
if os.path.isfile(img):
|
||||||
wal_img = img
|
wal_img = img
|
||||||
|
|
||||||
elif os.path.isdir(img):
|
elif os.path.isdir(img):
|
||||||
wal_img = get_random_image(img)
|
if iterative:
|
||||||
|
wal_img = get_next_image(img)
|
||||||
|
|
||||||
|
else:
|
||||||
|
wal_img = get_random_image(img)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.error("No valid image file found.")
|
logging.error("No valid image file found.")
|
||||||
|
Loading…
Reference in New Issue
Block a user