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

declare -A colors=(
	[bold_black]='\[\e[01;30m\]'
	[bold_red]='\[\e[01;31m\]'
	[bold_green]='\[\e[01;32m\]'
	[bold_yellow]='\[\e[01;33m\]'
	[bold_blue]='\[\e[01;34m\]'
	[bold_magenta]='\[\e[01;35m\]'
	[bold_cyan]='\[\e[01;36m\]'
	[bold_white]='\[\e[01;37m\]'
	[reset]='\[\e[00m\]'
)

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

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

# Current branch (short SHA if detached) plus divergence vs upstream — empty outside a repo
function prompt_git() {
	local green=$'\001\e[01;32m\002' red=$'\001\e[01;31m\002' reset=$'\001\e[00m\002'
	local branch ahead behind diverge=
	branch=$(git symbolic-ref --short HEAD 2>/dev/null) || branch=$(git rev-parse --short HEAD 2>/dev/null) || return
	branch=$green'('$branch')'$reset
	if read -r ahead behind < <(git rev-list --left-right --count "HEAD...@{u}" 2>/dev/null); then
		((ahead > 0)) && diverge+="${green}↑${ahead}${reset}"
		((behind > 0)) && diverge+="${red}↓${behind}${reset}"
	fi
	printf '%s%s ' "$branch" "${diverge:+ $diverge}"
}

PS1=${colors[bold_green]}'[\u@\h'${colors[bold_white]}' \W $(prompt_git)$(prompt_shlvl)$(prompt_jobs)${colors[bold_green]}]\$'${colors[reset]}' '

# vim: ft=bash