config/bash/super_prompt.sh

43 lines
1.2 KiB
Bash
Raw Normal View History

2017-05-22 17:02:46 +00:00
# Produces a nice, colorful 2-line prompt of the form:
# [username@hostname:current_directory] (git_repo|branch)
# $
src_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source $src_dir/colorize.sh
2017-05-22 17:02:46 +00:00
function sp_git_info {
# Get the current branch name
local git_branch=$(git branch 2>/dev/null | grep "*" | cut -d ' ' -f 2) || ""
# If we're in a git repo, add the repo (i.e. top-level directory) name, parens, and a newline
if [ "$git_branch" != "" ]; then
local git_repo=$(git rev-parse --show-toplevel)
git_branch=\($(colorize magenta ${git_repo//*\/})"|"$(colorize magenta ${git_branch})\)
printf "%s\n" "$git_branch"
else
printf ""
fi
}
function sp_path {
2017-05-22 17:02:46 +00:00
local git_dir=$(git rev-parse --show-toplevel 2>/dev/null)
if [ "$git_dir" != "" ]; then
local path=$(colorize green ${PWD//$git_dir/\!})
else
local path=$(colorize green ${PWD//$HOME/\~})
fi
printf $path
}
function super_prompt {
2017-05-22 17:02:46 +00:00
local user=$(colorize yellow $(whoami))
local host=$(colorize blue ${HOSTNAME})
local prompt_char=$(colorize red '$')
sp_git_info
printf "[%s@%s:%s]\n%s " "$user" "$host" "$(sp_path)" "$prompt_char"
2017-05-22 17:02:46 +00:00
}
export PS1="\$(super_prompt)"