Skip to content

Instantly share code, notes, and snippets.

@d6e
Created April 24, 2014 00:11
Show Gist options
  • Save d6e/11236957 to your computer and use it in GitHub Desktop.
Save d6e/11236957 to your computer and use it in GitHub Desktop.
unicorn_term_tabs.py
#!/usr/bin/env python
"""
Set terminal tab / decoration color by the server name.
Get a random colour which matches the server name and use it for the tab colour:
the benefit is that each server gets a distinct color which you do not need
to configure beforehand.
"""
import socket
import random
import colorsys
import sys
# http://stackoverflow.com/questions/1523427/python-what-is-the-common-header-format
__copyright__ = "Copyright 2012 Mikko Ohtamaa - http://opensourcehacker.com"
__author__ = "Mikko Ohtamaa <[email protected]>"
__licence__ = "WTFPL"
__credits__ = ["Antti Haapala"]
USAGE = """
Colorize terminal tab based on the current host name.
Usage: rainbow-parade.py [0-1.0] [0-1.0] # Lightness and saturation values
An iTerm 2 example (recolorize dark grey background and black text):
rainbow-parade.py 0.7 0.4
"""
def get_random_by_string(s):
"""
Get always the same 0...1 random number based on an arbitrary string
"""
# Initialize random gen by server name hash
random.seed(s)
return random.random()
def decorate_terminal(color):
"""
Set terminal tab / decoration color.
Please note that iTerm 2 / Konsole have different control codes over this.
Note sure what other terminals support this behavior.
:param color: tuple of (r, g, b)
"""
r, g, b = color
# iTerm 2
# http://www.iterm2.com/#/section/documentation/escape_codes"
sys.stdout.write("\033]6;1;bg;red;brightness;%d\a" % int(r * 255))
sys.stdout.write("\033]6;1;bg;green;brightness;%d\a" % int(g * 255))
sys.stdout.write("\033]6;1;bg;blue;brightness;%d\a" % int(b * 255))
sys.stdout.flush()
# Konsole
# TODO
# http://meta.ath0.com/2006/05/24/unix-shell-games-with-kde/
def main():
"""
From Toholampi with love http://www.toholampi.fi/tiedostot/119_yleisesite_englanti_naytto.pdf
"""
color = colorsys.hls_to_rgb(random.random(), .5, .4)
decorate_terminal(color)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment