-
-
Save HotelCalifornia/353833ed45270263277b3413065ad9d8 to your computer and use it in GitHub Desktop.
quick way to change background image in hyper terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
yeah, maybe this could be a hyper plugin, but this stems from a prototype of something I was working on in my fork of the Pokemon Terminal.
but anyway, this is a fun way to make your terminal display random pictures in the background. add
rcbg
to yourbashrc
or set up acrontab
rule.note that if you are running hyper with WSL, you should make available a copy of your
.hyper.js
in your home directory. otherwise, anything goes with the image path (thanks to the handy wslpath command -- though you may need to be on an insider build).change_bg.py
will attempt to set the terminal's foreground color to contrast with the new image's dominant color, but I make no guarantees on any other colors that decorate your terminal. it's also probably best to use images with simple colors.finally, note that if you already have custom
css
ortermCSS
defined this will likely not work.