pywal/wal.py

232 lines
6.2 KiB
Python
Raw Normal View History

2017-06-17 03:14:32 +02:00
"""
wal - Generate and change colorschemes on the fly.
Created by Dylan Araps
"""
2017-06-16 16:50:13 +02:00
import argparse
2017-06-17 03:14:32 +02:00
import re
import random
2017-06-17 03:48:02 +02:00
import glob
2017-06-17 04:31:53 +02:00
import shutil
import subprocess
from subprocess import call
2017-06-17 03:14:32 +02:00
2017-06-16 16:59:55 +02:00
import os
2017-06-17 03:14:32 +02:00
from os.path import expanduser
2017-06-16 17:48:14 +02:00
import pathlib
2017-06-16 16:59:55 +02:00
from pathlib import Path
2017-06-17 03:14:32 +02:00
2017-06-16 16:59:55 +02:00
# Internal variables.
2017-06-17 03:14:32 +02:00
CACHE_DIR = expanduser("~") + "/.cache/wal"
COLOR_COUNT = 16
OS = os.uname
2017-06-16 16:50:13 +02:00
def get_args():
2017-06-17 03:14:32 +02:00
"""Get the script arguments."""
description = "wal - Generate colorschemes on the fly"
arg = argparse.ArgumentParser(description=description)
2017-06-16 16:50:27 +02:00
2017-06-17 03:14:32 +02:00
# Add the args.
arg.add_argument('-a', metavar='0-100', type=int,
help='Set terminal background transparency. \
*Only works in URxvt*')
2017-06-16 16:50:27 +02:00
2017-06-17 03:14:32 +02:00
arg.add_argument('-c', action='store_true',
help='Delete all cached colorschemes.')
2017-06-16 16:50:13 +02:00
2017-06-17 03:14:32 +02:00
arg.add_argument('-f', metavar='"/path/to/colors"',
help='Load colors directly from a colorscheme file.')
2017-06-16 16:50:13 +02:00
2017-06-17 03:14:32 +02:00
arg.add_argument('-i', metavar='"/path/to/img.jpg"', required=True,
help='Which image or directory to use.')
arg.add_argument('-n', action='store_true',
help='Skip setting the wallpaper.')
arg.add_argument('-o', metavar='script_name',
help='External script to run after "wal".')
arg.add_argument('-q', action='store_true',
help='Quiet mode, don\'t print anything.')
arg.add_argument('-r', action='store_true',
help='Reload current colorscheme.')
arg.add_argument('-t', action='store_true',
help='Fix artifacts in VTE Terminals. \
(Termite, xfce4-terminal)')
arg.add_argument('-x', action='store_true',
help='Use extended 16-color palette.')
return arg.parse_args()
def get_image(img):
"""Validate image input."""
2017-06-16 16:59:55 +02:00
image = Path(img)
if image.is_file():
2017-06-17 03:14:32 +02:00
return image
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
elif image.is_dir():
rand = random.choice(os.listdir(image))
rand_img = Path(str(image) + "/" + rand)
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
if rand_img.is_file():
return rand_img
2017-06-16 17:48:14 +02:00
def magic(color_count, img):
"""Call Imagemagick to generate a scheme."""
colors = subprocess.Popen(["convert", img, "+dither", "-colors",
str(color_count), "-unique-colors", "txt:-"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return colors.stdout
2017-06-17 03:23:19 +02:00
def gen_colors(img):
"""Generate a color palette using imagemagick."""
2017-06-17 03:14:32 +02:00
colors = []
# Generate initial scheme.
magic_output = magic(COLOR_COUNT, img).readlines()
# If imagemagick finds less than 16 colors, use a larger source number
# of colors.
index = 0
while len(magic_output) - 1 <= 15:
index += 1
magic_output = magic(COLOR_COUNT + index, img).readlines()
print("colors: Imagemagick couldn't generate a", COLOR_COUNT,
"color palette, trying a larger palette size",
COLOR_COUNT + index)
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
# Create a list of hex colors.
for color in magic_output:
2017-06-17 03:14:32 +02:00
hex_color = re.search('#.{6}', str(color))
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
if hex_color:
colors.append(hex_color.group(0))
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
# Remove the first element, which isn't a color.
del colors[0]
2017-06-16 17:48:14 +02:00
2017-06-17 03:23:19 +02:00
return colors
def get_colors(img):
"""Generate a colorscheme using imagemagick."""
# Cache file.
cache_file = Path(CACHE_DIR + "/schemes/" + img.replace('/', '_'))
2017-06-17 03:48:02 +02:00
if cache_file.is_file():
with open(cache_file) as file:
colors = file.readlines()
colors = [x.strip() for x in colors]
else:
2017-06-17 03:23:19 +02:00
# Cache the wallpaper name.
wal = open(CACHE_DIR + "/wal", 'w')
wal.write(img + "\n")
wal.close()
# Generate the colors.
colors = gen_colors(img)
# Cache the colorscheme.
scheme = open(cache_file, 'w')
for color in colors:
scheme.write(color + "\n")
scheme.close()
2017-06-16 16:59:55 +02:00
2017-06-17 03:48:02 +02:00
return colors
def set_color(index, color):
"""Build the escape sequence we need for each color."""
return "\\033]4;" + str(index) + ";" + color + "\\007"
def send_sequences(colors):
"""Send colors to all open terminals."""
sequences = set_color(1, colors[9])
sequences += set_color(2, colors[10])
sequences += set_color(3, colors[11])
sequences += set_color(4, colors[12])
sequences += set_color(5, colors[13])
sequences += set_color(6, colors[14])
sequences += set_color(7, colors[15])
sequences += set_color(9, colors[9])
sequences += set_color(10, colors[10])
sequences += set_color(11, colors[11])
sequences += set_color(12, colors[12])
sequences += set_color(13, colors[13])
sequences += set_color(14, colors[14])
sequences += set_color(15, colors[15])
# Set a blank color that isn't affected by bold highlighting.
sequences += set_color(66, colors[0])
# Decode the string.
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
# Send the sequences to all open terminals.
2017-06-17 03:48:02 +02:00
for term in glob.glob("/dev/pts/[0-9]*"):
term_file = open(term, 'w')
term_file.write(sequences)
term_file.close()
2017-06-16 16:59:55 +02:00
2017-06-17 04:31:53 +02:00
def set_wallpaper(img):
"""Set the wallpaper."""
if shutil.which("feh"):
call(["feh", "--bg-fill", img])
elif shutil.which("nitrogen"):
call(["nitrogen", "--set-zoom-fill", img])
elif shutil.which("bgs"):
call(["bgs", img])
elif shutil.which("hsetroot"):
call(["hsetroot", "-fill", img])
elif shutil.which("habak"):
call(["habak", "-mS", img])
elif OS == "Darwin":
call(["osascript", "-e", "'tell application \"Finder\" to set \
desktop picture to POSIX file \"'\"", img, "\""])
else:
call(["gsettings", "set", "org.gnome.desktop.background",
"picture-uri", img])
print("wallpaper: Set the new wallpaper")
return 0
2017-06-16 16:50:13 +02:00
def main():
2017-06-17 03:14:32 +02:00
"""Main script function."""
2017-06-16 16:50:13 +02:00
args = get_args()
2017-06-17 03:14:32 +02:00
image = str(get_image(args.i))
2017-06-17 03:23:19 +02:00
# Create colorscheme dir.
pathlib.Path(CACHE_DIR + "/schemes").mkdir(parents=True, exist_ok=True)
2017-06-17 03:48:02 +02:00
colors = get_colors(image)
send_sequences(colors)
2017-06-17 04:31:53 +02:00
set_wallpaper(image)
2017-06-17 03:48:02 +02:00
2017-06-16 16:50:13 +02:00
return 0
main()