aboutsummaryrefslogtreecommitdiff
path: root/bash/10-prompt.bash
blob: ab3b9775404ba678360918a24f8ac8b2f6573aee (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
# vi:syntax=sh

function prompt_command () {
	local EXIT="$?"
	local REMOTE=""
	local VENV=""
	local EXECTIME=""

	local NOW=$(date +%s)

	[ $EXIT -eq 0 ] && EXIT=""
	[ ! -z "$SSH_CONNECTION" ] && REMOTE="${orange}[R] "
	[ -z "$VIRTUAL_ENV" ] || VENV="$(basename "$VIRTUAL_ENV")"

	if [ -n "$__LAST_PROMPT" ]; then
		EXECTIME=" ($(( NOW - __LAST_PROMPT ))s)"
	fi
	
	PS1="\n${yellow}\t${EXECTIME}${reset_color}\n${REMOTE}${white}\u@${cyan}\h: ${reset_color} ${yellow}\w ${green}${VENV}\n${red}${EXIT} ${reset_color}→ "
	__LAST_PROMPT="$(date +%s)"
}

function preexec() {
	__LAST_PROMPT="$(date +%s)"
}

preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return  # do nothing if completing
    [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
    local this_command=`HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"`;
    preexec "$this_command"
}
trap 'preexec_invoke_exec' DEBUG

PROMPT_COMMAND=prompt_command