summaryrefslogtreecommitdiff
path: root/zsh/rc.d/40-prompt
blob: d3519ee54e79e66061f977ed27986dfb6432693d (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# -*- sh -*-

zmodload zsh/pcre

autoload colors && colors

_VCS_RESET="%{${reset_color}%}"
_VCS_COLOUR="%{${reset_color}${fg_bold[cyan]}%}"
_VCS_BRANCH_COLOUR="%{${reset_color}${fg_bold[yellow]}%}"
_SUDO_RESET="%{${reset_color}%}"
_SUDO_ALLOWED="%{${reset_color}${fg_bold[red]}%}"

_VCS_GENERIC_AWK_SCRIPT=$(cat <<'EOF'
BEGIN {
  unk=0;
  add=0;
  del=0;
  mod=0;
  con=0;
  mer=0;
  ren=0;
  prop=0;
  other=0;
  others="";
}

{
  if ($1 == "?") { unk++; } else
  if ($1 == "A") { add++; } else
  if ($1 == "M") { mod++; } else
  if ($1 == "D" || $1 == "-D") { del++; } else
  if ($1 == "C") { con++; } else
  if ($1 == "N" || $1 == "+N") { add++; } else
  if ($1 == "P" || $1 == "P.") { mer++; } else
  if ($1 == "R") { ren++; } else
  if ($1 == "Performing") { } else
  if ($1 == "RM") { ren++; mod++; } else
  if ($1 == "*") { prop++; } else
  if ($1 == "X") { } else
  if ($1 == "") { } else
  if ($1 == "Current") { } else
  { other++; others = others $1}
}

END {
  ret="";
  if (unk > 0) ret = ret "?";
  if (add > 0) ret = ret "A";
  if (del > 0) ret = ret "D";
  if (ren > 0) ret = ret "R";
  if (mod > 0) ret = ret "M";
  if (prop > 0) ret = ret "F"
  if (con > 0) ret = ret "C[" con "]";
  if (mer > 0) ret = ret "+P";
  if (other > 0) ret = ret "-";
  print ret others;
}
EOF
)

_VCS_GIT_AWK_SCRIPT=$(cat <<'GITEOF'
BEGIN {
  unk=0;
  add=0;
  del=0;
  mod=0;
  con=0;
  mer=0;
  ren=0;
  other=0;
  others="";
  mode=0;
}

/^\?\?/ {
  unk++;
}

/^A/ {
  add++;
}

/^.A/ {
  add++;
}

/^R/ {
  ren++;
}

/^.R/ {
  ren++;
}

/^D/ {
  del++;
}

/^.D/ {
  del++;
}

/^M/ {
  mod++;
}

/^.M/ {
  mod++;
}

END {
  ret="";
  if (unk > 0) ret = ret "?";
  if (add > 0) ret = ret "A";
  if (del > 0) ret = ret "D";
  if (ren > 0) ret = ret "R";
  if (mod > 0) ret = ret "M";
  if (con > 0) ret = ret "C[" con "]";
  if (mer > 0) ret = ret "+P";
  if (other > 0) ret = ret "-"; 
  print ret others;
}
GITEOF
)

_vcs_status () {
    # Parse a command (passed in as $@) 's output like an svn st or bzr st -S
    # Decide on the working tree status of the tree if possible
    local vcs_status
    _VCS_STATUS=""
    vcs_status=$("$@" "${_VCS_ROOT}" 2>/dev/null)
    # If there are no changes, return now
    [[ "x$vcs_status" == "x" ]] && return 0
    # Okay there are changes
    _VCS_STATUS=$(echo "${vcs_status}" | awk "${_VCS_GENERIC_AWK_SCRIPT}")
}

_vcs_git_status () {
    # Parse a command (passed in as $@) 's output like git status
    # Decide on the working tree status of the tree if possible
    local vcs_status
    vcs_status=$("$@" 2>/dev/null)
    _VCS_STATUS=$(echo "${vcs_status}" | awk "${_VCS_GIT_AWK_SCRIPT}")
}

_find_bzr_root () {
    # Scanning from CWD upward, look for a .bzr directory
    # once found, echo that top level
    local cdir
    cdir=$PWD
    while [[ $(dirname $cdir) != $cdir ]]; do
	[[ -d $cdir/.bzr && -r $cdir/.bzr/README ]] && {
	    echo $cdir
	    return 0
	}
	cdir=$(dirname $cdir)
    done
    return 1
}

# Find if we're in a BZR tree and if so, store its data and succeed
# otherwise return failure
_find_bzr () {
    local bzr_root
    bzr_root=$(_find_bzr_root) || return 1
    # Okay we know it's a bzr tree, let's gather data and form a
    # bit of prompt data...
    
    if ! test -d "$bzr_root/.bzr/branch"; then
        _VCS="bzr-repo"
        _VCS_ROOT="$bzr_root"
        _VCS_BRANCH="N/A"
        _VCS_REVNO="N/A"
        _VCS_STATUS=""
	return 0
    fi

    # Check if we're bound, have push/pull/merge locations saved etc.

    _VCS="bzr"
    _VCS_ROOT="$bzr_root"
    _VCS_BRANCH=$(bzr nick)
    local tree_rev branch_rev
    tree_rev=$(bzr revno --tree || echo "No Tree")
    branch_rev=$(bzr revno)
    if test "x$tree_rev" = "x$branch_rev"; then
        _VCS_REVNO="$branch_rev"
    else
        _VCS_REVNO="$tree_rev/$branch_rev"
    fi
    _vcs_status bzr status -S
}

_svn_uri () {
    # echo the uri of the provided svn dir
    test -r $1/.svn/entries || return 0
    svn info --non-interactive $1 | grep "^URL:" | cut -d' ' -f2
}

_svn_revno () {
    svn info --non-interactive $1 | grep "^Revision:" | cut -d' ' -f2
}

# Find if we're in an SVN tree and if so, find the uppermost level which
# is a full prefix of the CWD
_find_svn () {
    local cwd_uri parent_uri this_uri this_dir
    # Do nothing if we're not in an SVN tree
    test -r .svn/entries || return 1
    # Okay, we're in an SVN tree, so we're going to look upwards until we
    # find a dir which either isn't svn or isn't a strict parent of where
    # we are now.
    cwd_uri=$(_svn_uri $(pwd))
    parent_uri=$(_svn_uri $(pwd)/..)
    this_uri=${cwd_uri}
    this_dir=$(pwd)
    latest_ver=$(_svn_revno $this_dir)
    earliest_ver=$(_svn_revno $this_dir)
    while [[ "x$parent_uri" = "x$(dirname $this_uri)" ]]; do
	this_dir=$(dirname $this_dir)
	this_uri=$parent_uri
	parent_uri=$(_svn_uri ${this_dir}/..)
	this_ver=$(_svn_revno $this_dir)
	if [ "0$this_ver" -gt "0$latest_ver" ]; then
	    latest_ver="$this_ver"
	fi
	if [ "0$this_ver" -lt "0$earliest_ver" ]; then
	    earliest_ver="$this_ver"
	fi
    done
    # Now, this_dir is the top level (the VCS root)
    _VCS="svn"
    _VCS_ROOT="$this_dir"
    if [ "0$latest_ver" != "0$earliest_ver" ]; then
	_VCS_REVNO="$earliest_ver:$latest_ver"
    else
	_VCS_REVNO="$latest_ver"
    fi
    _vcs_status svn status
    # Branch is a little harder.
    [[ "$this_uri" -pcre-match trunk || "$this_uri" -pcre-match branch ]] || {
	# No mention of banches or trunks, just use the basename of the uri
	_VCS_BRANCH=$(basename $this_uri)
	return 0
    }
    [[ "$this_uri" -pcre-match trunk/([^/]+) || "$this_uri" -pcre-match ([^/]+)/trunk ]] && {
	# trunk/blah or blah/trunk
	local trunkname
	trunkname=$(basename $this_dir)
	if [[ "x$trunkname" = "x$match" ]]; then
	    _VCS_BRANCH="trunk"
	else
	    _VCS_BRANCH="trunk($match)"
	fi
	return 0
    }
    [[ "$this_uri" -pcre-match branch[^/]*/(.+)$ ]] && {
	# branches/*
	_VCS_BRANCH="$match"
	return 0
    }

    # Otherwise, use the basename of the svn uri
    _VCS_BRANCH=$(basename $this_uri)
}

_find_git_root () {
    # Scanning from CWD upward, look for a .git directory
    # once found, echo that top level
    local cdir
    cdir=$PWD
    while [[ $(dirname $cdir) != $cdir ]]; do
	[[ -d $cdir/.git && -r $cdir/.git/config ]] && {
	    echo $cdir
	    return 0
	}
	cdir=$(dirname $cdir)
    done
    return 1
}

# Retrieve the git branch name
_get_git_branch () {
    local branchname
    branchname=$(git symbolic-ref -q HEAD 2>/dev/null ||
	git name-rev --name-only HEAD)
    echo "${branchname#refs/heads/}"
}

# Retrieve the seven digit revcode for the branch
_get_git_revcode () {
    (git show-ref -s --abbrev=7 $(_get_git_branch) || git rev-parse --short $(_get_git_branch)) | head -1
}

# Find if we're in a GIT tree, and fill out _VCS_* if we are.
_find_git () {
    local git_root
    git_root=$(_find_git_root) || return 1
    _VCS="git"
    _VCS_ROOT="$git_root"
    _VCS_BRANCH=$(_get_git_branch)
    _VCS_REVNO=$(_get_git_revcode)
    _vcs_git_status git status --porcelain
}

_vcsify_path () {
    # Take the current path and VCSify it given the contents of the _VCS* variables
    _VCS_PROMPT_ROOT=$(print -D $_VCS_ROOT)
    if [[ "x${_VCS_STATUS}" != "x" ]]; then
	_VCS_STATUS=",${_VCS_STATUS}"
    fi
    if [[ $(basename $_VCS_ROOT) == $_VCS_BRANCH ]]; then
	_VCS_PROMPT_BRANCH="[${_VCS_REVNO}${_VCS_STATUS}]"
    else
	_VCS_PROMPT_BRANCH="[${_VCS_BRANCH}:${_VCS_REVNO}${_VCS_STATUS}]"
    fi
    _VCS_PROMPT_LEAF=${PWD#${_VCS_ROOT}}
}

_find_cargo_root () {
    # Scanning from CWD upward, look for a Cargo.toml file
    # once found, echo that top level
    local cdir
    cdir=$PWD
    while [[ $(dirname $cdir) != $cdir ]]; do
	[[ -r $cdir/Cargo.toml ]] && {
	    echo $cdir
	    return 0
	}
	cdir=$(dirname $cdir)
    done
    return 1
}

_RUST_BADGE_CRAB="🦀"
_RUST_BADGE_STABLE="🏠"
_RUST_BADGE_BETA="β"
_RUST_BADGE_NIGHTLY="🌙"

_find_rust () {
    local rust_root
    local toolchain
    rust_root=$(_find_cargo_root) || return 1
    toolchain=$(cd "$rust_root"; rustup show active-toolchain)
    case "$toolchain" in
	stable-*)
	    _RUST_LABEL="${_RUST_BADGE_CRAB}${_RUST_BADGE_STABLE}"
	    ;;
	beta-*)
	    _RUST_LABEL="${_RUST_BADGE_CRAB}${_RUST_BADGE_BETA}"
	    ;;
	nightly-*)
	    _RUST_LABEL="${_RUST_BADGE_CRAB}${_RUST_BADGE_NIGHTLY}"
	    ;;
	*)
	    _RUST_LABEL="${_RUST_BADGE_CRAB}${toolchain}"
    esac

    return 0
}

_find_debian_source_dir () {
    # Scanning from CWD upward, look for a debian/changelog file
    # once found, echo that top level
    local cdir
    cdir=$PWD
    while [[ $(dirname $cdir) != $cdir ]]; do
	[[ -d $cdir/debian && -r $cdir/debian/changelog ]] && {
	    echo $cdir
	    return 0
	}
	cdir=$(dirname $cdir)
    done
    return 1
}

_DEBIAN_BADGE="🍥"

_find_debian () {
    local debian_root
    local version
    debian_root=$(_find_debian_source_dir) || return 1
    version=$(cd "${debian_root}"; dpkg-parsechangelog -S Version)
    _DEBIAN_LABEL="${_DEBIAN_BADGE}${version}"
}

_KEY_BADGE="🔑"

_check_keys () {
    if ssh-add -l >/dev/null 2>/dev/null; then
	_SSH_LABEL="${_KEY_BADGE}"
    else
	_SSH_LABEL=""
    fi
}

# Now hide any functions which won't work due to missing apps.

which bzr >/dev/null 2>&1 || { 
    _find_bzr () { return 1; }
}

which svn >/dev/null 2>&1 || {
    _find_svn () { return 1; }
}

which git-name-rev >/dev/null 2>&1 || test -x /usr/lib/git-core/git-name-rev || {
    _find_git () { return 1; }
}

which dpkg-parsechangelog >/dev/null 2>&1 || {
    _find_debian () { return 1; }
}

# Sudo support

SUDO=$(which sudo)
if test -x "$SUDO" >/dev/null 2>/dev/null; then
    could_sudo () { sudo -n -l /bin/true >/dev/null 2>/dev/null; }
else
    # No sudo support
    could_sudo () { return 1; }
fi

sudo_precmd () {
    if could_sudo; then
	_SUDO_COLOUR="yes"
    else
	_SUDO_COLOUR=""
    fi
}

# And put it all together

precmd () {
    local ok
    ok=1
    _find_svn ||
    _find_bzr ||
    _find_git ||
    {
	ok=0
    }
    psvar=()
    title_precmd
    sudo_precmd
    [[ $ok == 1 ]] && {
	_vcsify_path
	psvar[1]="${_VCS}"
	psvar[2]="${_VCS_PROMPT_ROOT}"
	psvar[3]="${_VCS_PROMPT_BRANCH}"
	psvar[4]="${_VCS_PROMPT_LEAF}"
    }
    psvar[5]="${_SUDO_COLOUR}"

    _LABELS=""
    _find_rust &&   { _LABELS="${_LABELS}${_RUST_LABEL}" }
    _find_debian && { _LABELS="${_LABELS}${_DEBIAN_LABEL}" }

    psvar[6]="$_LABELS"

    _check_keys

    psvar[7]="${_SSH_LABEL}"
}

_VCS_PROMPT="${_VCS_COLOUR}(%1v)${_VCS_RESET}"
_VCS_RPROMPT="%2v${_VCS_BRANCH_COLOUR}%3v${_VCS_RESET}%4v"

SHELLDEPTH=$((${SHELLDEPTH:-0} + 1))
export SHELLDEPTH

if test "x$SHELLDEPTH" != "x1"; then
    _SHELLDEPTH="%{${reset_color}${fg_bold[blue]}%}(${SHELLDEPTH})%{${reset_color}%}"
else
    _SHELLDEPTH=""
fi

# Prompt as machine%                                           /path/on/right
PS1="%(5V.${_SUDO_ALLOWED}.${_SUDO_RESET})%m${_SUDO_RESET}${_SHELLDEPTH}%(1V.${_VCS_PROMPT}.)%(6V.(%6v).)%# "
RPS1="%(1V.${_VCS_RPROMPT}.%~)%7v"

if [ "x$debian_chroot" = "x" -a -e /etc/debian_chroot ]; then
  debian_chroot=$(cat /etc/debian_chroot)
fi

if [ "x$debian_chroot" != "x" ]; then
  PS1="%(5V.${_SUDO_ALLOWED}.${_SUDO_RESET})%m${_SUDO_RESET}/$debian_chroot${_SHELLDEPTH}%(1V.${_VCS_PROMPT}.)%(6V.(%6v).)%# "
fi