mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-01-10 07:58:12 +01:00
general: Remove comments that just repeat what the code does.
This commit is contained in:
parent
97de3110de
commit
12f9211cd4
@ -16,7 +16,6 @@ def get_args():
|
|||||||
description = "wal - Generate colorschemes on the fly"
|
description = "wal - Generate colorschemes on the fly"
|
||||||
arg = argparse.ArgumentParser(description=description)
|
arg = argparse.ArgumentParser(description=description)
|
||||||
|
|
||||||
# Add the args.
|
|
||||||
arg.add_argument("-c", action="store_true",
|
arg.add_argument("-c", action="store_true",
|
||||||
help="Delete all cached colorschemes.")
|
help="Delete all cached colorschemes.")
|
||||||
|
|
||||||
@ -51,7 +50,6 @@ def get_args():
|
|||||||
|
|
||||||
def process_args(args):
|
def process_args(args):
|
||||||
"""Process args."""
|
"""Process args."""
|
||||||
# If no args were passed.
|
|
||||||
if not len(sys.argv) > 1:
|
if not len(sys.argv) > 1:
|
||||||
print("error: wal needs to be given arguments to run.\n"
|
print("error: wal needs to be given arguments to run.\n"
|
||||||
" Refer to \"wal -h\" for more info.")
|
" Refer to \"wal -h\" for more info.")
|
||||||
@ -62,34 +60,27 @@ def process_args(args):
|
|||||||
" Refer to \"wal -h\" for more info.")
|
" Refer to \"wal -h\" for more info.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# -q
|
|
||||||
if args.q:
|
if args.q:
|
||||||
sys.stdout = sys.stderr = open(os.devnull, "w")
|
sys.stdout = sys.stderr = open(os.devnull, "w")
|
||||||
|
|
||||||
# -c
|
|
||||||
if args.c:
|
if args.c:
|
||||||
shutil.rmtree(wal.CACHE_DIR / "schemes")
|
shutil.rmtree(wal.CACHE_DIR / "schemes")
|
||||||
util.create_dir(wal.CACHE_DIR / "schemes")
|
util.create_dir(wal.CACHE_DIR / "schemes")
|
||||||
|
|
||||||
# -r
|
|
||||||
if args.r:
|
if args.r:
|
||||||
wal.reload_colors(args.t)
|
wal.reload_colors(args.t)
|
||||||
|
|
||||||
# -v
|
|
||||||
if args.v:
|
if args.v:
|
||||||
print(f"wal {wal.__version__}")
|
print(f"wal {wal.__version__}")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# -i
|
|
||||||
if args.i:
|
if args.i:
|
||||||
image_file = wal.get_image(args.i)
|
image_file = wal.get_image(args.i)
|
||||||
colors_plain = wal.create_palette(img=image_file, quiet=args.q)
|
colors_plain = wal.create_palette(img=image_file, quiet=args.q)
|
||||||
|
|
||||||
# -f
|
|
||||||
elif args.f:
|
elif args.f:
|
||||||
colors_plain = util.read_file_json(args.f)
|
colors_plain = util.read_file_json(args.f)
|
||||||
|
|
||||||
# -i or -f
|
|
||||||
if args.i or args.f:
|
if args.i or args.f:
|
||||||
wal.send_sequences(colors_plain, args.t)
|
wal.send_sequences(colors_plain, args.t)
|
||||||
|
|
||||||
@ -99,7 +90,6 @@ def process_args(args):
|
|||||||
wal.export_all_templates(colors_plain)
|
wal.export_all_templates(colors_plain)
|
||||||
wal.reload_env()
|
wal.reload_env()
|
||||||
|
|
||||||
# -o
|
|
||||||
if args.o:
|
if args.o:
|
||||||
util.disown(args.o)
|
util.disown(args.o)
|
||||||
|
|
||||||
|
@ -9,37 +9,21 @@ from pywal import util
|
|||||||
def template(colors, input_file, output_dir):
|
def template(colors, input_file, output_dir):
|
||||||
"""Read template file, substitute markers and
|
"""Read template file, substitute markers and
|
||||||
save the file elsewhere."""
|
save the file elsewhere."""
|
||||||
# Import the template.
|
template_data = util.read_file_raw(input_file)
|
||||||
with open(input_file) as file:
|
|
||||||
template_data = file.readlines()
|
|
||||||
|
|
||||||
# Format the markers.
|
|
||||||
template_data = "".join(template_data).format(**colors)
|
template_data = "".join(template_data).format(**colors)
|
||||||
|
template_name = os.path.basename(input_file)
|
||||||
# Get the template name.
|
util.save_file(template_data, output_dir / template_name)
|
||||||
template_file = os.path.basename(input_file)
|
print(f"export: Exported {template_name}.")
|
||||||
|
|
||||||
# Export the template.
|
|
||||||
output_file = output_dir / template_file
|
|
||||||
util.save_file(template_data, output_file)
|
|
||||||
|
|
||||||
print(f"export: Exported {template_file}.")
|
|
||||||
|
|
||||||
|
|
||||||
def export_all_templates(colors, output_dir, template_dir=None):
|
def export_all_templates(colors, output_dir, template_dir=None):
|
||||||
"""Export all template files."""
|
"""Export all template files."""
|
||||||
# Add the template dir to module path.
|
|
||||||
template_dir = template_dir or \
|
template_dir = template_dir or \
|
||||||
os.path.join(os.path.dirname(__file__), "templates")
|
os.path.join(os.path.dirname(__file__), "templates")
|
||||||
|
|
||||||
# Merge all colors (specials and normals) into one dict so we can access
|
|
||||||
# their values simpler.
|
|
||||||
all_colors = {"wallpaper": colors["wallpaper"],
|
all_colors = {"wallpaper": colors["wallpaper"],
|
||||||
**colors["special"],
|
**colors["special"],
|
||||||
**colors["colors"]}
|
**colors["colors"]}
|
||||||
|
|
||||||
# Turn all those colors into util.Color instances for accessing the
|
|
||||||
# .hex and .rgb formats
|
|
||||||
all_colors = {k: util.Color(v) for k, v in all_colors.items()}
|
all_colors = {k: util.Color(v) for k, v in all_colors.items()}
|
||||||
|
|
||||||
# pylint: disable=W0106
|
# pylint: disable=W0106
|
||||||
|
@ -16,12 +16,10 @@ def get_random_image(img_dir, cache_dir):
|
|||||||
current_wall = util.read_file(current_wall)
|
current_wall = util.read_file(current_wall)
|
||||||
current_wall = os.path.basename(current_wall[0])
|
current_wall = os.path.basename(current_wall[0])
|
||||||
|
|
||||||
# Add all images to a list excluding the current wallpaper.
|
|
||||||
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
|
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
|
||||||
images = [img for img in os.scandir(img_dir)
|
images = [img for img in os.scandir(img_dir)
|
||||||
if img.name.endswith(file_types) and img.name != current_wall]
|
if img.name.endswith(file_types) and img.name != current_wall]
|
||||||
|
|
||||||
# If no images are found, use the current wallpaper.
|
|
||||||
if not images:
|
if not images:
|
||||||
print("image: No new images found (nothing to do), exiting...")
|
print("image: No new images found (nothing to do), exiting...")
|
||||||
quit(1)
|
quit(1)
|
||||||
|
@ -5,7 +5,6 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
# from pywal.settings import color_count
|
|
||||||
from pywal import util
|
from pywal import util
|
||||||
|
|
||||||
|
|
||||||
@ -21,17 +20,13 @@ def imagemagick(color_count, img):
|
|||||||
def gen_colors(img, color_count):
|
def gen_colors(img, color_count):
|
||||||
"""Format the output from imagemagick into a list
|
"""Format the output from imagemagick into a list
|
||||||
of hex colors."""
|
of hex colors."""
|
||||||
# Check if the user has Imagemagick installed.
|
|
||||||
if not shutil.which("convert"):
|
if not shutil.which("convert"):
|
||||||
print("error: imagemagick not found, exiting...\n"
|
print("error: imagemagick not found, exiting...\n"
|
||||||
"error: wal requires imagemagick to function.")
|
"error: wal requires imagemagick to function.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Generate initial scheme.
|
|
||||||
raw_colors = imagemagick(color_count, img)
|
raw_colors = imagemagick(color_count, img)
|
||||||
|
|
||||||
# If imagemagick finds less than 16 colors, use a larger source number
|
|
||||||
# of colors.
|
|
||||||
index = 0
|
index = 0
|
||||||
while len(raw_colors) - 1 < color_count:
|
while len(raw_colors) - 1 < color_count:
|
||||||
index += 1
|
index += 1
|
||||||
@ -46,19 +41,16 @@ def gen_colors(img, color_count):
|
|||||||
"for the image. Exiting...")
|
"for the image. Exiting...")
|
||||||
quit(1)
|
quit(1)
|
||||||
|
|
||||||
# Remove the first element, which isn't a color.
|
# Remove the first element because it isn't a color code.
|
||||||
del raw_colors[0]
|
del raw_colors[0]
|
||||||
|
|
||||||
# Create a list of hex colors.
|
|
||||||
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]
|
return [re.search("#.{6}", str(col)).group(0) for col in raw_colors]
|
||||||
|
|
||||||
|
|
||||||
def get_colors(img, cache_dir, color_count, quiet):
|
def get_colors(img, cache_dir, color_count, quiet):
|
||||||
"""Get the colorscheme."""
|
"""Get the colorscheme."""
|
||||||
# Cache the wallpaper name.
|
|
||||||
util.save_file(img, cache_dir / "wal")
|
util.save_file(img, cache_dir / "wal")
|
||||||
|
|
||||||
# Cache the sequences file.
|
|
||||||
# _home_dylan_img_jpg.json
|
# _home_dylan_img_jpg.json
|
||||||
cache_file = cache_dir / "schemes" / \
|
cache_file = cache_dir / "schemes" / \
|
||||||
img.replace("/", "_").replace(".", "_")
|
img.replace("/", "_").replace(".", "_")
|
||||||
@ -71,11 +63,9 @@ def get_colors(img, cache_dir, color_count, quiet):
|
|||||||
else:
|
else:
|
||||||
util.msg("wal: Generating a colorscheme...", quiet)
|
util.msg("wal: Generating a colorscheme...", quiet)
|
||||||
|
|
||||||
# Generate the colors.
|
|
||||||
colors = gen_colors(img, color_count)
|
colors = gen_colors(img, color_count)
|
||||||
colors = sort_colors(img, colors)
|
colors = sort_colors(img, colors)
|
||||||
|
|
||||||
# Cache the colorscheme.
|
|
||||||
util.save_file_json(colors, cache_file)
|
util.save_file_json(colors, cache_file)
|
||||||
util.msg("wal: Generation complete.", quiet)
|
util.msg("wal: Generation complete.", quiet)
|
||||||
|
|
||||||
@ -87,24 +77,18 @@ def sort_colors(img, colors):
|
|||||||
we will later save in json format."""
|
we will later save in json format."""
|
||||||
raw_colors = colors[:1] + colors[9:] + colors[8:]
|
raw_colors = colors[:1] + colors[9:] + colors[8:]
|
||||||
|
|
||||||
# Wallpaper.
|
|
||||||
colors = {"wallpaper": img}
|
colors = {"wallpaper": img}
|
||||||
|
|
||||||
# Special colors.
|
|
||||||
colors_special = {}
|
colors_special = {}
|
||||||
colors_special.update({"background": raw_colors[0]})
|
colors_special.update({"background": raw_colors[0]})
|
||||||
colors_special.update({"foreground": raw_colors[15]})
|
colors_special.update({"foreground": raw_colors[15]})
|
||||||
colors_special.update({"cursor": raw_colors[15]})
|
colors_special.update({"cursor": raw_colors[15]})
|
||||||
|
|
||||||
# Colors 0-15.
|
|
||||||
colors_hex = {}
|
colors_hex = {}
|
||||||
[colors_hex.update({f"color{index}": color}) # pylint: disable=W0106
|
[colors_hex.update({f"color{index}": color}) # pylint: disable=W0106
|
||||||
for index, color in enumerate(raw_colors)]
|
for index, color in enumerate(raw_colors)]
|
||||||
|
|
||||||
# Color 8.
|
|
||||||
colors_hex["color8"] = util.set_grey(raw_colors)
|
colors_hex["color8"] = util.set_grey(raw_colors)
|
||||||
|
|
||||||
# Add the colors to a dict.
|
|
||||||
colors["special"] = colors_special
|
colors["special"] = colors_special
|
||||||
colors["colors"] = colors_hex
|
colors["colors"] = colors_hex
|
||||||
|
|
||||||
|
@ -40,7 +40,6 @@ def send_sequences(colors, vte, cache_dir):
|
|||||||
if not vte:
|
if not vte:
|
||||||
sequences.append(set_special(708, colors["special"]["background"]))
|
sequences.append(set_special(708, colors["special"]["background"]))
|
||||||
|
|
||||||
# Get the list of terminals.
|
|
||||||
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
|
terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/")
|
||||||
if len(term) < 4]
|
if len(term) < 4]
|
||||||
terminals.append(cache_dir / "sequences")
|
terminals.append(cache_dir / "sequences")
|
||||||
@ -48,7 +47,6 @@ def send_sequences(colors, vte, cache_dir):
|
|||||||
# Send the sequences to all open terminals.
|
# Send the sequences to all open terminals.
|
||||||
# pylint: disable=W0106
|
# pylint: disable=W0106
|
||||||
[util.save_file("".join(sequences), term) for term in terminals]
|
[util.save_file("".join(sequences), term) for term in terminals]
|
||||||
|
|
||||||
print("colors: Set terminal colors")
|
print("colors: Set terminal colors")
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,24 +44,31 @@ def set_grey(colors):
|
|||||||
|
|
||||||
|
|
||||||
def read_file(input_file):
|
def read_file(input_file):
|
||||||
"""Read data from a file."""
|
"""Read data from a file and trim newlines."""
|
||||||
with open(input_file) as file:
|
with open(input_file, "r") as file:
|
||||||
data = file.read().splitlines()
|
data = file.read().splitlines()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def read_file_json(input_file):
|
def read_file_json(input_file):
|
||||||
"""Read data from a json file."""
|
"""Read data from a json file."""
|
||||||
with open(input_file) as json_file:
|
with open(input_file, "r") as json_file:
|
||||||
data = json.load(json_file)
|
data = json.load(json_file)
|
||||||
|
|
||||||
# If wallpaper is unset, set it to "None"
|
|
||||||
if "wallpaper" not in data:
|
if "wallpaper" not in data:
|
||||||
data["wallpaper"] = "None"
|
data["wallpaper"] = "None"
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def read_file_raw(input_file):
|
||||||
|
"""Read data from a file as is, don't strip
|
||||||
|
newlines or other special characters.."""
|
||||||
|
with open(input_file, "r") as file:
|
||||||
|
data = file.readlines()
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def save_file(data, export_file):
|
def save_file(data, export_file):
|
||||||
"""Write data to a file."""
|
"""Write data to a file."""
|
||||||
with open(export_file, "w") as file:
|
with open(export_file, "w") as file:
|
||||||
|
Loading…
Reference in New Issue
Block a user