General: Generalise functions.

This commit is contained in:
Dylan Araps 2017-06-26 12:25:58 +10:00
parent 3d1627f3c7
commit b47dcb1e0d
4 changed files with 40 additions and 48 deletions

View File

@ -69,11 +69,11 @@ def process_args(args):
# -c
if args.c:
shutil.rmtree(s.CACHE_DIR / "schemes")
util.create_cache_dir()
util.create_dir(s.CACHE_DIR / "schemes")
# -r
if args.r:
gen_colors.reload_colors(args.t)
export.reload_colors(args.t)
# -v
if args.v:
@ -99,7 +99,7 @@ def process_args(args):
def main():
"""Main script function."""
util.create_cache_dir()
util.create_dir(s.CACHE_DIR / "schemes")
args = get_args()
process_args(args)

View File

@ -1,6 +1,8 @@
"""
Export colors in various formats.
"""
import pathlib
import re
import shutil
import subprocess
@ -15,6 +17,35 @@ def save_colors(colors, export_file, message):
print(f"export: exported {message}.")
def reload_colors(vte):
"""Reload colors."""
sequence_file = pathlib.Path(s.CACHE_DIR / "sequences")
if sequence_file.is_file():
sequences = "".join(util.read_file(sequence_file))
# If vte mode was used, remove the problem sequence.
if vte:
sequences = re.sub(r"\]708;\#.{6}", "", sequences)
# Make the terminal interpret escape sequences.
print(util.fix_escape(sequences), end="")
exit(0)
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", s.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."""
s.ColorType.xrdb.append(f"rofi.color-window: {colors[0]}, "
@ -36,18 +67,6 @@ def export_emacs(colors):
s.ColorType.xrdb.append(f"emacs*foreground: {colors[15]}")
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", s.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."""
save_colors(s.ColorType.plain, "colors", "plain hex colors")

View File

@ -103,7 +103,8 @@ def get_colors(img):
else:
print("colors: Generating a colorscheme...")
util.notify("wal: Generating a colorscheme...")
if s.Args.notify:
util.disown("notify-send", "wal: Generating a colorscheme...")
# Generate the colors.
colors = gen_colors(img)
@ -113,7 +114,8 @@ def get_colors(img):
util.save_file("\n".join(colors), cache_file)
print("colors: Generated colorscheme")
util.notify("wal: Generation complete.")
if s.Args.notify:
util.disown("notify-send", "wal: Generation complete.")
return colors
@ -138,20 +140,3 @@ def sort_colors(colors):
sorted_colors.append(colors[14])
sorted_colors.append(colors[15])
return sorted_colors
def reload_colors(vte):
"""Reload colors."""
sequence_file = pathlib.Path(CACHE_DIR / "sequences")
if sequence_file.is_file():
sequences = "".join(util.read_file(sequence_file))
# If vte mode was used, remove the problem sequence.
if vte:
sequences = re.sub(r"\]708;\#.{6}", "", sequences)
# Make the terminal interpret escape sequences.
print(util.fix_escape(sequences), end="")
exit(0)

View File

@ -4,9 +4,6 @@ Misc helper functions.
import os
import pathlib
import subprocess
import shutil
from pywal import settings as s
def read_file(input_file):
@ -20,9 +17,9 @@ def save_file(colors, export_file):
file.write(colors)
def create_cache_dir():
def create_dir(directory):
"""Alias to create the cache dir."""
pathlib.Path(s.CACHE_DIR / "schemes").mkdir(parents=True, exist_ok=True)
pathlib.Path(directory).mkdir(parents=True, exist_ok=True)
def hex_to_rgb(color):
@ -36,15 +33,6 @@ def fix_escape(string):
return bytes(string, "utf-8").decode("unicode_escape")
def notify(msg):
"""Send arguements to notify-send."""
if shutil.which("notify-send") and s.Args.notify:
subprocess.Popen(["notify-send", msg],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
preexec_fn=os.setpgrp)
def disown(*cmd):
"""Call a system command in the background,
disown it and hide it's output."""