Skip to content

Instantly share code, notes, and snippets.

@jakeonrails
Created July 20, 2016 19:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakeonrails/adb08e614a195b40a96cf1d31329598b to your computer and use it in GitHub Desktop.
Save jakeonrails/adb08e614a195b40a96cf1d31329598b to your computer and use it in GitHub Desktop.
Set Tab Color for ZSH + iTerm2
#!/usr/bin/env ruby
# Change tab color based on pwd.
#
# If a directory contains a .tabcolor file, it will use the lines of that file
# as RGB values. It should look like:
#
# 255
# 128
# 96
# To easily create this file, call `~/.set_tab_color red green blue`
#
# INSTALLATION:
#
# Add the following to your ~/.zshrc for it to work
#
# ```
# function tab_color_precmd { ~/.set_tab_color }
# autoload -U add-zsh-hook
# add-zsh-hook precmd tab_color_precmd
# ```
#
# Save this file in your home directory, and remember to `chmod +x` it.
def set_tab_color(r, g, b)
print "\033]6;1;bg;red;brightness;#{r}\a"
print "\033]6;1;bg;green;brightness;#{g}\a"
print "\033]6;1;bg;blue;brightness;#{b}\a"
end
def rgb_for_pwd
colors = []
colors = File.readlines(color_path) if File.exist?(color_path)
unless colors.size == 3
srand(Dir.pwd.split("/").last.to_i(36))
colors = 3.times.map { rand(0..255) }
end
colors
end
def color_path
File.join(Dir.pwd, ".tabcolor")
end
if ARGV.size == 3
File.open(color_path, 'w') do |f|
ARGV.each {|arg| f.puts arg }
end
else
set_tab_color(*rgb_for_pwd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment