-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.zsh
executable file
·67 lines (55 loc) · 2 KB
/
prompt.zsh
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
## Customize prompt
#
# Uses flags:
# - %B ... %b => Makes span bold
# - %F{<color>} ... %f => Colorizes span with <color>
# - %n => Username
# - %<n>~ => Last n directory segments (using ~ for home)
# - %# => Usually shows '%' (would be '#' if running as root)
# - \$(...) => As normal process substitution but defer until runtime
function git-branch() {
: << 'DOCSTRING'
## If in a Git repo, shows current branch name; no output otherwise.
#
# From https://gist.github.com/joseluisq/1e96c54fa4e1e5647940
DOCSTRING
git branch 2> /dev/null | sed --silent --expression='s/^\* \(.*\)/\1/p'
}
function git-repo() {
: << 'DOCSTRING'
## If in a Git repo, shows name of repository; no output otherwise.
DOCSTRING
basename "$(git rev-parse --show-toplevel 2> /dev/null)" 2> /dev/null
}
function toggle-git-prompt() {
: << 'DOCSTRING'
## Toggles display of Git info in shell prompt.
DOCSTRING
case ${+PROMPT_HIDE_GIT} in
(0) export PROMPT_HIDE_GIT='true';;
(1) unset PROMPT_HIDE_GIT;;
esac
}
function _git-info-for-prompt() {
: << 'DOCSTRING'
## Retrieve info about current Git repo for use in prompt.
#
# - If currently in a repo, show current repository name (in blue) and branch name (in magenta).
# - If PROMPT_HIDE_GIT variable is set (to any value), instead show a constant value "(-)".
# - If not in a repo, show nothing. This takes precedence over PROMPT_HIDE_GIT.
DOCSTRING
local REPO; REPO=$(git-repo)
local BRANCH; BRANCH=$(git-branch)
local OPEN_PAREN="%F{blue\}(%f"
local REPO_PART="%F{blue\}$REPO%f"
local BRANCH_PART="%F{magenta\}$BRANCH%f"
local CLOSE_PAREN="%F{blue\})%f"
case ${+PROMPT_HIDE_GIT} in
(0) local GIT_INFO="${OPEN_PAREN}${REPO_PART}:${BRANCH_PART}${CLOSE_PAREN}";;
(1) local GIT_INFO="${OPEN_PAREN}-${CLOSE_PAREN}";;
esac
echo "${REPO:+ $GIT_INFO}"
}
setopt PROMPT_SUBST
export PROMPT="%B%F{green}%3~\$(_git-info-for-prompt)%F{green} %#%f%b "
export RPROMPT="\$(date --utc '+%T')"