/
prompt
137 lines (118 loc) · 3.41 KB
/
prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Adapted from github.com/bitprophet/dotfiles
#
# Colorized prompt, with different username colors for different systems.
#
# My specific palette, using zsh's builtin color expansions
typeset -A PALETTE C # Force zsh to treat as an array
PALETTE=(
blue 33
cyan 51
green 82
purple 171
red 160
yellow 227
)
# Wrap in %F{} and use short array name
for key in ${(k)PALETTE}; do
C[$key]="%F{$PALETTE[$key]}"
done
# System => color/hostname map:
# USER: username color
# CWD: location/cwd color
# HOST: hostname display
# Defaults:
USER=$C[green]
CWD=$C[blue]
# Manually cut hostname; hostname -s bails out on some systems.
HOST=`hostname | cut -d '.' -f 1`
DOMAIN=`hostname | cut -d '.' -f 2-`
# Themed by domain name
case $DOMAIN in
# Personal production
#bitprophet.org | fabfile.org ) USER=$C[cyan] ;;
# Work production
#whiskeymedia.com | giantbomb.com | comicvine.com \
#| tested.com | screened.com | animevice.com) USER=$C[red] ;;
esac
# Themed by host/node name (takes precedence over domain)
case $HOST in
# Workstations
alex-debian ) USER=$C[red] CWD=$C[green] ;;
esac
function shortpath() {
# Special vim-tab-like shortpath (~/folder/directory/foo => ~/f/d/foo)
_pwd=`pwd | sed "s#$HOME#~#"`
if [[ $_pwd == "~" ]]; then
_dirname=$_pwd
else
_dirname=`dirname "$_pwd" | esed "s/\/(.)[^\/]*/\/\1/g"`
if [[ $_dirname == "/" ]]; then
_dirname=""
fi
_dirname="$_dirname/`basename "$_pwd"`"
fi
echo "${_dirname}"
}
function virtualenv_() {
# Python virtualenv
_venv=`basename "$VIRTUAL_ENV"`
venv="" # need this to clear it when we leave a venv
if [[ -n $_venv ]]; then
venv=$_venv
fi
# RVM (steals the venv slot if there's no venv)
if [[ -s $_rvm && -z $_venv ]]; then
# Just show Ruby gemset. I don't use different interpreter lines, nor
# do I care about patchlevel.
_venv=`$_rvm_home/bin/rvm-prompt g`
if [[ "$_venv" != "system" && -n $_venv ]]; then
venv=$_venv
fi
fi
if [[ -n "$venv" ]]; then
echo "$venv"
else
echo - -
fi
}
function git_status() {
_branch=$(git symbolic-ref HEAD 2>/dev/null)
current_git_branch=${_branch#refs/heads/}
}
function dirty() {
git_status
# Print nothing if not in a git repo
[[ -z $current_git_branch ]] && return
# Git branch / dirtiness
# Dirtiness cribbed from:
# http://henrik.nyh.se/2008/12/git-dirty-prompt#comment-8325834
_dirty="*"
if git update-index -q --refresh &>/dev/null; git diff-index --quiet --cached HEAD --ignore-submodules -- 2>/dev/null && git diff-files --quiet --ignore-submodules 2>/dev/null
then _dirty=""
fi
echo $_dirty
}
function branch() {
git_status
if [[ -n "$current_git_branch" ]]; then
echo "$current_git_branch"
else
echo - -
fi
}
function displayhost() {
# %m/%M seem broken for me? :(
echo "$(hostname)"
}
#
# Prompt itself
#
function precmd() {
# [<green>time</green>] <USER>HOST</USER>:<CWD>pwd</CWD>
# {<magenta>venv</magenta>} [<magenta>branch</magenta>] <CWD>end</CWD>
time="[$C[green]%T%f]"
hostname="${USER}$(displayhost)%f"
_path="${CWD}$(shortpath)%f"
end="${CWD}»%f"
export PS1="${time} ${hostname}:${_path} {$C[purple]$(virtualenv_)%f} [$C[purple]$(branch)$C[red]$(dirty)%f] ${end} "
}