General: Remove most globals.

This commit is contained in:
Dylan Araps 2017-06-26 23:13:51 +10:00
parent 091021a878
commit 8119a65f51
6 changed files with 154 additions and 135 deletions

View File

@ -8,7 +8,7 @@ import shutil
import sys
from pywal import globals as g
from pywal import export
from pywal import exp_colors
from pywal import gen_colors
from pywal import set_colors
from pywal import wallpaper
@ -80,15 +80,17 @@ def process_args(args):
# -i
if args.i:
image = gen_colors.get_image(args.i)
g.ColorType.plain = gen_colors.get_colors(image)
g.ColorType.plain[8] = set_colors.set_grey(g.ColorType.plain)
# Create a list of hex colors.
colors_plain = gen_colors.get_colors(image)
colors_plain[8] = set_colors.set_grey(colors_plain)
if not args.n:
wallpaper.set_wallpaper(image)
# Set the colors.
set_colors.send_sequences(g.ColorType.plain, args.t)
export.export_colors(g.ColorType.plain)
set_colors.send_sequences(colors_plain, args.t)
exp_colors.export_colors(colors_plain)
# -o
if args.o:

56
pywal/exp_colors.py Normal file
View File

@ -0,0 +1,56 @@
"""
Export colors in various formats.
"""
import shutil
import subprocess
from pywal import globals as g
from pywal import util
from pywal import format_color
def save_colors(colors, export_file, message):
"""Export colors to var format."""
colors = "".join(colors)
util.save_file(colors, g.CACHE_DIR / export_file)
print(f"export: exported {message}.")
def reload_xrdb(export_file):
"""Merge the colors into the X db so new terminals use them."""
if shutil.which("xrdb"):
subprocess.call(["xrdb", "-merge", g.CACHE_DIR / export_file])
def reload_i3():
"""Reload i3 colors."""
if shutil.which("i3-msg"):
util.disown("i3-msg", "reload")
def export_colors(colors):
"""Export colors in various formats."""
plain_colors = format_color.plain(colors)
save_colors(plain_colors, "colors", "plain hex colors")
# Shell based colors.
shell_colors = format_color.shell(colors)
save_colors(shell_colors, "colors.sh", "shell variables")
# Web based colors.
css_colors = format_color.css(colors)
save_colors(css_colors, "colors.css", "css variables")
scss_colors = format_color.scss(colors)
save_colors(scss_colors, "colors.scss", "scss variables")
# Text editor based colors.
putty_colors = format_color.putty(colors)
save_colors(putty_colors, "colors-putty.reg", "putty theme")
# X based colors.
xrdb_colors = format_color.xrdb(colors)
save_colors(xrdb_colors, "xcolors", "xrdb colors")
# i3 colors.
reload_xrdb("xcolors")
reload_i3()

View File

@ -1,71 +0,0 @@
"""
Export colors in various formats.
"""
import shutil
import subprocess
from pywal import globals as g
from pywal import util
def save_colors(colors, export_file, message):
"""Export colors to var format."""
colors = "\n".join(colors)
util.save_file(f"{colors}\n", g.CACHE_DIR / export_file)
print(f"export: exported {message}.")
def reload_xrdb(export_file):
"""Merge the colors into the X db so new terminals use them."""
if shutil.which("xrdb"):
subprocess.call(["xrdb", "-merge", g.CACHE_DIR / export_file])
def reload_i3():
"""Reload i3 colors."""
if shutil.which("i3-msg"):
util.disown("i3-msg", "reload")
def export_rofi(colors):
"""Append rofi colors to the x_colors list."""
g.ColorType.xrdb.append(f"rofi.color-window: {colors[0]}, "
f"{colors[0]}, {colors[10]}")
g.ColorType.xrdb.append(f"rofi.color-normal: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}")
g.ColorType.xrdb.append(f"rofi.color-active: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}")
g.ColorType.xrdb.append(f"rofi.color-urgent: {colors[0]}, "
f"{colors[9]}, {colors[0]}, "
f"{colors[9]}, {colors[15]}")
def export_emacs(colors):
"""Set emacs colors."""
g.ColorType.xrdb.append(f"emacs*background: {colors[0]}")
g.ColorType.xrdb.append(f"emacs*foreground: {colors[15]}")
def export_colors(colors):
"""Export colors in various formats."""
save_colors(g.ColorType.plain, "colors", "plain hex colors")
save_colors(g.ColorType.shell, "colors.sh", "shell variables")
# Web based colors.
g.ColorType.css.append("}")
save_colors(g.ColorType.css, "colors.css", "css variables")
save_colors(g.ColorType.scss, "colors.scss", "scss variables")
# Text editor based colors.
save_colors(g.ColorType.putty, "colors-putty.reg", "putty theme")
# X based colors.
export_rofi(colors)
export_emacs(colors)
save_colors(g.ColorType.xrdb, "xcolors", "xrdb colors")
# i3 colors.
reload_xrdb("xcolors")
reload_i3()

78
pywal/format_color.py Normal file
View File

@ -0,0 +1,78 @@
"""
Convert colors to various formats.
"""
from pywal import util
def plain(colors):
"""Convert colors to plain hex."""
return [f"{color}\n" for color in colors]
def shell(colors):
"""Convert colors to shell variables."""
return [f"color{index}='{color}'\n"
for index, color in enumerate(colors)]
def css(colors):
"""Convert colors to css variables."""
css_colors = [":root {\n"]
css_colors.extend([f"\t--color{index}: {color};\n"
for index, color in enumerate(colors)])
css_colors.append("}\n")
return css_colors
def scss(colors):
"""Convert colors to scss variables."""
return [f"$color{index}: {color};\n"
for index, color in enumerate(colors)]
def putty(colors):
"""Convert colors to putty theme."""
rgb = util.hex_to_rgb
putty_colors = [
"Windows Registry Editor Version 5.00\n\n",
"[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n",
]
putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n"
for index, color in enumerate(colors)])
return putty_colors
def xrdb(colors):
"""Convert colors to xrdb format."""
x_colors = []
x_colors.append(f"URxvt*foreground: {colors[15]}\n")
x_colors.append(f"XTerm*foreground: {colors[15]}\n")
x_colors.append(f"URxvt*background: {colors[0]}\n")
x_colors.append(f"XTerm*background: {colors[0]}\n")
x_colors.append(f"URxvt*cursorColor: {colors[15]}\n")
x_colors.append(f"XTerm*cursorColor: {colors[15]}\n")
# Colors 0-15
x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n"
for index, color in enumerate(colors)])
x_colors.append(f"*.color66: {colors[0]}\n*color66: {colors[0]}\n")
# Rofi colors.
x_colors.append(f"rofi.color-window: {colors[0]}, "
f"{colors[0]}, {colors[10]}\n")
x_colors.append(f"rofi.color-normal: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}\n")
x_colors.append(f"rofi.color-active: {colors[0]}, "
f"{colors[15]}, {colors[0]}, "
f"{colors[10]}, {colors[0]}\n")
x_colors.append(f"rofi.color-urgent: {colors[0]}, "
f"{colors[9]}, {colors[0]}, "
f"{colors[9]}, {colors[15]}\n")
# Emacs colors.
x_colors.append(f"emacs*background: {colors[0]}\n")
x_colors.append(f"emacs*foreground: {colors[15]}\n")
return x_colors

View File

@ -12,21 +12,6 @@ COLOR_COUNT = 16
CACHE_DIR = pathlib.Path.home() / ".cache/wal/"
# pylint: disable=too-few-public-methods
class ColorType(object):
"""Store colors in various formats."""
plain = []
xrdb = []
sequences = []
shell = []
scss = []
css = [":root {"]
putty = [
"Windows Registry Editor Version 5.00",
"[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]",
]
# pylint: disable=too-few-public-methods
class Args(object):
"""Store args."""

View File

@ -11,37 +11,12 @@ from pywal import util
def set_special(index, color):
"""Build the escape sequence for special colors."""
g.ColorType.sequences.append(f"\\033]{index};{color}\\007")
if index == 10:
g.ColorType.xrdb.append(f"URxvt*foreground: {color}")
g.ColorType.xrdb.append(f"XTerm*foreground: {color}")
elif index == 11:
g.ColorType.xrdb.append(f"URxvt*background: {color}")
g.ColorType.xrdb.append(f"XTerm*background: {color}")
elif index == 12:
g.ColorType.xrdb.append(f"URxvt*cursorColor: {color}")
g.ColorType.xrdb.append(f"XTerm*cursorColor: {color}")
elif index == 66:
g.ColorType.xrdb.append(f"*.color{index}: {color}")
g.ColorType.xrdb.append(f"*color{index}: {color}")
g.ColorType.sequences.append(f"\\033]4;{index};{color}\\007")
return f"\033]{index};{color}\007"
def set_color(index, color):
"""Build the escape sequence we need for each color."""
g.ColorType.xrdb.append(f"*.color{index}: {color}")
g.ColorType.xrdb.append(f"*color{index}: {color}")
g.ColorType.sequences.append(f"\\033]4;{index};{color}\\007")
g.ColorType.shell.append(f"color{index}='{color}'")
g.ColorType.css.append(f"\t--color{index}: {color};")
g.ColorType.scss.append(f"$color{index}: {color};")
rgb = util.hex_to_rgb(color)
g.ColorType.putty.append(f"\"Colour{index}\"=\"{rgb}\"")
return f"\033]4;{index};{color}\007"
def set_grey(colors):
@ -62,25 +37,19 @@ def set_grey(colors):
def send_sequences(colors, vte):
"""Send colors to all open terminals."""
set_special(10, colors[15])
set_special(11, colors[0])
set_special(12, colors[15])
set_special(13, colors[15])
set_special(14, colors[0])
sequences = [set_color(num, color) for num, color in enumerate(colors)]
sequences.append(set_special(10, colors[15]))
sequences.append(set_special(11, colors[0]))
sequences.append(set_special(12, colors[15]))
sequences.append(set_special(13, colors[15]))
sequences.append(set_special(14, colors[0]))
# Set a blank color that isn"t affected by bold highlighting.
sequences.append(set_special(66, colors[0]))
# This escape sequence doesn"t work in VTE terminals.
if not vte:
set_special(708, colors[0])
# Create the sequences.
# pylint: disable=W0106
[set_color(num, color) for num, color in enumerate(colors)]
# Set a blank color that isn"t affected by bold highlighting.
set_special(66, colors[0])
# Make the terminal interpret escape sequences.
sequences = util.fix_escape("".join(g.ColorType.sequences))
sequences.append(set_special(708, colors[0]))
# Get a list of terminals.
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
@ -89,7 +58,7 @@ def send_sequences(colors, vte):
# Send the sequences to all open terminals.
# pylint: disable=W0106
[util.save_file(sequences, term) for term in terminals]
[util.save_file("".join(sequences), term) for term in terminals]
print("colors: Set terminal colors")