mirror of
https://github.com/dylanaraps/pywal.git
synced 2024-11-29 03:13:47 +01:00
General: Generalise functions.
This commit is contained in:
parent
3d1627f3c7
commit
b47dcb1e0d
@ -69,11 +69,11 @@ def process_args(args):
|
|||||||
# -c
|
# -c
|
||||||
if args.c:
|
if args.c:
|
||||||
shutil.rmtree(s.CACHE_DIR / "schemes")
|
shutil.rmtree(s.CACHE_DIR / "schemes")
|
||||||
util.create_cache_dir()
|
util.create_dir(s.CACHE_DIR / "schemes")
|
||||||
|
|
||||||
# -r
|
# -r
|
||||||
if args.r:
|
if args.r:
|
||||||
gen_colors.reload_colors(args.t)
|
export.reload_colors(args.t)
|
||||||
|
|
||||||
# -v
|
# -v
|
||||||
if args.v:
|
if args.v:
|
||||||
@ -99,7 +99,7 @@ def process_args(args):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main script function."""
|
"""Main script function."""
|
||||||
util.create_cache_dir()
|
util.create_dir(s.CACHE_DIR / "schemes")
|
||||||
args = get_args()
|
args = get_args()
|
||||||
process_args(args)
|
process_args(args)
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Export colors in various formats.
|
Export colors in various formats.
|
||||||
"""
|
"""
|
||||||
|
import pathlib
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -15,6 +17,35 @@ def save_colors(colors, export_file, message):
|
|||||||
print(f"export: exported {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):
|
def export_rofi(colors):
|
||||||
"""Append rofi colors to the x_colors list."""
|
"""Append rofi colors to the x_colors list."""
|
||||||
s.ColorType.xrdb.append(f"rofi.color-window: {colors[0]}, "
|
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]}")
|
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):
|
def export_colors(colors):
|
||||||
"""Export colors in various formats."""
|
"""Export colors in various formats."""
|
||||||
save_colors(s.ColorType.plain, "colors", "plain hex colors")
|
save_colors(s.ColorType.plain, "colors", "plain hex colors")
|
||||||
|
@ -103,7 +103,8 @@ def get_colors(img):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
print("colors: Generating a colorscheme...")
|
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.
|
# Generate the colors.
|
||||||
colors = gen_colors(img)
|
colors = gen_colors(img)
|
||||||
@ -113,7 +114,8 @@ def get_colors(img):
|
|||||||
util.save_file("\n".join(colors), cache_file)
|
util.save_file("\n".join(colors), cache_file)
|
||||||
|
|
||||||
print("colors: Generated colorscheme")
|
print("colors: Generated colorscheme")
|
||||||
util.notify("wal: Generation complete.")
|
if s.Args.notify:
|
||||||
|
util.disown("notify-send", "wal: Generation complete.")
|
||||||
|
|
||||||
return colors
|
return colors
|
||||||
|
|
||||||
@ -138,20 +140,3 @@ def sort_colors(colors):
|
|||||||
sorted_colors.append(colors[14])
|
sorted_colors.append(colors[14])
|
||||||
sorted_colors.append(colors[15])
|
sorted_colors.append(colors[15])
|
||||||
return sorted_colors
|
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)
|
|
||||||
|
@ -4,9 +4,6 @@ Misc helper functions.
|
|||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import shutil
|
|
||||||
|
|
||||||
from pywal import settings as s
|
|
||||||
|
|
||||||
|
|
||||||
def read_file(input_file):
|
def read_file(input_file):
|
||||||
@ -20,9 +17,9 @@ def save_file(colors, export_file):
|
|||||||
file.write(colors)
|
file.write(colors)
|
||||||
|
|
||||||
|
|
||||||
def create_cache_dir():
|
def create_dir(directory):
|
||||||
"""Alias to create the cache dir."""
|
"""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):
|
def hex_to_rgb(color):
|
||||||
@ -36,15 +33,6 @@ def fix_escape(string):
|
|||||||
return bytes(string, "utf-8").decode("unicode_escape")
|
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):
|
def disown(*cmd):
|
||||||
"""Call a system command in the background,
|
"""Call a system command in the background,
|
||||||
disown it and hide it's output."""
|
disown it and hide it's output."""
|
||||||
|
Loading…
Reference in New Issue
Block a user