mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-06-19 17:19:08 +02:00
General: Optimization
This commit is contained in:
parent
6736a954d6
commit
9355441d81
175
wal.py
175
wal.py
@ -19,8 +19,15 @@ import pathlib
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
# 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")
|
||||||
|
|
||||||
# Internal variables.
|
# Internal variables.
|
||||||
CACHE_DIR = expanduser("~") + "/.cache/wal/"
|
|
||||||
COLOR_COUNT = 16
|
COLOR_COUNT = 16
|
||||||
OS = os.uname
|
OS = os.uname
|
||||||
|
|
||||||
@ -74,7 +81,7 @@ def process_args(args):
|
|||||||
|
|
||||||
# -c
|
# -c
|
||||||
if args.c:
|
if args.c:
|
||||||
shutil.rmtree(CACHE_DIR + "schemes")
|
shutil.rmtree(SCHEME_DIR)
|
||||||
|
|
||||||
# -r
|
# -r
|
||||||
if args.r:
|
if args.r:
|
||||||
@ -88,7 +95,7 @@ def process_args(args):
|
|||||||
|
|
||||||
def reload_colors(vte):
|
def reload_colors(vte):
|
||||||
"""Reload colors."""
|
"""Reload colors."""
|
||||||
with open(CACHE_DIR + "sequences") as file:
|
with open(SEQUENCE_FILE) as file:
|
||||||
sequences = file.read()
|
sequences = file.read()
|
||||||
|
|
||||||
# If vte mode was used, remove the problem sequence.
|
# If vte mode was used, remove the problem sequence.
|
||||||
@ -123,7 +130,8 @@ def get_image(img):
|
|||||||
|
|
||||||
elif image.is_dir():
|
elif image.is_dir():
|
||||||
rand = random.choice(os.listdir(image))
|
rand = random.choice(os.listdir(image))
|
||||||
rand_img = Path(str(image) + "/" + rand)
|
rand_img = "%s/%s" % (str(image), rand)
|
||||||
|
rand_img = Path(rand_img)
|
||||||
|
|
||||||
if rand_img.is_file():
|
if rand_img.is_file():
|
||||||
return rand_img
|
return rand_img
|
||||||
@ -173,7 +181,8 @@ def gen_colors(img):
|
|||||||
def get_colors(img):
|
def get_colors(img):
|
||||||
"""Generate a colorscheme using imagemagick."""
|
"""Generate a colorscheme using imagemagick."""
|
||||||
# Cache file.
|
# Cache file.
|
||||||
cache_file = Path(CACHE_DIR + "schemes/" + img.replace('/', '_'))
|
cache_file = "%s%s" % (SCHEME_DIR, img.replace('/', '_'))
|
||||||
|
cache_file = Path(cache_file)
|
||||||
|
|
||||||
if cache_file.is_file():
|
if cache_file.is_file():
|
||||||
with open(cache_file) as file:
|
with open(cache_file) as file:
|
||||||
@ -182,8 +191,9 @@ def get_colors(img):
|
|||||||
colors = [x.strip() for x in colors]
|
colors = [x.strip() for x in colors]
|
||||||
else:
|
else:
|
||||||
# Cache the wallpaper name.
|
# Cache the wallpaper name.
|
||||||
wal = open(CACHE_DIR + "wal", 'w')
|
wal = open(WAL_FILE, 'w')
|
||||||
wal.write(img + "\n")
|
wal.write(img)
|
||||||
|
wal.write("\n")
|
||||||
wal.close()
|
wal.close()
|
||||||
|
|
||||||
# Generate the colors.
|
# Generate the colors.
|
||||||
@ -192,7 +202,8 @@ def get_colors(img):
|
|||||||
# Cache the colorscheme.
|
# Cache the colorscheme.
|
||||||
scheme = open(cache_file, 'w')
|
scheme = open(cache_file, 'w')
|
||||||
for color in colors:
|
for color in colors:
|
||||||
scheme.write(color + "\n")
|
scheme.write(color)
|
||||||
|
scheme.write("\n")
|
||||||
scheme.close()
|
scheme.close()
|
||||||
|
|
||||||
print("colors: Generated colorscheme")
|
print("colors: Generated colorscheme")
|
||||||
@ -207,12 +218,12 @@ def get_colors(img):
|
|||||||
|
|
||||||
def set_special(index, color):
|
def set_special(index, color):
|
||||||
"""Build the escape sequence for special colors."""
|
"""Build the escape sequence for special colors."""
|
||||||
return "\\033]" + str(index) + ";" + color + "\\007"
|
return "\\033]%s;%s\\007" % (str(index), color)
|
||||||
|
|
||||||
|
|
||||||
def set_color(index, color):
|
def set_color(index, color):
|
||||||
"""Build the escape sequence we need for each color."""
|
"""Build the escape sequence we need for each color."""
|
||||||
return "\\033]4;" + str(index) + ";" + color + "\\007"
|
return "\\033]4;%s;%s\\007" % (str(index), color)
|
||||||
|
|
||||||
|
|
||||||
def get_grey(color, color2):
|
def get_grey(color, color2):
|
||||||
@ -239,35 +250,39 @@ def get_grey(color, color2):
|
|||||||
|
|
||||||
def send_sequences(colors, vte):
|
def send_sequences(colors, vte):
|
||||||
"""Send colors to all open terminals."""
|
"""Send colors to all open terminals."""
|
||||||
sequences = set_special(10, colors[15])
|
seq = []
|
||||||
sequences += set_special(11, colors[0])
|
seq.append(set_special(10, colors[15]))
|
||||||
sequences += set_special(12, colors[15])
|
seq.append(set_special(11, colors[0]))
|
||||||
sequences += set_special(13, colors[15])
|
seq.append(set_special(12, colors[15]))
|
||||||
sequences += set_special(14, colors[0])
|
seq.append(set_special(13, colors[15]))
|
||||||
|
seq.append(set_special(14, colors[0]))
|
||||||
|
|
||||||
# This escape sequence doesn't work in VTE terminals.
|
# This escape sequence doesn't work in VTE terminals.
|
||||||
if not vte:
|
if not vte:
|
||||||
sequences += set_special(708, colors[0])
|
seq.append(set_special(708, colors[0]))
|
||||||
|
|
||||||
sequences += set_color(0, colors[0])
|
seq.append(set_color(0, colors[0]))
|
||||||
sequences += set_color(1, colors[9])
|
seq.append(set_color(1, colors[9]))
|
||||||
sequences += set_color(2, colors[10])
|
seq.append(set_color(2, colors[10]))
|
||||||
sequences += set_color(3, colors[11])
|
seq.append(set_color(3, colors[11]))
|
||||||
sequences += set_color(4, colors[12])
|
seq.append(set_color(4, colors[12]))
|
||||||
sequences += set_color(5, colors[13])
|
seq.append(set_color(5, colors[13]))
|
||||||
sequences += set_color(6, colors[14])
|
seq.append(set_color(6, colors[14]))
|
||||||
sequences += set_color(7, colors[15])
|
seq.append(set_color(7, colors[15]))
|
||||||
sequences += set_color(8, get_grey(colors[0], colors[7]))
|
seq.append(set_color(8, get_grey(colors[0], colors[7])))
|
||||||
sequences += set_color(9, colors[9])
|
seq.append(set_color(9, colors[9]))
|
||||||
sequences += set_color(10, colors[10])
|
seq.append(set_color(10, colors[10]))
|
||||||
sequences += set_color(11, colors[11])
|
seq.append(set_color(11, colors[11]))
|
||||||
sequences += set_color(12, colors[12])
|
seq.append(set_color(12, colors[12]))
|
||||||
sequences += set_color(13, colors[13])
|
seq.append(set_color(13, colors[13]))
|
||||||
sequences += set_color(14, colors[14])
|
seq.append(set_color(14, colors[14]))
|
||||||
sequences += set_color(15, colors[15])
|
seq.append(set_color(15, colors[15]))
|
||||||
|
|
||||||
# Set a blank color that isn't affected by bold highlighting.
|
# Set a blank color that isn't affected by bold highlighting.
|
||||||
sequences += set_color(66, colors[0])
|
seq.append(set_color(66, colors[0]))
|
||||||
|
|
||||||
|
# Create the string.
|
||||||
|
sequences = ''.join(seq)
|
||||||
|
|
||||||
# Decode the string.
|
# Decode the string.
|
||||||
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
|
sequences = bytes(sequences, "utf-8").decode("unicode_escape")
|
||||||
@ -279,7 +294,7 @@ def send_sequences(colors, vte):
|
|||||||
term_file.close()
|
term_file.close()
|
||||||
|
|
||||||
# Cache the sequences.
|
# Cache the sequences.
|
||||||
sequence_file = open(CACHE_DIR + "sequences", 'w')
|
sequence_file = open(SEQUENCE_FILE, 'w')
|
||||||
sequence_file.write(sequences)
|
sequence_file.write(sequences)
|
||||||
sequence_file.close()
|
sequence_file.close()
|
||||||
|
|
||||||
@ -329,47 +344,67 @@ def set_wallpaper(img):
|
|||||||
|
|
||||||
def export_plain(colors):
|
def export_plain(colors):
|
||||||
"""Export colors to a plain text file."""
|
"""Export colors to a plain text file."""
|
||||||
plain_file = CACHE_DIR + "colors"
|
file = open(PLAIN_FILE, 'w')
|
||||||
|
|
||||||
file = open(plain_file, 'w')
|
|
||||||
for color in colors:
|
for color in colors:
|
||||||
file.write(color + "\n")
|
file.write(color)
|
||||||
|
file.write("\n")
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
|
||||||
def export_xrdb(colors):
|
def export_xrdb(colors):
|
||||||
"""Export colors to xrdb."""
|
"""Export colors to xrdb."""
|
||||||
x_colors = "URxvt*foreground: " + colors[15] + "\n"
|
x_colors = """
|
||||||
x_colors += "XTerm*foreground: " + colors[15] + "\n"
|
URxvt*foreground: %s
|
||||||
x_colors += "URxvt*background: " + colors[0] + "\n"
|
XTerm*forefround: %s
|
||||||
x_colors += "XTerm*background: " + colors[0] + "\n"
|
URxvt*background: %s
|
||||||
x_colors += "URxvt*cursorColor: " + colors[15] + "\n"
|
XTerm*background: %s
|
||||||
x_colors += "XTerm*cursorColor: " + colors[15] + "\n"
|
URxvt*cursorColor: %s
|
||||||
x_colors += "*.color0: " + colors[0] + "\n"
|
XTerm*cursorColor: %s
|
||||||
x_colors += "*.color1: " + colors[9] + "\n"
|
*.color0: %s
|
||||||
x_colors += "*.color2: " + colors[10] + "\n"
|
*.color1: %s
|
||||||
x_colors += "*.color3: " + colors[11] + "\n"
|
*.color2: %s
|
||||||
x_colors += "*.color4: " + colors[12] + "\n"
|
*.color3: %s
|
||||||
x_colors += "*.color5: " + colors[13] + "\n"
|
*.color4: %s
|
||||||
x_colors += "*.color6: " + colors[14] + "\n"
|
*.color5: %s
|
||||||
x_colors += "*.color7: " + colors[15] + "\n"
|
*.color6: %s
|
||||||
x_colors += "*.color8: " + get_grey(colors[0], colors[7]) + "\n"
|
*.color7: %s
|
||||||
x_colors += "*.color9: " + colors[9] + "\n"
|
*.color8: %s
|
||||||
x_colors += "*.color10: " + colors[10] + "\n"
|
*.color9: %s
|
||||||
x_colors += "*.color11: " + colors[11] + "\n"
|
*.color10: %s
|
||||||
x_colors += "*.color12: " + colors[12] + "\n"
|
*.color11: %s
|
||||||
x_colors += "*.color13: " + colors[13] + "\n"
|
*.color12: %s
|
||||||
x_colors += "*.color14: " + colors[14] + "\n"
|
*.color13: %s
|
||||||
x_colors += "*.color15: " + colors[15] + "\n"
|
*.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])
|
||||||
|
|
||||||
xrdb_file = CACHE_DIR + "xcolors"
|
file = open(XRDB_FILE, 'w')
|
||||||
|
|
||||||
file = open(xrdb_file, 'w')
|
|
||||||
file.write(x_colors)
|
file.write(x_colors)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
# Merge the colors into the X db so new terminals use them.
|
# Merge the colors into the X db so new terminals use them.
|
||||||
call(["xrdb", "-merge", "<<<", xrdb_file])
|
call(["xrdb", "-merge", XRDB_FILE])
|
||||||
|
|
||||||
print("export: Exported xrdb colors.")
|
print("export: Exported xrdb colors.")
|
||||||
|
|
||||||
@ -379,21 +414,21 @@ def export_xrdb(colors):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main script function."""
|
"""Main script function."""
|
||||||
|
# Create colorscheme dir.
|
||||||
|
pathlib.Path(SCHEME_DIR).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
args = get_args()
|
args = get_args()
|
||||||
process_args(args)
|
process_args(args)
|
||||||
|
|
||||||
if args.i:
|
if args.i:
|
||||||
image = str(get_image(args.i))
|
image = str(get_image(args.i))
|
||||||
|
|
||||||
# Set the wallpaper.
|
|
||||||
set_wallpaper(image)
|
|
||||||
|
|
||||||
# Create colorscheme dir.
|
|
||||||
pathlib.Path(CACHE_DIR + "schemes").mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
# Get the colors.
|
# Get the colors.
|
||||||
colors = get_colors(image)
|
colors = get_colors(image)
|
||||||
|
|
||||||
|
# Set the wallpaper.
|
||||||
|
set_wallpaper(image)
|
||||||
|
|
||||||
# Set the colors.
|
# Set the colors.
|
||||||
send_sequences(colors, args.t)
|
send_sequences(colors, args.t)
|
||||||
export_plain(colors)
|
export_plain(colors)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user