Created
November 3, 2017 22:48
-
-
Save gknoy/8fa785dbb3c728e8047b943f94c51477 to your computer and use it in GitHub Desktop.
iterm2 terminal helpers (sourced as part of login)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# iterm2-badge.sh | |
# | |
# Send escape sequences so that iterm2 can set the badge to arbitrary text | |
# | |
# https://www.iterm2.com/documentation-badges.html | |
# | |
function iterm2_print_user_vars() { | |
iterm2_set_user_var gitBranch $((git branch 2> /dev/null) | grep --color=never \* | cut -c3-) | |
} | |
# e.g. i2-badge "central05" | |
function i2-badge { | |
# Set badge to show arbitrary text | |
export ITERM2_BADGE="$1" # lets my prompt auto-revert to this ;) | |
printf "\e]1337;SetBadgeFormat=%s\a" \ | |
$(echo -n "$1" | base64) | |
} | |
alias ib='i2-badge' | |
function hostname-badge { | |
export ITERM2_BADGE="`hostname`" # lets my prompt auto-revert to this ;) | |
printf "\e]1337;SetBadgeFormat=%s\a" $(echo -n "`hostname | sed 's/-.*//'`" | base64) | |
} | |
function gb-badge { | |
export ITERM2_BADGE="\(user.gitBranch)" | |
# Set badge to show the current session name and git branch, if any is set. | |
printf "\e]1337;SetBadgeFormat=%s\a" \ | |
$(echo -n "\(user.gitBranch)" | base64) | |
} | |
# | |
# Set Tab colors | |
# | |
# | |
# iterm2 colors | |
# | |
function tab-color { | |
echo -ne "\033]6;1;bg;red;brightness;$1\a" | |
echo -ne "\033]6;1;bg;green;brightness;$2\a" | |
echo -ne "\033]6;1;bg;blue;brightness;$3\a" | |
} | |
function tab-reset { | |
echo -ne "\033]6;1;bg;*;default\a" | |
} | |
# | |
# Automatically set the current tab color based on what project folder I am working on. | |
# | |
# A coworker made the (IMO brilliant!) suggestion to derive the color from a hash of the path, instead of this. | |
# | |
function auto-tab-color { | |
_p=`pwd` | |
current=`gbasename ${_p}` | |
case "$current" in | |
'foo-ui') | |
tab-color 40 110 240 # blue | |
;; | |
'bar-ui') | |
tab-color 200 200 40 # yellow | |
;; | |
'foo') | |
tab-color 210 110 70 # orange | |
;; | |
'foo-core') | |
tab-color 150 40 150 # purple | |
;; | |
'devops-tools') | |
# when releasing | |
tab-color 150 40 40 # dark red | |
;; | |
*) | |
tab-reset | |
;; | |
esac | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Prompt configuration. | |
# | |
# Originally swiped from tobiassjosten's example of setting git prompt stuff [0] | |
# with additions of iterm2 coloring/badging. | |
# | |
# 0: [https://gist.github.com/tobiassjosten/828432] | |
# | |
# Configure colors, if available. | |
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then | |
c_reset='\[\e[0m\]' | |
c_user='\[\e[1;32m\]' | |
c_host='\[\e[0;32m\]' | |
c_path='\[\e[1;34m\]' | |
c_git_clean='\[\e[0;37m\]' | |
c_git_staged='\[\e[0;32m\]' | |
c_git_unstaged='\[\e[0;31m\]' | |
else | |
c_reset= | |
c_user= | |
c_path= | |
c_git_clean= | |
c_git_staged= | |
c_git_unstaged= | |
fi | |
# Add the titlebar information when it is supported. | |
case $TERM in | |
xterm*|rxvt*) | |
TITLEBAR='\[\e]0;\u@\h: \w\a\]'; | |
;; | |
*) | |
TITLEBAR=""; | |
;; | |
esac | |
# Function to assemble the Git parsing part of our prompt. | |
git_prompt () | |
{ | |
GIT_DIR=`git rev-parse --git-dir 2>/dev/null` | |
if [ -z "$GIT_DIR" ]; then | |
return 0 | |
fi | |
GIT_HEAD=`cat $GIT_DIR/HEAD` | |
GIT_BRANCH=${GIT_HEAD##*/} | |
if [ ${#GIT_BRANCH} -gt 40 ]; then | |
# GIT_BRANCH="(no branch)" | |
GIT_BRANCH="${GIT_BRANCH:0:40}..." | |
fi | |
STATUS=`git status --porcelain` | |
if [ -z "$STATUS" ]; then | |
git_color="${c_git_clean}" | |
else | |
echo -e "$STATUS" | grep -q '^ [A-Z\?]' | |
if [ $? -eq 0 ]; then | |
git_color="${c_git_unstaged}" | |
else | |
git_color="${c_git_staged}" | |
fi | |
fi | |
branch_depth=`git rev-list HEAD --not --remotes|wc -l|tr -d ' '` | |
echo "[$git_color$GIT_BRANCH$c_reset:${branch_depth}]" | |
} | |
# re-print $ITERM2_BADGE | |
# (This will reset it to what it originally was if we ssh to a system that changes it.) | |
autoset_i2_badge () { | |
printf "\e]1337;SetBadgeFormat=%s\a" \ | |
$(echo -n "${ITERM2_BADGE}" | base64) | |
auto-tab-color | |
} | |
# Thy holy prompt. | |
# ^^^ | |
PROMPT_COMMAND="autoset_i2_badge && $PROMPT_COMMAND && PS1=\"${TITLEBAR}${c_user}\u${c_reset}@${c_host}\h${c_reset}:${c_path}\w${c_reset}\$(git_prompt)\$ \" ;" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment