Refactor super_prompt so that it actually works.

This commit is contained in:
Anna Wiggins 2017-05-22 10:26:35 -07:00
parent 2e218bdc0a
commit 5297c7a7e9
No known key found for this signature in database
GPG Key ID: 682893DD0448ED78

View File

@ -2,9 +2,24 @@
# [username@hostname:current_directory] (git_repo|branch) # [username@hostname:current_directory] (git_repo|branch)
# $ # $
source ./colorize.sh src_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source $src_dir/colorize.sh
function super_prompt { 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 {
local git_dir=$(git rev-parse --show-toplevel 2>/dev/null) local git_dir=$(git rev-parse --show-toplevel 2>/dev/null)
if [ "$git_dir" != "" ]; then if [ "$git_dir" != "" ]; then
local path=$(colorize green ${PWD//$git_dir/\!}) local path=$(colorize green ${PWD//$git_dir/\!})
@ -12,18 +27,16 @@ function super_prompt {
local path=$(colorize green ${PWD//$HOME/\~}) local path=$(colorize green ${PWD//$HOME/\~})
fi fi
local git_branch=$(git branch 2>/dev/null | grep "*" | cut -d ' ' -f 2) || "" printf $path
if [ "$git_branch" != "" ]; then }
local git_repo=$(git rev-parse --show-toplevel)
git_branch=\($(colorize magenta ${git_repo//*\/})"|"$(colorize magenta ${git_branch})\)
fi
function super_prompt {
local user=$(colorize yellow $(whoami)) local user=$(colorize yellow $(whoami))
local host=$(colorize blue ${HOSTNAME}) local host=$(colorize blue ${HOSTNAME})
local prompt_char=$(colorize red '$') local prompt_char=$(colorize red '$')
prompt=$(printf "[%s@%s:%s] %s\n\f\r%s " "$user" "$host" "$path" "$git_branch" "$prompt_char") sp_git_info
echo $prompt printf "[%s@%s:%s]\n%s " "$user" "$host" "$(sp_path)" "$prompt_char"
} }
# export PS1=super_prompt export PS1="\$(super_prompt)"