summaryrefslogtreecommitdiffstats
path: root/.config/bash/prompt
blob: 84cf84d1edfa8cabc2993f1c59150a492175357a (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
# prompt
#
# bash prompt configuration

# Declared global so that functions that run in PS1 can access
declare -gA _PROMPT_COLORS=(
	[bold_black]=$'\001\e[01;30m\002'
	[bold_red]=$'\001\e[01;31m\002'
	[bold_green]=$'\001\e[01;32m\002'
	[bold_yellow]=$'\001\e[01;33m\002'
	[bold_blue]=$'\001\e[01;34m\002'
	[bold_magenta]=$'\001\e[01;35m\002'
	[bold_cyan]=$'\001\e[01;36m\002'
	[bold_white]=$'\001\e[01;37m\002'
	[reset]=$'\001\e[00m\002'
)

_prompt_shlvl() { ((SHLVL > 1)) && printf '%s:%s ' $'' "$SHLVL"; }

_prompt_jobs() {
	local job_count
	job_count=$(jobs | wc --lines)
	((job_count > 0)) && printf '%s:%s ' $'' "$job_count"
}

_prompt_git() {
	local branch ahead behind diverge=
	# HEAD name (commit SHA in detached HEAD)
	branch=$(git symbolic-ref --short HEAD 2>/dev/null) || branch=$(git rev-parse --short HEAD 2>/dev/null) || return
	branch="${_PROMPT_COLORS[bold_green]}($branch)${_PROMPT_COLORS[reset]}"
	if read -r ahead behind < <(git rev-list --left-right --count "HEAD...@{u}" 2>/dev/null); then
		((ahead > 0)) && diverge+="${_PROMPT_COLORS[bold_green]}↑${ahead}${_PROMPT_COLORS[reset]}"
		((behind > 0)) && diverge+="${_PROMPT_COLORS[bold_red]}↓${behind}${_PROMPT_COLORS[reset]}"
	fi
	printf '%s%s ' "$branch" "${diverge:+ $diverge}"
}

# The quoted parts are expanded on each prompt, the unquoted parts immediately
PS1=${_PROMPT_COLORS[bold_green]}'[\u@\h'${_PROMPT_COLORS[bold_white]}' \W $(_prompt_git)$(_prompt_shlvl)$(_prompt_jobs)]\$'${_PROMPT_COLORS[reset]}' '

# vim: ft=bash