Skip to content

Instantly share code, notes, and snippets.

@HotelCalifornia
Last active February 16, 2018 04:52
Show Gist options
  • Save HotelCalifornia/353833ed45270263277b3413065ad9d8 to your computer and use it in GitHub Desktop.
Save HotelCalifornia/353833ed45270263277b3413065ad9d8 to your computer and use it in GitHub Desktop.
quick way to change background image in hyper terminal
#!/usr/bin/env bash
# change hyper background image given a file
python3 $HOME/change_bg.py $1
# change this to your own hyper config
cp $HOME/.hyper.js /path/to/windows/.hyper.js
#!/usr/bin/env python3
import colorthief as ct
import colour as color
import fileinput
import os
import re
import subprocess
import sys
def stringify(c):
return hex((int(hex(int(c.red*255)), base=16) << 0x10) + (int(hex(int(c.green*255)), base=16) << 0x08) + int(hex(int(c.blue*255)), base=16))
if len(sys.argv) < 2:
print("need a file path")
sys.exit(-1)
argc = 1
if sys.argv[0].lower().startswith('python'):
argc = 2
p = subprocess.Popen(['cat', '/proc/version'], stdout=subprocess.PIPE)
x, _ = p.communicate()
IS_WSL = 'microsoft' in x.decode().lower()
CONF_PATH = os.path.expanduser('~/.hyper.js')
img_path = sys.argv[argc]
if not os.path.exists(img_path): # check path using native module
print("{} does not exist!".format(img_path))
sys.exit(-1)
img = ct.ColorThief(img_path)
tbg = img.get_color()
bg = color.Color(red=tbg[0]/255, green=tbg[1]/255, blue=tbg[2]/255)
fgl = 1 if bg.luminance < 0.5 else 0
fg = color.Color(hue=bg.hue, saturation=bg.saturation, luminance=fgl)
if IS_WSL: # but if env is WSL, change path to windows path for hyper's sake
p = subprocess.Popen(['wslpath', '-m', sys.argv[argc]], stdout=subprocess.PIPE)
x, _ = p.communicate()
img_path = x.decode()[:-1]
r = re.compile(r"(css: '\.terms_terms ){( background: url\()(.+)(\) center;.+)}(',)")
found = False
with open(CONF_PATH) as conf:
for line in conf:
match = r.search(line)
if match:
found = True
break
if not found:
for line in fileinput.input(CONF_PATH, inplace=True):
line = re.sub(r"(foregroundColor: ').+(',)$", r"\g<1>{}\g<2>".format(stringify(fg)), line)
line = re.sub(r"css: '',$", "css: '.terms_terms {{ background: url(file://{}) center; background-size: cover; }}',".format(img_path), line)
sys.stdout.write(re.sub(r"termCSS: '',$", "termCSS: 'x-screen { background: transparent !important; }',", line))
sys.exit(0)
for line in fileinput.input(CONF_PATH, inplace=True):
line = re.sub(r"(foregroundColor: ').+(',)$", r"\1{}\2".format(str(fg)), line)
line = r.sub(r'\1{{\2"file://{}"\4}}\5'.format(img_path), line)
sys.stdout.write(line)
sys.exit(0)
#!/usr/bin/env bash
# change hyper background to a random image given a directory
ls $1 | sort -R | tail -1 | while read file; do
$HOME/cbg $1/$file
done
@HotelCalifornia
Copy link
Author

for an extra fun time, you can also add the rcbg command to the PROMPT_COMMAND shell variable, so every time you hit enter (or after a program completes) you'll get a new, random background

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment