Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.