Skip to content

Instantly share code, notes, and snippets.

@kopf
Last active April 7, 2018 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kopf/a8a12922a6fdbcad394a4a94a2c1f701 to your computer and use it in GitHub Desktop.
Save kopf/a8a12922a6fdbcad394a4a94a2c1f701 to your computer and use it in GitHub Desktop.
set a different background / foreground tmux bar colour, generated from hostname

~/.tmux.conf

# ... your tmux config
run "~/.tmux_host_specific_colour.py" # set tmux colour based on hostname

~/.tmux_host_specific_color.py

#!/usr/bin/env python3
import hashlib
import socket
import subprocess


BLACKLIST = [ # colors too close to black, see https://i.stack.imgur.com/e63et.png
    0, 16, 232, 233, 234, 235, 236, 237
]


def hash_hostname(salt=''):
    hostname = socket.gethostname() + salt
    hash_ = hashlib.md5(hostname.encode('utf-8')).hexdigest()
    return int(hash_, 16)


def get_color(fg=False):
    color = BLACKLIST[0]
    salt = ''
    while color in BLACKLIST:
        hash_ = hash_hostname(salt)
        if fg:
            hash_ = hash_ // 2
        color = hash_ % 256
        salt += 'a'
    return int(color)


def main():
    bgcolor = get_color()
    fgcolor = get_color(fg=True)
    cmd = "tmux set -g status-bg colour{}".format(bgcolor).split()
    print("Running {}".format(' '.join(cmd)))
    subprocess.check_output(cmd)
    cmd = "tmux set -g status-fg colour{}".format(fgcolor).split()
    print("Running {}".format(' '.join(cmd)))
    subprocess.check_output(cmd)


if __name__ == '__main__':
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment