Cleanup: Remove duplicate code and cleanup format strings

This commit is contained in:
Dylan Araps 2017-06-22 11:37:32 +10:00
parent 104f731239
commit c159d98f62

113
wal
View File

@ -29,10 +29,10 @@ class ColorType(object):
sequences = [] sequences = []
shell = [] shell = []
scss = [] scss = []
css = [":root {\n"] css = [":root {"]
putty = [ putty = [
"Windows Registry Editor Version 5.00\n", "Windows Registry Editor Version 5.00",
"[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n", "[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]",
] ]
@ -83,8 +83,7 @@ def process_args(args):
# -q # -q
if args.q: if args.q:
sys.stdout = open("/dev/null", "w") sys.stdout = sys.stderr = open("/dev/null", "w")
sys.stderr = open("/dev/null", "w")
# -c # -c
if args.c: if args.c:
@ -97,26 +96,24 @@ def process_args(args):
# -v # -v
if args.v: if args.v:
print("wal %s" % (__version__)) print(f"wal {__version__}")
exit(0) exit(0)
# -i # -i
if args.i: if args.i:
image = str(get_image(args.i)) image = str(get_image(args.i))
ColorType.plain = get_colors(image)
colors = get_colors(image)
if not args.n: if not args.n:
set_wallpaper(image) set_wallpaper(image)
# Set the colors. # Set the colors.
send_sequences(colors, args.t) send_sequences(ColorType.plain, args.t)
export_colors(colors) export_colors(ColorType.plain)
# -o # -o
if args.o: if args.o:
subprocess.Popen(["nohup", args.o], subprocess.Popen(["nohup", args.o],
stdout=open("/dev/null", "w"),
stderr=open("/dev/null", "w"), stderr=open("/dev/null", "w"),
preexec_fn=os.setpgrp) preexec_fn=os.setpgrp)
@ -205,14 +202,13 @@ 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 the wallpaper name.
save_file(img, CACHE_DIR / "wal")
# Cache the sequences file.
cache_file = CACHE_DIR / "schemes" / img.replace("/", "_") cache_file = CACHE_DIR / "schemes" / img.replace("/", "_")
cache_file = pathlib.Path(cache_file) cache_file = pathlib.Path(cache_file)
# Cache the wallpaper name.
with open(CACHE_DIR / "wal", "w") as file:
file.write("%s\n" % (img))
if cache_file.is_file(): if cache_file.is_file():
colors = read_file(cache_file) colors = read_file(cache_file)
@ -224,8 +220,7 @@ def get_colors(img):
colors = sort_colors(colors) colors = sort_colors(colors)
# Cache the colorscheme. # Cache the colorscheme.
with open(cache_file, "w") as file: save_file("\n".join(colors), cache_file)
file.write("\n".join(colors))
print("colors: Generated colorscheme") print("colors: Generated colorscheme")
return colors return colors
@ -261,33 +256,32 @@ def sort_colors(colors):
def set_special(index, color): def set_special(index, color):
"""Build the escape sequence for special colors.""" """Build the escape sequence for special colors."""
ColorType.sequences.append("\\033]%s;%s\\007" % (str(index), color)) ColorType.sequences.append(f"\\033]{index};{color}\\007")
if index == 10: if index == 10:
ColorType.xrdb.append("URxvt*foreground: %s\n" % (color)) ColorType.xrdb.append(f"URxvt*foreground: {color}")
ColorType.xrdb.append("XTerm*foreground: %s\n" % (color)) ColorType.xrdb.append(f"XTerm*foreground: {color}")
elif index == 11: elif index == 11:
ColorType.xrdb.append("URxvt*background: %s\n" % (color)) ColorType.xrdb.append(f"URxvt*background: {color}")
ColorType.xrdb.append("XTerm*background: %s\n" % (color)) ColorType.xrdb.append(f"XTerm*background: {color}")
elif index == 12: elif index == 12:
ColorType.xrdb.append("URxvt*cursorColor: %s\n" % (color)) ColorType.xrdb.append(f"URxvt*cursorColor: {color}")
ColorType.xrdb.append("XTerm*cursorColor: %s\n" % (color)) ColorType.xrdb.append(f"XTerm*cursorColor: {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."""
index = str(index) ColorType.xrdb.append(f"*.color{index}: {color}")
ColorType.plain.append("%s\n" % (color)) ColorType.xrdb.append(f"*color{index}: {color}")
ColorType.xrdb.append("*.color%s: %s\n" % (index, color)) ColorType.sequences.append(f"\\033]4;{index};{color}\\007")
ColorType.xrdb.append("*color%s: %s\n" % (index, color)) ColorType.shell.append(f"color{index}='{color}'")
ColorType.sequences.append("\\033]4;%s;%s\\007" % (index, color)) ColorType.css.append(f"\t--color{index}: {color};")
ColorType.shell.append("color%s='%s'\n" % (index, color)) ColorType.scss.append(f"$color{index}: {color};")
ColorType.css.append("\t--color%s: %s;\n" % (index, color))
ColorType.scss.append("$color%s: %s;\n" % (index, color)) rgb = hex_to_rgb(color)
ColorType.putty.append("\"Colour%s\"=\"%s\"\n" ColorType.putty.append(f"\"Colour{index}\"=\"{rgb}\"")
% (index, hex_to_rgb(color)))
def set_grey(colors): def set_grey(colors):
@ -325,9 +319,9 @@ def send_sequences(colors, vte):
# Set a blank color that isn"t affected by bold highlighting. # Set a blank color that isn"t affected by bold highlighting.
set_color(66, colors[0]) set_color(66, colors[0])
# Decode the string. # Make the terminal interpret escape sequences.
sequences = "".join(ColorType.sequences) sequences = "".join(ColorType.sequences)
sequences = bytes(sequences, "utf-8").decode("unicode_escape") sequences = fix_escape(sequences)
# Get a list of terminals. # Get a list of terminals.
terminals = ["%s%s" % ("/dev/pts/", term) terminals = ["%s%s" % ("/dev/pts/", term)
@ -440,30 +434,30 @@ def set_wallpaper(img):
def save_colors(colors, export_file, message): def save_colors(colors, export_file, message):
"""Export colors to var format.""" """Export colors to var format."""
colors = "".join(colors) colors = "%s\n" % ("\n".join(colors))
save_file(colors, export_file) save_file(colors, export_file)
print("export: exported %s." % (message)) print("export: exported %s." % (message))
def export_rofi(colors): def export_rofi(colors):
"""Append rofi colors to the x_colors list.""" """Append rofi colors to the x_colors list."""
ColorType.xrdb.append("rofi.color-window: %s, %s, %s\n" ColorType.xrdb.append("rofi.color-window: %s, %s, %s"
% (colors[0], colors[0], colors[10])) % (colors[0], colors[0], colors[10]))
ColorType.xrdb.append("rofi.color-normal: %s, %s, %s, %s, %s\n" ColorType.xrdb.append("rofi.color-normal: %s, %s, %s, %s, %s"
% (colors[0], colors[15], colors[0], % (colors[0], colors[15], colors[0],
colors[10], colors[0])) colors[10], colors[0]))
ColorType.xrdb.append("rofi.color-active: %s, %s, %s, %s, %s\n" ColorType.xrdb.append("rofi.color-active: %s, %s, %s, %s, %s"
% (colors[0], colors[15], colors[0], % (colors[0], colors[15], colors[0],
colors[10], colors[0])) colors[10], colors[0]))
ColorType.xrdb.append("rofi.color-urgent: %s, %s, %s, %s, %s\n" ColorType.xrdb.append("rofi.color-urgent: %s, %s, %s, %s, %s"
% (colors[0], colors[9], colors[0], % (colors[0], colors[9], colors[0],
colors[9], colors[15])) colors[9], colors[15]))
def export_emacs(colors): def export_emacs(colors):
"""Set emacs colors.""" """Set emacs colors."""
ColorType.xrdb.append("emacs*background: %s\n" % (colors[0])) ColorType.xrdb.append("emacs*background: %s" % (colors[0]))
ColorType.xrdb.append("emacs*foreground: %s\n" % (colors[15])) ColorType.xrdb.append("emacs*foreground: %s" % (colors[15]))
def export_xrdb(colors, export_file): def export_xrdb(colors, export_file):
@ -480,7 +474,7 @@ def export_colors(colors):
save_colors(ColorType.shell, CACHE_DIR / "colors.sh", "shell variables") save_colors(ColorType.shell, CACHE_DIR / "colors.sh", "shell variables")
# Web based colors. # Web based colors.
ColorType.css.append("}\n") ColorType.css.append("}")
save_colors(ColorType.css, CACHE_DIR / "colors.css", "css variables") save_colors(ColorType.css, CACHE_DIR / "colors.css", "css variables")
save_colors(ColorType.scss, CACHE_DIR / "colors.scss", "scss variables") save_colors(ColorType.scss, CACHE_DIR / "colors.scss", "scss variables")
@ -499,16 +493,6 @@ def export_colors(colors):
# OTHER FUNCTIONS {{{ # OTHER FUNCTIONS {{{
def read_file(input_file):
"""Read colors from a file"""
with open(input_file) as file:
contents = file.readlines()
# Strip newlines from each list element.
contents = [x.strip() for x in contents]
return contents
def reload_colors(vte): def reload_colors(vte):
"""Reload colors.""" """Reload colors."""
sequence_file = pathlib.Path(CACHE_DIR / "sequences") sequence_file = pathlib.Path(CACHE_DIR / "sequences")
@ -521,13 +505,23 @@ def reload_colors(vte):
if vte: if vte:
sequences = re.sub(r"\]708;\#.{6}", "", sequences) sequences = re.sub(r"\]708;\#.{6}", "", sequences)
# Decode the string. # Make the terminal interpret escape sequences.
sequences = bytes(sequences, "utf-8").decode("unicode_escape") sequences = fix_escape(sequences)
print(sequences, end="") print(sequences, end="")
exit(0) exit(0)
def read_file(input_file):
"""Read colors from a file"""
with open(input_file) as file:
contents = file.readlines()
# Strip newlines from each list element.
contents = [x.strip() for x in contents]
return contents
def save_file(colors, export_file): def save_file(colors, export_file):
"""Write the colors to the file.""" """Write the colors to the file."""
with open(export_file, "w") as file: with open(export_file, "w") as file:
@ -545,6 +539,11 @@ def hex_to_rgb(color):
return "%s,%s,%s" % (red, green, blue) return "%s,%s,%s" % (red, green, blue)
def fix_escape(string):
"""Decode a string."""
return bytes(string, "utf-8").decode("unicode_escape")
# }}} # }}}