-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_prompt
98 lines (83 loc) · 2.73 KB
/
bash_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
#! /bin/bash
# BASH prompt hacking
# See bash_prompt.md for details.
# For machines that are not running at least BASH version 4, just silently
# exit.
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
return 0
fi
function __my_prompt_sshagent_string {
if type -p __my_ssh_agent_prompt_string > /dev/null; then
__my_ssh_agent_prompt_string
fi
}
# Optionally print the gitbranch portion of the prompt
function __my_prompt_print_gitbranch {
[ "${2-}" ] && printf "$@"
}
# Just like (format t "~&")
## UNUSED (for now)
function __my_prompt_format_tilde_ampersand {
local curpos
echo -en "\E[6n"
IFS=";" read -sdR -a curpos
((curpos[1]!=1)) && echo -e '\E[1m\E[41m\E[33m%\E[0m'
}
trap '__previous_command=$__this_command; __this_command=$BASH_COMMAND' DEBUG
__my_prompt_cached_pwd=
__my_prompt_cached_gitbranch=
# For Solaris
if id -u &>/dev/null; then
__my_prompt_id_works=nonnull
else
__my_prompt_id_works=
fi
# This is the value of PROMPT_COMMAND, executed before the prompt is printed.
# We set PS1 here rather than running functions in the prompt because
# each time you use $(...) in PS1 you get a new subshell. That makes
# using shell variables USELESS, and I want to use shell variables
# to cache things, to make consing up a new prompt faster. Cygwin can
# be slow.
function __my_prompt_prompt_command {
local pwd_changed
local agent
local gitbranch
if [ "${__my_prompt_cached_pwd}" -a '(' "${__my_prompt_cached_pwd}" = "$PWD" ')' ]; then
pwd_changed=
# See if we changed the prompt:
if [[ "${__previous_command}" =~ 'git checkout' ]]; then
pwd_changed=xxx
fi
else
pwd_changed=xxx
fi
if [ ! "${__my_prompt_cached_gitbranch}" -o '(' -n "$pwd_changed" ')' ]; then
if git rev-parse --git-dir >/dev/null 2>&1; then
__my_prompt_cached_gitbranch=$(git symbolic-ref -q HEAD 2>/dev/null | sed 's,refs/heads/,,')
fi
fi
agent="$(__my_prompt_sshagent_string)"
gitbranch="$(__my_prompt_print_gitbranch '[git:%s]' ${__my_prompt_cached_gitbranch})"
PS1="$agent\\h$gitbranch"
userchar='$'
if [ "${WINDIR-}" ]; then
if net session > /dev/null 2>&1; then
userchar='#'
fi
elif [ "${__my_prompt_id_works}" ] && [ "$(id -u)" = 0 ]; then
userchar='#'
fi
export PS1="$PS1$userchar "
# First time only, fire off the env-setting function below
if [ ! "$__my_prompt_cached_pwd" ]; then
cd .
fi
__my_prompt_cached_pwd=$PWD
# This tries to ensure that the prompt is printed at the beginning
# of a new line, but I could not get this to work, so it is
# disabled for now.
#__my_prompt_format_tilde_ampersand
}
# "If set, the value is executed as a command prior to issuing
# each primary prompt."
PROMPT_COMMAND=__my_prompt_prompt_command