dotfiles/.bash_prompt
Alexandra Dunn 5fc152455f COOL PINK
2021-09-29 15:28:05 -07:00

106 lines
2.5 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# prompt is largely due to $js
# https://github.com/jennschiffer/dotfiles/blob/master/bash_prompt
# http://stackoverflow.com/a/1703567
# Solarized colors
# https://github.com/altercation/solarized/tree/master/iterm2-colors-solarized
BOLD=$(tput bold)
RESET=$(tput sgr0)
COOL_PINK=$(tput setaf 13)
SOLAR_YELLOW=$(tput setaf 136)
SOLAR_ORANGE=$(tput setaf 166)
SOLAR_RED=$(tput setaf 124)
SOLAR_MAGENTA=$(tput setaf 125)
SOLAR_VIOLET=$(tput setaf 61)
SOLAR_BLUE=$(tput setaf 33)
SOLAR_CYAN=$(tput setaf 37)
SOLAR_GREEN=$(tput setaf 64)
SOLAR_WHITE=$(tput setaf 254)
is_git_repo() {
$(git rev-parse --is-inside-work-tree &> /dev/null)
}
is_git_dir() {
$(git rev-parse --is-inside-git-dir 2> /dev/null)
}
get_git_branch() {
local branch_name
# Get the short symbolic ref
branch_name=$(git symbolic-ref --quiet --short HEAD 2> /dev/null) ||
# If HEAD isn't a symbolic ref, get the short SHA
branch_name=$(git rev-parse --short HEAD 2> /dev/null) ||
# Otherwise, just give up
branch_name="(unknown)"
printf $branch_name
}
# Git status information
prompt_git() {
local git_info git_state uc us ut st
if ! is_git_repo || is_git_dir; then
return 1
fi
git_info=$(get_git_branch)
# Check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
uc="${SOLAR_GREEN}+"
fi
# Check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
us="${SOLAR_RED}!"
fi
# Check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
ut="${SOLAR_ORANGE}?"
fi
# Check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
st="${SOLAR_YELLOW}$"
fi
git_state=$uc$us$ut$st
# Combine the branch name and state information
if [[ $git_state ]]; then
git_info="$git_info${SOLAR_CYAN}[$git_state${SOLAR_CYAN}]${RESET}"
fi
# You know youre on `gh-pages`, right? *Right*?
if [[ $git_info == "gh-pages" ]]; then
style_branch="${SOLAR_BLUE}"
fi
# Dont screw up `stable`.
if [[ $git_info == *stable* ]]; then
style_branch="${SOLAR_RED}"
fi
printf "${RESET}${SOLAR_WHITE} on ${RESET}${style_branch}${git_info}"
}
# TODO: what is this
PS1="\[\033]0;\w\007\]"
PS1+="\[$BOLD\]\u\[$RESET\]"
PS1+="\[$SOLAR_WHITE\] in\[$RESET\]"
PS1+="\[$COOL_PINK\] \h\[$RESET\]"
PS1+="\[$SOLAR_WHITE\] at\[$RESET\]"
PS1+="\[$SOLAR_BLUE\] \w"
PS1+="\$(prompt_git)"
PS1+="\[$RESET\]"
PS1+="\n=> "
export PS1;
PS2="\[$SOLAR_WHITE\]→ \[$RESET\]";
export PS2;