Skip to content

Instantly share code, notes, and snippets.

@jessykate
Forked from jakeonrails/.change-tab-color-pwd
Created June 14, 2016 01:04
Show Gist options
  • Save jessykate/08dbc2ff20827d00fabdaa7b2710e463 to your computer and use it in GitHub Desktop.
Save jessykate/08dbc2ff20827d00fabdaa7b2710e463 to your computer and use it in GitHub Desktop.
How to have change the tab color in iTerm2 based on what folder or directory you are in
#!/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
import os
# http://stackoverflow.com/questions/1523427/python-what-is-the-common-header-format
__copyright__ = "Copyright 2012 Mikko Ohtamaa - http://opensourcehacker.com"
__author__ = "Mikko Ohtamaa <mikko@opensourcehacker.com>"
__licence__ = "WTFPL"
__credits__ = ["Antti Haapala"]
USAGE = """
Colorize terminal tab based on the current PWD.
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 hash of string s
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 rainbow_unicorn(lightness, saturation):
"""
Colorize terminal tab by your server name.
Create a color in HSL space where lightness and saturation is locked, tune only hue by the server.
http://games.adultswim.com/robot-unicorn-attack-twitchy-online-game.html
"""
name = os.getcwd()
hue = get_random_by_string(name)
color = colorsys.hls_to_rgb(hue, lightness, saturation)
decorate_terminal(color)
def main():
"""
From Toholampi with love http://www.toholampi.fi/tiedostot/119_yleisesite_englanti_naytto.pdf
"""
if(len(sys.argv) < 3):
sys.exit(USAGE)
lightness = float(sys.argv[1])
saturation = float(sys.argv[2])
rainbow_unicorn(lightness, saturation)
if __name__ == "__main__":
main()
# Change tab color based on pwd.
function tab_color_precmd {
~/.change-tab-color-pwd 0.5 0.5
}
autoload -U add-zsh-hook
add-zsh-hook precmd tab_color_precmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment