Browsed by
Month: August 2013

Displaying the git branch on your bash prompt

Displaying the git branch on your bash prompt

My bash prompt looks something like this:

prompt

Pretty sweet, eh? At a glance, I can see what git branch I’m in and whether or not it’s dirty (red or green). To get this functionality, I utilized Sexy Bash Prompt. For my needs, I found Sexy Bash Prompt to be a little overweight; so here’s my stripped-down version:

#!/usr/bin/env bash
# Determine what type of terminal to use for tput
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then export TERM=gnome-256color
elif [[ $TERM != dumb ]] && infocmp xterm-256color >/dev/null 2>&1; then export TERM=xterm-256color
fi
if tput setaf 1 &> /dev/null; then
# Colored terminal available
tput sgr0
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
# 256 colors available
COLOR_DARK=$(tput setaf 27)
COLOR_PREPOSITION=$(tput setaf 7)
COLOR_LIGHT=$(tput setaf 39)
COLOR_DIR=$(tput setaf 76)
COLOR_GIT_STATUS=$(tput setaf 154)
COLOR_GIT_STATUS_DIRTY=$(tput setaf 196)
else
# 16 colors available
COLOR_DARK=$(tput setaf 5)
COLOR_PREPOSITION=$(tput setaf 7)
COLOR_LIGHT=$(tput setaf 4)
COLOR_DIR=$(tput setaf 2)
COLOR_GIT_STATUS=$(tput setaf 10)
COLOR_GIT_STATUS_DIRTY=$(tput setaf 9)
fi
NORMAL=$COLOR_PREPOSITION
RESET=$(tput sgr0)
else
# Use ANSI escape sequences for coloring
COLOR_DARK="\033[1;31m"
COLOR_PREPOSITION="\033[1;37m"
COLOR_LIGHT="\033[1;33m"
COLOR_DIR="\033[1;32m"
COLOR_GIT_STATUS="\033[1;32m"
COLOR_GIT_STATUS_DIRTY="\033[1;35m"
RESET="\033[m"
fi
function get_git_branch() {
REF="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ $REF != "" ]]; then
echo $REF
else
echo "no branch"
fi
}
parse_git_dirty () {
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then
echo 1
fi
}
function is_on_git() {
git rev-parse 2> /dev/null
}
get_git_info () {
if [[ $(parse_git_dirty) == 1 ]]; then
COLOR_GIT_STATUS=$COLOR_GIT_STATUS_DIRTY
fi
BRANCH="$(get_git_branch)"
if [[ $BRANCH != "" ]]; then
echo "$COLOR_GIT_STATUS[$BRANCH]"
fi
}
# Define the prompt
PS1="\[$COLOR_LIGHT\]\u\
\[$COLOR_DARK\]@\
\[$COLOR_LIGHT\]\h \
\[$COLOR_DIR\]\w\
\[$COLOR_PREPOSITION\]\
\$( is_on_git && \
echo -n \" \" && \
echo -n \"\[\$(get_git_info)\]\" && \
echo -n \"\[$NORMAL\]\") \
$ \[$RESET\]"

It’s fairly simple to implement. Just save the script to your home directory (e.g. ~/.bash_prompt), make sure it’s executable, and add the following line to the end of your .bashrc:

. ~/.bash_prompt

P.S. You can use this juggernaut command to display a list of possible colors for tput:

( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; )
Using ng-model with contenteditable

Using ng-model with contenteditable

The ng-model directive is glorious! That is, until you try to use it with contenteditable elements. You’d think it would just… work… but it doesn’t. Fortunately, there’s a way to stiff-arm your way through this problem; just pop the following directive into your application:

@myApp.directive 'contenteditable', ->
{
restrict: 'A' # match the 'contenteditable' attribute
require: '?ngModel' # gain access to the associated model
priority: 1 # apply this directive before others
link: (scope, element, attrs, ngModel) ->
# no model? no poblem...
return if not ngModel
# set the view according to the model
ngModel.$render = -> element.html(ngModel.$viewValue || '')
# set the model according to the view
changeModel = -> ngModel.$setViewValue(element.text())
# watch for changes
element.bind 'blur keyup change', -> scope.$apply(changeModel)
}