mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-05-29 22:18:49 +02:00
General: Unhardcode all CACHE_DIR and COLOR_COUNT usage.
This commit is contained in:
parent
a3d4b3d9f3
commit
52cd5e5f1a
@ -1,3 +1,4 @@
|
|||||||
|
"""Test script for wal api."""
|
||||||
import pywal
|
import pywal
|
||||||
|
|
||||||
|
|
||||||
@ -20,9 +21,9 @@ def main():
|
|||||||
pywal.reload_env()
|
pywal.reload_env()
|
||||||
|
|
||||||
# Export template files.
|
# Export template files.
|
||||||
pywal.export_all_templates(colors,
|
# pywal.export_all_templates(colors,
|
||||||
"path/to/templates",
|
# "path/to/templates",
|
||||||
"path/to/save/files/")
|
# "path/to/save/files/")
|
||||||
|
|
||||||
# Set the wallpaper.
|
# Set the wallpaper.
|
||||||
pywal.set_wallpaper(image)
|
pywal.set_wallpaper(image)
|
||||||
|
@ -3,7 +3,7 @@ wal - Generate and change colorschemes on the fly.
|
|||||||
Created by Dylan Araps.
|
Created by Dylan Araps.
|
||||||
"""
|
"""
|
||||||
# flake8: noqa: F401
|
# flake8: noqa: F401
|
||||||
from pywal.settings import __version__
|
from pywal.wal import __version__
|
||||||
from pywal.wal import create_palette
|
from pywal.wal import create_palette
|
||||||
from pywal.wal import export_all_templates
|
from pywal.wal import export_all_templates
|
||||||
from pywal.wal import get_image
|
from pywal.wal import get_image
|
||||||
|
@ -7,7 +7,6 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR, __version__
|
|
||||||
from pywal import wal
|
from pywal import wal
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
@ -69,8 +68,8 @@ def process_args(args):
|
|||||||
|
|
||||||
# -c
|
# -c
|
||||||
if args.c:
|
if args.c:
|
||||||
shutil.rmtree(CACHE_DIR / "schemes")
|
shutil.rmtree(wal.CACHE_DIR / "schemes")
|
||||||
util.create_dir(CACHE_DIR / "schemes")
|
util.create_dir(wal.CACHE_DIR / "schemes")
|
||||||
|
|
||||||
# -r
|
# -r
|
||||||
if args.r:
|
if args.r:
|
||||||
@ -78,13 +77,13 @@ def process_args(args):
|
|||||||
|
|
||||||
# -v
|
# -v
|
||||||
if args.v:
|
if args.v:
|
||||||
print(f"wal {__version__}")
|
print(f"wal {wal.__version__}")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# -i
|
# -i
|
||||||
if args.i:
|
if args.i:
|
||||||
image_file = wal.get_image(args.i)
|
image_file = wal.get_image(args.i)
|
||||||
colors_plain = wal.create_palette(image_file, args.q)
|
colors_plain = wal.create_palette(img=image_file, quiet=args.q)
|
||||||
|
|
||||||
# -f
|
# -f
|
||||||
elif args.f:
|
elif args.f:
|
||||||
@ -107,7 +106,7 @@ def process_args(args):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main script function."""
|
"""Main script function."""
|
||||||
util.create_dir(CACHE_DIR / "schemes")
|
util.create_dir(wal.CACHE_DIR / "schemes")
|
||||||
args = get_args()
|
args = get_args()
|
||||||
process_args(args)
|
process_args(args)
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ Export colors in various formats.
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR
|
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
@ -27,16 +26,12 @@ def template(colors, input_file, output_dir):
|
|||||||
print(f"export: Exported {template_file}.")
|
print(f"export: Exported {template_file}.")
|
||||||
|
|
||||||
|
|
||||||
def export_all_templates(colors, template_dir=None, output_dir=CACHE_DIR):
|
def export_all_templates(colors, output_dir, template_dir=None):
|
||||||
"""Export all template files."""
|
"""Export all template files."""
|
||||||
# Add the template dir to module path.
|
# Add the template dir to module path.
|
||||||
template_dir = template_dir or \
|
template_dir = template_dir or \
|
||||||
os.path.join(os.path.dirname(__file__), "templates")
|
os.path.join(os.path.dirname(__file__), "templates")
|
||||||
|
|
||||||
# Convert path strings into Path types.
|
|
||||||
template_dir = util.str_to_path(template_dir)
|
|
||||||
output_dir = util.str_to_path(output_dir)
|
|
||||||
|
|
||||||
# Merge all colors (specials and normals) into one dict so we can access
|
# Merge all colors (specials and normals) into one dict so we can access
|
||||||
# their values simpler.
|
# their values simpler.
|
||||||
all_colors = {"wallpaper": colors["wallpaper"],
|
all_colors = {"wallpaper": colors["wallpaper"],
|
||||||
|
@ -5,13 +5,12 @@ import os
|
|||||||
import pathlib
|
import pathlib
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR
|
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
def get_random_image(img_dir):
|
def get_random_image(img_dir, cache_dir):
|
||||||
"""Pick a random image file from a directory."""
|
"""Pick a random image file from a directory."""
|
||||||
current_wall = CACHE_DIR / "wal"
|
current_wall = cache_dir / "wal"
|
||||||
|
|
||||||
if current_wall.is_file():
|
if current_wall.is_file():
|
||||||
current_wall = util.read_file(current_wall)
|
current_wall = util.read_file(current_wall)
|
||||||
@ -30,7 +29,7 @@ def get_random_image(img_dir):
|
|||||||
return img_dir / random.choice(images).name
|
return img_dir / random.choice(images).name
|
||||||
|
|
||||||
|
|
||||||
def get_image(img):
|
def get_image(img, cache_dir):
|
||||||
"""Validate image input."""
|
"""Validate image input."""
|
||||||
image = pathlib.Path(img)
|
image = pathlib.Path(img)
|
||||||
|
|
||||||
@ -38,7 +37,7 @@ def get_image(img):
|
|||||||
wal_img = image
|
wal_img = image
|
||||||
|
|
||||||
elif image.is_dir():
|
elif image.is_dir():
|
||||||
wal_img = get_random_image(image)
|
wal_img = get_random_image(image, cache_dir)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("error: No valid image file found.")
|
print("error: No valid image file found.")
|
||||||
|
@ -5,7 +5,7 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR, COLOR_COUNT
|
# from pywal.settings import color_count
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ def imagemagick(color_count, img):
|
|||||||
return colors.stdout.readlines()
|
return colors.stdout.readlines()
|
||||||
|
|
||||||
|
|
||||||
def gen_colors(img):
|
def gen_colors(img, color_count):
|
||||||
"""Format the output from imagemagick into a list
|
"""Format the output from imagemagick into a list
|
||||||
of hex colors."""
|
of hex colors."""
|
||||||
# Check if the user has Imagemagick installed.
|
# Check if the user has Imagemagick installed.
|
||||||
@ -28,18 +28,18 @@ def gen_colors(img):
|
|||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Generate initial scheme.
|
# Generate initial scheme.
|
||||||
raw_colors = imagemagick(COLOR_COUNT, img)
|
raw_colors = imagemagick(color_count, img)
|
||||||
|
|
||||||
# If imagemagick finds less than 16 colors, use a larger source number
|
# If imagemagick finds less than 16 colors, use a larger source number
|
||||||
# of colors.
|
# of colors.
|
||||||
index = 0
|
index = 0
|
||||||
while len(raw_colors) - 1 < COLOR_COUNT:
|
while len(raw_colors) - 1 < color_count:
|
||||||
index += 1
|
index += 1
|
||||||
raw_colors = imagemagick(COLOR_COUNT + index, img)
|
raw_colors = imagemagick(color_count + index, img)
|
||||||
|
|
||||||
print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
|
print("colors: Imagemagick couldn't generate a", color_count,
|
||||||
"color palette, trying a larger palette size",
|
"color palette, trying a larger palette size",
|
||||||
COLOR_COUNT + index)
|
color_count + index)
|
||||||
|
|
||||||
if index > 20:
|
if index > 20:
|
||||||
print("colors: Imagemagick couldn't generate a suitable scheme",
|
print("colors: Imagemagick couldn't generate a suitable scheme",
|
||||||
@ -53,14 +53,14 @@ def gen_colors(img):
|
|||||||
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]
|
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]
|
||||||
|
|
||||||
|
|
||||||
def get_colors(img, quiet=False):
|
def get_colors(img, cache_dir, color_count, quiet):
|
||||||
"""Get the colorscheme."""
|
"""Get the colorscheme."""
|
||||||
# Cache the wallpaper name.
|
# Cache the wallpaper name.
|
||||||
util.save_file(img, CACHE_DIR / "wal")
|
util.save_file(img, cache_dir / "wal")
|
||||||
|
|
||||||
# Cache the sequences file.
|
# Cache the sequences file.
|
||||||
# _home_dylan_img_jpg.json
|
# _home_dylan_img_jpg.json
|
||||||
cache_file = CACHE_DIR / "schemes" / \
|
cache_file = cache_dir / "schemes" / \
|
||||||
img.replace("/", "_").replace(".", "_")
|
img.replace("/", "_").replace(".", "_")
|
||||||
cache_file = cache_file.with_suffix(".json")
|
cache_file = cache_file.with_suffix(".json")
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ def get_colors(img, quiet=False):
|
|||||||
util.disown("notify-send", "wal: Generating a colorscheme...")
|
util.disown("notify-send", "wal: Generating a colorscheme...")
|
||||||
|
|
||||||
# Generate the colors.
|
# Generate the colors.
|
||||||
colors = gen_colors(img)
|
colors = gen_colors(img, color_count)
|
||||||
colors = sort_colors(img, colors)
|
colors = sort_colors(img, colors)
|
||||||
|
|
||||||
# Cache the colorscheme.
|
# Cache the colorscheme.
|
||||||
|
@ -4,14 +4,13 @@ Reload programs.
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR
|
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
def reload_xrdb():
|
def reload_xrdb(cache_dir):
|
||||||
"""Merge the colors into the X db so new terminals use them."""
|
"""Merge the colors into the X db so new terminals use them."""
|
||||||
if shutil.which("xrdb"):
|
if shutil.which("xrdb"):
|
||||||
subprocess.call(["xrdb", "-merge", CACHE_DIR / "colors.Xresources"])
|
subprocess.call(["xrdb", "-merge", cache_dir / "colors.Xresources"])
|
||||||
|
|
||||||
|
|
||||||
def reload_i3():
|
def reload_i3():
|
||||||
@ -26,9 +25,9 @@ def reload_polybar():
|
|||||||
util.disown("pkill", "-USR1", "polybar")
|
util.disown("pkill", "-USR1", "polybar")
|
||||||
|
|
||||||
|
|
||||||
def reload_env():
|
def reload_env(cache_dir):
|
||||||
"""Reload environment."""
|
"""Reload environment."""
|
||||||
reload_xrdb()
|
reload_xrdb(cache_dir)
|
||||||
reload_i3()
|
reload_i3()
|
||||||
reload_polybar()
|
reload_polybar()
|
||||||
print("reload: Reloaded environment.")
|
print("reload: Reloaded environment.")
|
||||||
|
@ -4,7 +4,7 @@ Send sequences to all open terminals.
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from pywal.settings import CACHE_DIR
|
# from pywal.settings import CACHE_DIR
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ def set_color(index, color):
|
|||||||
return f"\033]4;{index};{color}\007"
|
return f"\033]4;{index};{color}\007"
|
||||||
|
|
||||||
|
|
||||||
def send_sequences(colors, vte):
|
def send_sequences(colors, vte, cache_dir):
|
||||||
"""Send colors to all open terminals."""
|
"""Send colors to all open terminals."""
|
||||||
# Colors 0-15.
|
# Colors 0-15.
|
||||||
sequences = [set_color(num, color)
|
sequences = [set_color(num, color)
|
||||||
@ -44,7 +44,7 @@ def send_sequences(colors, vte):
|
|||||||
# Get the list of terminals.
|
# Get the list of terminals.
|
||||||
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
|
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
|
||||||
if len(term) < 4]
|
if len(term) < 4]
|
||||||
terminals.append(CACHE_DIR / "sequences")
|
terminals.append(cache_dir / "sequences")
|
||||||
|
|
||||||
# Send the sequences to all open terminals.
|
# Send the sequences to all open terminals.
|
||||||
# pylint: disable=W0106
|
# pylint: disable=W0106
|
||||||
@ -53,9 +53,9 @@ def send_sequences(colors, vte):
|
|||||||
print("colors: Set terminal colors")
|
print("colors: Set terminal colors")
|
||||||
|
|
||||||
|
|
||||||
def reload_colors(vte, sequence_file=None):
|
def reload_colors(vte, cache_dir):
|
||||||
"""Reload the current scheme."""
|
"""Reload the current scheme."""
|
||||||
sequence_file = sequence_file or CACHE_DIR / "sequences"
|
sequence_file = cache_dir / "sequences"
|
||||||
|
|
||||||
if sequence_file.is_file():
|
if sequence_file.is_file():
|
||||||
sequences = "".join(util.read_file(sequence_file))
|
sequences = "".join(util.read_file(sequence_file))
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
"""
|
|
||||||
Global Constants.
|
|
||||||
"""
|
|
||||||
import pathlib
|
|
||||||
|
|
||||||
|
|
||||||
__version__ = "0.4.0"
|
|
||||||
|
|
||||||
|
|
||||||
COLOR_COUNT = 16
|
|
||||||
CACHE_DIR = pathlib.Path.home() / ".cache/wal/"
|
|
@ -98,8 +98,3 @@ def disown(*cmd):
|
|||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
preexec_fn=os.setpgrp)
|
preexec_fn=os.setpgrp)
|
||||||
|
|
||||||
|
|
||||||
def str_to_path(str_path):
|
|
||||||
"""Convert a string to a Path type."""
|
|
||||||
return pathlib.Path(str_path)
|
|
||||||
|
34
pywal/wal.py
34
pywal/wal.py
@ -2,6 +2,8 @@
|
|||||||
wal - Generate and change colorschemes on the fly.
|
wal - Generate and change colorschemes on the fly.
|
||||||
Created by Dylan Araps.
|
Created by Dylan Araps.
|
||||||
"""
|
"""
|
||||||
|
import pathlib
|
||||||
|
|
||||||
from pywal import export
|
from pywal import export
|
||||||
from pywal import image
|
from pywal import image
|
||||||
from pywal import magic
|
from pywal import magic
|
||||||
@ -10,29 +12,37 @@ from pywal import sequences
|
|||||||
from pywal import wallpaper
|
from pywal import wallpaper
|
||||||
|
|
||||||
|
|
||||||
def get_image(img):
|
__version__ = "0.4.0"
|
||||||
|
|
||||||
|
|
||||||
|
COLOR_COUNT = 16
|
||||||
|
CACHE_DIR = pathlib.Path.home() / ".cache/wal/"
|
||||||
|
|
||||||
|
|
||||||
|
def get_image(img, cache_dir=CACHE_DIR):
|
||||||
"""Validate image input."""
|
"""Validate image input."""
|
||||||
return image.get_image(img)
|
return image.get_image(img, cache_dir)
|
||||||
|
|
||||||
|
|
||||||
def create_palette(img, quiet=False):
|
def create_palette(img, cache_dir=CACHE_DIR,
|
||||||
|
color_count=COLOR_COUNT, quiet=False):
|
||||||
"""Create a palette and return it as a dict."""
|
"""Create a palette and return it as a dict."""
|
||||||
return magic.get_colors(img, quiet)
|
return magic.get_colors(img, cache_dir, color_count, quiet)
|
||||||
|
|
||||||
|
|
||||||
def send_sequences(colors, vte):
|
def send_sequences(colors, vte, cache_dir=CACHE_DIR):
|
||||||
"""Send the sequences."""
|
"""Send the sequences."""
|
||||||
sequences.send_sequences(colors, vte)
|
sequences.send_sequences(colors, vte, cache_dir)
|
||||||
|
|
||||||
|
|
||||||
def reload_env():
|
def reload_env(cache_dir=CACHE_DIR):
|
||||||
"""Reload the environment."""
|
"""Reload the environment."""
|
||||||
reload.reload_env()
|
reload.reload_env(cache_dir)
|
||||||
|
|
||||||
|
|
||||||
def export_all_templates(colors, template_dir=None, export_dir=None):
|
def export_all_templates(colors, output_dir=CACHE_DIR, template_dir=None):
|
||||||
"""Export all templates."""
|
"""Export all templates."""
|
||||||
export.export_all_templates(colors, template_dir, export_dir)
|
export.export_all_templates(colors, output_dir, template_dir)
|
||||||
|
|
||||||
|
|
||||||
def set_wallpaper(img):
|
def set_wallpaper(img):
|
||||||
@ -40,6 +50,6 @@ def set_wallpaper(img):
|
|||||||
wallpaper.set_wallpaper(img)
|
wallpaper.set_wallpaper(img)
|
||||||
|
|
||||||
|
|
||||||
def reload_colors(vte, sequence_file=None):
|
def reload_colors(vte, cache_dir=CACHE_DIR):
|
||||||
"""Reload the colors."""
|
"""Reload the colors."""
|
||||||
sequences.reload_colors(vte, sequence_file)
|
sequences.reload_colors(vte, cache_dir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user