pywal/wal.py

428 lines
10 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 10:16:10 +02:00
from subprocess import Popen
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
2017-06-19 05:08:22 +02:00
# wal files.
CACHE_DIR = "%s%s" % (expanduser("~"), "/.cache/wal/")
SCHEME_DIR = "%s%s" % (CACHE_DIR, "schemes/")
SEQUENCE_FILE = "%s%s" % (CACHE_DIR, "sequences")
WAL_FILE = "%s%s" % (CACHE_DIR, "wal")
PLAIN_FILE = "%s%s" % (CACHE_DIR, "colors")
XRDB_FILE = "%s%s" % (CACHE_DIR, "xcolors")
2017-06-16 16:59:55 +02:00
# Internal variables.
2017-06-17 03:14:32 +02:00
COLOR_COUNT = 16
OS = os.uname
2017-06-16 16:50:13 +02:00
# ARGS {{{
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
# 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 09:45:03 +02:00
arg.add_argument('-i', metavar='"/path/to/img.jpg"',
2017-06-17 03:14:32 +02:00
help='Which image or directory to use.')
# arg.add_argument('-n', action='store_true',
# help='Skip setting the wallpaper.')
2017-06-17 03:14:32 +02:00
# arg.add_argument('-o', metavar='script_name',
# help='External script to run after "wal".')
2017-06-17 03:14:32 +02:00
# arg.add_argument('-q', action='store_true',
# help='Quiet mode, don\'t print anything.')
2017-06-17 03:14:32 +02:00
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.')
2017-06-17 03:14:32 +02:00
return arg.parse_args()
def process_args(args):
"""Process the arguments."""
# -c
if args.c:
2017-06-19 05:08:22 +02:00
shutil.rmtree(SCHEME_DIR)
2017-06-19 05:33:56 +02:00
quit()
# -r
if args.r:
reload_colors(args.t)
# }}}
# RELOAD COLORS {{{
def reload_colors(vte):
2017-06-17 09:45:03 +02:00
"""Reload colors."""
2017-06-19 05:08:22 +02:00
with open(SEQUENCE_FILE) as file:
2017-06-17 09:45:03 +02:00
sequences = file.read()
# If vte mode was used, remove the problem sequence.
if vte:
sequences = re.sub(r'\]708;\#.{6}', '', sequences)
2017-06-17 09:45:03 +02:00
# Decode the string.
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
print(sequences, end='')
quit()
# }}}
# COLORSCHEME GENERATION {{{
2017-06-17 03:14:32 +02:00
def get_image(img):
"""Validate image input."""
2017-06-16 16:59:55 +02:00
image = Path(img)
2017-06-19 04:17:57 +02:00
# Check if the user has Imagemagick installed.
if not shutil.which("convert"):
print("error: imagemagick not found, exiting...")
print("error: wal requires imagemagick to function.")
exit(1)
2017-06-16 16:59:55 +02:00
if image.is_file():
2017-06-19 08:15:13 +02:00
wal_img = 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))
2017-06-19 05:08:22 +02:00
rand_img = "%s/%s" % (str(image), rand)
rand_img = Path(rand_img)
2017-06-16 17:48:14 +02:00
2017-06-17 03:14:32 +02:00
if rand_img.is_file():
2017-06-19 08:15:13 +02:00
wal_img = rand_img
print("image: Using image", wal_img)
return wal_img
2017-06-16 17:48:14 +02:00
def magic(color_count, img):
"""Call Imagemagick to generate a scheme."""
2017-06-17 10:16:10 +02:00
colors = Popen(["convert", img, "+dither", "-colors",
str(color_count), "-unique-colors", "txt:-"],
2017-06-19 06:09:09 +02:00
stdout=subprocess.PIPE)
2017-06-19 06:09:09 +02:00
return colors.stdout.readlines()
2017-06-17 03:23:19 +02:00
def gen_colors(img):
"""Generate a color palette using imagemagick."""
# Generate initial scheme.
2017-06-19 06:09:09 +02:00
magic_output = magic(COLOR_COUNT, img)
# If imagemagick finds less than 16 colors, use a larger source number
# of colors.
index = 0
while len(magic_output) - 1 <= 15:
index += 1
2017-06-19 06:09:09 +02:00
magic_output = magic(COLOR_COUNT + index, img)
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.
2017-06-19 06:09:09 +02:00
colors = [re.search('#.{6}', str(col)).group(0) for col in magic_output]
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.
2017-06-19 05:08:22 +02:00
cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
cache_file = Path(cache_file)
2017-06-17 03:23:19 +02:00
2017-06-19 05:33:56 +02:00
# Cache the wallpaper name.
with open(WAL_FILE, 'w') as file:
file.write("%s\n" % (img))
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
# Generate the colors.
colors = gen_colors(img)
# Cache the colorscheme.
2017-06-19 05:33:56 +02:00
with open(cache_file, 'w') as file:
file.write("\n".join(colors))
2017-06-16 16:59:55 +02:00
2017-06-17 08:56:12 +02:00
print("colors: Generated colorscheme")
2017-06-17 03:48:02 +02:00
return colors
# }}}
# SEND SEQUENCES {{{
2017-06-17 08:56:12 +02:00
def set_special(index, color):
"""Build the escape sequence for special colors."""
2017-06-19 05:08:22 +02:00
return "\\033]%s;%s\\007" % (str(index), color)
2017-06-17 08:56:12 +02:00
2017-06-17 03:48:02 +02:00
def set_color(index, color):
"""Build the escape sequence we need for each color."""
2017-06-19 05:08:22 +02:00
return "\\033]4;%s;%s\\007" % (str(index), color)
2017-06-17 03:48:02 +02:00
2017-06-17 09:45:03 +02:00
def get_grey(color, color2):
"""Set a grey color based on brightness of color0"""
brightness = int(color[1])
if 0 <= brightness <= 1:
return "#666666"
elif brightness == 2:
return "#757575"
elif 3 <= brightness <= 4:
return "#999999"
elif brightness == 5:
return "#8a8a8a"
elif 6 <= brightness <= 9:
return "#a1a1a1"
return color2
2017-06-17 08:56:12 +02:00
def send_sequences(colors, vte):
2017-06-17 03:48:02 +02:00
"""Send colors to all open terminals."""
2017-06-19 05:08:22 +02:00
seq = []
seq.append(set_special(10, colors[15]))
seq.append(set_special(11, colors[0]))
seq.append(set_special(12, colors[15]))
seq.append(set_special(13, colors[15]))
seq.append(set_special(14, colors[0]))
2017-06-17 08:56:12 +02:00
# This escape sequence doesn't work in VTE terminals.
if not vte:
2017-06-19 05:08:22 +02:00
seq.append(set_special(708, colors[0]))
seq.append(set_color(0, colors[0]))
seq.append(set_color(1, colors[9]))
seq.append(set_color(2, colors[10]))
seq.append(set_color(3, colors[11]))
seq.append(set_color(4, colors[12]))
seq.append(set_color(5, colors[13]))
seq.append(set_color(6, colors[14]))
seq.append(set_color(7, colors[15]))
seq.append(set_color(8, get_grey(colors[0], colors[7])))
seq.append(set_color(9, colors[9]))
seq.append(set_color(10, colors[10]))
seq.append(set_color(11, colors[11]))
seq.append(set_color(12, colors[12]))
seq.append(set_color(13, colors[13]))
seq.append(set_color(14, colors[14]))
seq.append(set_color(15, colors[15]))
2017-06-17 03:48:02 +02:00
# Set a blank color that isn't affected by bold highlighting.
2017-06-19 05:08:22 +02:00
seq.append(set_color(66, colors[0]))
# Create the string.
sequences = ''.join(seq)
2017-06-17 03:48:02 +02:00
# 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]*"):
2017-06-19 05:33:56 +02:00
with open(term, 'w') as file:
file.write(sequences)
2017-06-17 03:48:02 +02:00
2017-06-17 10:16:10 +02:00
# Cache the sequences.
2017-06-19 05:33:56 +02:00
with open(SEQUENCE_FILE, 'w') as file:
file.write(sequences)
2017-06-17 10:16:10 +02:00
2017-06-17 08:56:12 +02:00
print("colors: Set terminal colors")
2017-06-16 16:59:55 +02:00
# }}}
# WALLPAPER SETTING {{{
2017-06-17 04:31:53 +02:00
def set_wallpaper(img):
"""Set the wallpaper."""
if shutil.which("feh"):
2017-06-17 10:16:10 +02:00
Popen(["feh", "--bg-fill", img])
2017-06-17 04:31:53 +02:00
elif shutil.which("nitrogen"):
2017-06-17 10:16:10 +02:00
Popen(["nitrogen", "--set-zoom-fill", img])
2017-06-17 04:31:53 +02:00
elif shutil.which("bgs"):
2017-06-17 10:16:10 +02:00
Popen(["bgs", img])
2017-06-17 04:31:53 +02:00
elif shutil.which("hsetroot"):
2017-06-17 10:16:10 +02:00
Popen(["hsetroot", "-fill", img])
2017-06-17 04:31:53 +02:00
elif shutil.which("habak"):
2017-06-17 10:16:10 +02:00
Popen(["habak", "-mS", img])
2017-06-17 04:31:53 +02:00
elif OS == "Darwin":
2017-06-17 10:16:10 +02:00
Popen(["osascript", "-e", "'tell application \"Finder\" to set \
2017-06-19 04:24:39 +02:00
desktop picture to POSIX file\'" + img + "\'"])
2017-06-17 04:31:53 +02:00
else:
2017-06-17 10:16:10 +02:00
Popen(["gsettings", "set", "org.gnome.desktop.background",
"picture-uri", img])
2017-06-17 04:31:53 +02:00
print("wallpaper: Set the new wallpaper")
return 0
# }}}
# EXPORT COLORS {{{
def export_plain(colors):
"""Export colors to a plain text file."""
2017-06-19 05:33:56 +02:00
with open(PLAIN_FILE, 'w') as file:
file.write('\n'.join(colors))
2017-06-17 09:45:03 +02:00
def export_xrdb(colors):
"""Export colors to xrdb."""
2017-06-19 05:08:22 +02:00
x_colors = """
URxvt*foreground: %s
XTerm*forefround: %s
URxvt*background: %s
XTerm*background: %s
URxvt*cursorColor: %s
XTerm*cursorColor: %s
*.color0: %s
*.color1: %s
*.color2: %s
*.color3: %s
*.color4: %s
*.color5: %s
*.color6: %s
*.color7: %s
*.color8: %s
*.color9: %s
*.color10: %s
*.color11: %s
*.color12: %s
*.color13: %s
*.color14: %s
*.color15: %s
""" % (colors[15],
colors[15],
colors[0],
colors[0],
colors[15],
colors[15],
colors[0],
colors[9],
colors[10],
colors[11],
colors[12],
colors[13],
colors[14],
colors[15],
get_grey(colors[0], colors[7]),
colors[9],
colors[10],
colors[11],
colors[12],
colors[13],
colors[14],
colors[15])
2017-06-19 05:33:56 +02:00
# Write the colors to the file.
with open(XRDB_FILE, 'w') as file:
file.write(x_colors)
2017-06-17 09:45:03 +02:00
# Merge the colors into the X db so new terminals use them.
2017-06-19 05:08:22 +02:00
call(["xrdb", "-merge", XRDB_FILE])
2017-06-17 09:45:03 +02:00
print("export: Exported xrdb colors.")
# }}}
2017-06-16 16:50:13 +02:00
def main():
2017-06-17 03:14:32 +02:00
"""Main script function."""
2017-06-19 05:08:22 +02:00
# Create colorscheme dir.
pathlib.Path(SCHEME_DIR).mkdir(parents=True, exist_ok=True)
2017-06-16 16:50:13 +02:00
args = get_args()
process_args(args)
2017-06-17 09:45:03 +02:00
if args.i:
image = str(get_image(args.i))
2017-06-17 03:23:19 +02:00
# Get the colors.
colors = get_colors(image)
2017-06-17 09:45:03 +02:00
2017-06-19 05:08:22 +02:00
# Set the wallpaper.
set_wallpaper(image)
# Set the colors.
send_sequences(colors, args.t)
export_plain(colors)
export_xrdb(colors)
2017-06-17 03:48:02 +02:00
2017-06-16 16:50:13 +02:00
return 0
main()