Last active
December 1, 2021 12:25
-
-
Save johnhpatton/01a5db3f858b5f04b81e4ccf99e6dc54 to your computer and use it in GitHub Desktop.
.bashrc with Active prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To get the most out of this .bashrc, install bash 4.x or higher along with the following tools: | |
# figlet | |
# screenfetch | |
# | |
# On Mac, install bash via brew and update the shell for the user: | |
# | |
# brew install bash | |
# sudo echo /usr/local/bin/bash >> /etc/shells | |
# chsh -s /usr/local/bin/bash | |
# | |
# For figlet, install the following fonts, example for linux: | |
# curl -sLo "/tmp/figlet-fonts-master.zip" "https://github.com/xero/figlet-fonts/archive/refs/heads/master.zip" | |
# unzip -oj -d /usr/share/figlet/ "/tmp/figlet-fonts-master.zip" | |
# rm -f "/tmp/figlet-fonts-master.zip" | |
# | |
# Installing any of the following tools will enable command completion by default: | |
# terraform | |
# kubectl | |
# eksctl | |
# helm | |
# k3d | |
# vault | |
# consul | |
# azure cli | |
# aws cli | |
# node if using nvm | |
INTERACTIVE=0 | |
# loggers | |
# adjust colors for terminal display needs | |
logdebug() { local NC='\e[0m' WHITE='\e[0;37m'; (( DEBUG )) && logmsg "${WHITE}DEBUG: ${NC}$1${NC}"; } | |
loginfo() { local NC='\e[0m' BICYAN='\e[1;96m'; logmsg "${BICYAN} INFO: ${NC}$1${NC}"; } | |
logok() { local NC='\e[0m' BIGREEN='\e[1;92m'; logmsg "${BIGREEN} OK: ${NC}$1${NC}"; } | |
logwarn() { local NC='\e[0m' BIYELLOW='\e[1;93m'; logmsg "${BIYELLOW} WARN: ${NC}$1${NC}"; } | |
logcrit() { local NC='\e[0m' BIRED='\e[1;91m'; logmsg "${BIRED} CRIT: ${NC}$1${NC}"; } | |
logmsg() { [[ ${FUNCNAME[1]} =~ log.* ]] && (( INTERACTIVE )) && echo -e "$1" || loginfo "$1"; } | |
pathadd() { | |
local option="a" | |
local retval=0 | |
[ "$1" == "-p" ] && option="p" && shift | |
[[ $1 =~ ^- ]] && shift | |
# assign newpath, remove any trailing / character | |
local newpath="${1%/}" | |
local pathvar="$2" | |
# default to PATH | |
[ -z "$pathvar" ] && pathvar="PATH" | |
local pathval=${!pathvar} | |
if [ -d "$newpath" ] && [[ ":$pathval:" != *":$newpath:"* ]]; then | |
if [ "$option" == "-p" ]; then | |
# prepend | |
pathval="$newpath${pathval:+":$pathval"}" | |
else | |
pathval="${pathval:+"$pathval:"}$newpath" | |
fi | |
eval $pathvar=$pathval | |
export $pathvar | |
logdebug "$newpath added to $pathvar." | |
elif [ ! -d "$newpath" ]; then | |
logdebug "$newpath folder does not exist, not updating $pathvar." | |
retval=1 | |
else | |
logdebug "$newpath already in $pathvar, not updating." | |
fi | |
return $(( retval )) | |
} | |
# Global function to run Akamai CLI | |
# Run as a command as if running the akamai command-line. | |
function akamaicli { | |
local module="$1" && shift | |
[ ! -s "${HOME}/.edgerc" ] && echo "ERROR: missing ${HOME}/.edgerc!" && return | |
case $module in | |
ss|siteshield|site-shield) | |
grep -sF '[site-shield]' "${HOME}/.edgerc" || echo "ERROR: missing [site-shield] credentials in ${HOME}/.edgerc" && return | |
docker run -it -v $HOME/.edgerc:/root/.edgerc:ro akamai/firewall akamai site-shield "$@" ;; | |
fw|firewall) | |
grep -sF '[firewall]' "${HOME}/.edgerc" || echo "ERROR: missing [firewall] credentials in ${HOME}/.edgerc" && return | |
docker run -it -v $HOME/.edgerc:/root/.edgerc:ro akamai/firewall akamai firewall "$@" ;; | |
vp|visitor-prioritization) | |
grep -sF '[cloudlets]' "${HOME}/.edgerc" || echo "ERROR: missing [cloudlets] credentials in ${HOME}/.edgerc" && return | |
docker run -it -v $HOME/.edgerc:/root/.edgerc:ro akamai/purge akamai visitor-prioritization $@ ;; | |
purge) | |
grep -sF '[ccu]' "${HOME}/.edgerc" || echo "ERROR: missing [ccu] credentials in ${HOME}/.edgerc" && return | |
docker run -it -v $HOME/.edgerc:/root/.edgerc:ro akamai/purge akamai firewall $@ ;; | |
*) | |
echo "run with: akamaicli {firewall|site-shield|visitor-prioritization|purge} {cmd/opts}" ;; | |
esac | |
} | |
configure_command_line_completion() { | |
local -a enabled=() | |
# Enable bash programmable completion features in interactive shells | |
if [ -f /usr/share/bash-completion/bash_completion ]; then | |
. /usr/share/bash-completion/bash_completion | |
elif [ -f /etc/bash_completion ]; then | |
. /etc/bash_completion | |
fi | |
# aws command line completion | |
type -P aws_completer &>/dev/null && enabled+=("aws") && complete -C $(which aws_completer) aws | |
# terraform command line completion | |
type -P terraform &>/dev/null && enabled+=("terraform") && complete -C $(which terraform) terraform | |
# vault command line completion | |
type -P vault &>/dev/null && enabled+=("vault") && complete -C $(which vault) vault | |
# consul command line completion | |
type -P consul &>/dev/null && enabled+=("consul") && complete -C $(which consul) consul | |
# k3d command line completion | |
type k3d &>/dev/null && enabled+=("k3d") && source <(k3d completion bash) | |
# helm command line completion | |
type helm &>/dev/null && enabled+=("helm") && source <(helm completion bash) | |
# kubectl command line completion | |
type kubectl &>/dev/null && enabled+=("kubectl") && source <(kubectl completion bash) | |
# eksctl command line completion | |
type eksctl &>/dev/null && enabled+=("eksctl") && source <(eksctl completion bash) | |
# node.js command line completion from NVM | |
type node &>/dev/null && [ -n "${NVM_DIR}" ] && enabled+=("node.js") && source "${NVM_DIR}/bash_completion" | |
loginfo "Command Line Completion Enabled:" | |
printf "%s\n" "${enabled[@]/#/ - }" | |
} | |
init_tty() { | |
local this_tty=$(tty) | |
local hostname screenfetch curl | |
local figlet figlet_host figlet_width local_city | |
local weather_log="/tmp/${this_tty#/*}/weather.log" | |
[[ ":$PATH:" != *":/usr/bin:"* ]] && PATH="/usr/bin:$PATH" && echo "updating PATH" | |
type -P figlet &>/dev/null && figlet=$(type -P figlet) || figlet="/usr/bin/figlet" | |
type -P screenfetch &>/dev/null && screenfetch=$(type -P screenfetch) || screenfetch="/usr/bin/screenfetch" | |
type -P curl &>/dev/null && curl=$(type -P curl) || curl="/usr/bin/curl" | |
type -P hostname &>/dev/null && hostname=$(type -P hostname) || hostname="/usr/bin/hostname" | |
# only display init details on first session in a tty | |
if (( $(ps -a -o ppid,comm,tty,pid | grep -F " ${this_tty#/*/} " | grep -F " su " | wc -l) == 0 )); then | |
clear | |
if [ -n "${figlet}" ]; then | |
read -r ROWS COLUMNS <<< $(stty size) | |
# Get figlet fonts from: https://github.com/xero/figlet-fonts | |
figlet_host=$($hostname) | |
figlet_width=$(( (${#figlet_host} + 1) * 13 + 8 )) | |
if (( COLUMNS < figlet_width )); then | |
figlet -w $COLUMNS -f small ${figlet_host} | |
else | |
figlet -w $COLUMNS -f Electronic ${figlet_host} | |
fi | |
fi | |
[ -n "${screenfetch}" ] && ${screenfetch} | |
if [ ! -f "${weather_log}" ] && [ -n "${curl}" ]; then | |
[ ! -d "${weather_log%/*}" ] && mkdir -p "${weather_log%/*}" | |
local_city="$(curl -s ipinfo.io/$(curl -s ifconfig.me)/city)" | |
curl -s -o "${weather_log}" --connect-timeout 1 --max-time 2 "wttr.in/$local_city?0Q" | |
echo "rm -f ${weather_log}" | at now + 60 minute &>/dev/null | |
fi | |
[ -s "${weather_log}" ] && cat "${weather_log}" | |
echo | |
fi | |
} | |
configure_tools() { | |
## NVM | |
if [ -d "${HOME}/.nvm" ]; then | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh" # This loads nvm | |
[ "$(nvm version)" == "none" ] && nvm install node &>/dev/null | |
loginfo "Node version loaded from nvm: $(nvm version)" | |
fi | |
# Python Config | |
if ! type -P python &>/dev/null && type -P python3 &>/dev/null; then | |
loginfo "creating python symlink to python3" | |
pushd "/usr/local/bin" &>/dev/null | |
ln -s $(type -P python3) python | |
popd &>/dev/null | |
elif type -P python &>/dev/null && type -P python3 &>/dev/null && ! [ "$(python --version 2>&1 | sed 's/.* \([0-9]\).*/\1/')" == "3" ]; then | |
if [ ! -L "/usr/local/bin/python" ] && [ -e "$(readlink -f /usr/local/bin/python3)" ]; then | |
loginfo "updating python symlink to python3" | |
pushd "/usr/local/bin" &>/dev/null | |
ln -s python3 python &>/dev/null | |
popd &>/dev/null | |
fi | |
fi | |
# added by travis gem | |
if [ -f "~/.travis/travis.sh" ] && [ -z "${TRAVIS_ENABLED}" ]; then | |
source ~/.travis/travis.sh | |
export TRAVIS_ENABLE=1 | |
loginfo "travis enabled" | |
fi | |
} | |
get_os_info() { | |
if [ -f /etc/os-release ]; then | |
# freedesktop.org and systemd | |
. /etc/os-release | |
OS=$NAME | |
VER=$VERSION_ID | |
elif type lsb_release >/dev/null 2>&1; then | |
# linuxbase.org | |
OS=$(lsb_release -si) | |
VER=$(lsb_release -sr) | |
elif [ -f /etc/lsb-release ]; then | |
# For some versions of Debian/Ubuntu without lsb_release command | |
. /etc/lsb-release | |
OS=$DISTRIB_ID | |
VER=$DISTRIB_RELEASE | |
elif [ -f /etc/debian_version ]; then | |
# Older Debian/Ubuntu/etc. | |
OS=Debian | |
VER=$(cat /etc/debian_version) | |
elif [ -f /etc/SuSe-release ]; then | |
# Older SuSE/etc. | |
. /etc/SuSe-release | |
echo "old suse release" | |
OS=openSUSE | |
VER=$VERSION | |
elif [ -f /etc/redhat-release ]; then | |
# Older Red Hat, CentOS, etc. | |
. /etc/os-release | |
OS=$NAME | |
VER=$VERSION_ID | |
else | |
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc. | |
OS=$(uname -s) | |
VER=$(uname -r) | |
fi | |
} | |
configure_colors() { | |
logdebug "configuring colors..." | |
# colored highlighting is awesome, enable if available | |
if type dircolors &>/dev/null; then | |
if [ ! -f "~/.dir_colors" ]; then | |
logdebug "generating dir colors" | |
dircolors -p | sed '/# directory/c DIR 01;33 # directory for dark theme' > ~/.dir_colors | |
fi | |
logdebug "loading colors" | |
eval "`dircolors -b ~/.dir_colors`" | |
elif [ "${OS}" == "Darwin" ]; then | |
export LSCOLORS='DxGxcxdxCxcgcdabagacad' | |
export CLICOLOR=1 | |
else | |
export LS_COLORS='rs=0:di=01;33:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'; | |
fi | |
# Color for manpages in less makes manpages a little easier to read | |
export LESS_TERMCAP_mb=$'\E[01;31m' | |
export LESS_TERMCAP_md=$'\E[01;31m' | |
export LESS_TERMCAP_me=$'\E[0m' | |
export LESS_TERMCAP_se=$'\E[0m' | |
export LESS_TERMCAP_so=$'\E[01;44;33m' | |
export LESS_TERMCAP_ue=$'\E[0m' | |
export LESS_TERMCAP_us=$'\E[01;32m' | |
} | |
# command_prompt() | |
# | |
# Function that is executed each time a command is run to update the PS1 | |
# variable (ie: prompt). To configure, let's put some fonts in place. | |
# Download and install Gabriele Lana's Awesome Fonts: | |
# | |
# https://github.com/gabrielelana/awesome-terminal-fonts/archive/master.zip | |
# | |
# Unzip the archive and run the install.sh from the resulting | |
# awesome-terminal-fonts-master folder to install some fonts. Some .sh files | |
# will also be installed in ~/.fonts to provide constants for each font that | |
# can be used in scripts. | |
# | |
# If using git, install the git-prompt.sh script in your home folder and add | |
# to .bashrc: | |
# | |
# curl -L -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh | |
# chown +x ~/.git-prompt.sh | |
# echo 'source ~/.git-prompt.sh' >> ~/.bashrc | |
# | |
# You'll need to log out and back in to pull that in, or you'll need to source | |
# the ~/.git-prompt.sh in your current session to test it. | |
# | |
# Color scheme is based on a dark background. If using a light background, | |
# you'll want to experiment with the colors. | |
command_prompt() { | |
#========================================================= | |
# Capture EXIT_CODE MUST BE FIRST!!! | |
#========================================================= | |
local EXIT_CODE="$?" | |
# Reset the PS1 (Prompt String) | |
PS1="" | |
# exit code output variables | |
local exit_code="" | |
local exit_code_color="" | |
# CONSTANTS | |
# Colors for bash prompt | |
# foreground colors, use with echo -e | |
local RESET='\[\e[m\]' # No Color, reset | |
# normal bright high intensity bright + high intensity | |
local DKGREY='\[\e[0;30m\]' | |
local LTGREY='\[\e[1;30m\]' | |
local RED='\[\e[0;31m\]' BRED='\[\e[1;31m\]' IRED='\[\e[0;91m\]' BIRED='\[\e[1;91m\]' | |
local GREEN='\[\e[0;32m\]' BGREEN='\[\e[1;32m\]' IGREEN='\[\e[0;92m\]' BIGREEN='\[\e[1;92m\]' | |
local YELLOW='\[\e[0;33m\]' BYELLOW='\[\e[1;33m\]' IYELLOW='\[\e[0;93m\]' BIYELLOW='\[\e[1;93m\]' | |
local BLUE='\[\e[0;34m\]' BBLUE='\[\e[1;34m\]' IBLUE='\[\e[0;94m\]' BIBLUE='\[\e[1;94m\]' | |
local PURPLE='\[\e[0;35m\]' BPURPLE='\[\e[1;35m\]' IPURPLE='\[\e[0;95m\]' BIPURPLE='\[\e[1;95m\]' | |
local CYAN='\[\e[0;36m\]' BCYAN='\[\e[1;36m\]' ICYAN='\[\e[0;96m\]' BICYAN='\[\e[1;96m\]' | |
local GREY='\[\e[0;37m\]' BGREY='\[\e[1;37m\]' | |
local WHITE='\[\e[0;97m\]' BWHITE='\[\e[1;97m\]' | |
local sym_radioactive=$(echo -e '\u2622') | |
local sym_warning=$(echo -e '\u26A0') | |
local sym_check_mark=$(echo -e '\u2714') | |
local sym_cancel=$(echo -e '\u2718') | |
local sym_sparkle="$(echo -e '\u2728')" | |
local sym_arrow="$(echo -e '\u2937')" | |
local sym_line="$(echo -e '\u2574')" | |
local sym_left_arrow_up="$(echo -e '\u2b11')" | |
local sym_background_process="$(echo -e '\u2749')" | |
local sym_colon="$(echo -e '\u003A')" | |
local user="${CYAN}\u${RESET}" | |
local host="${ICYAN}\h${RESET}" | |
local workdir="${YELLOW}\W${RESET}" | |
# is user in an SSH session? | |
local bracket_color="" # default | |
if [[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]]; then | |
bracket_color="${BPURPLE}" | |
fi | |
local info_bracket_color="${GREY}" | |
local git_branch="" | |
local git_symbols="" | |
local cursor_column="" | |
local cursor_position="" | |
echo -ne "\033[6n" # ask the terminal for the position | |
read -s -d\[ cursor_position # discard the first part of the response | |
read -s -d R cursor_position # store the position in bash variable 'foo' | |
cursor_column=${cursor_position##*;} | |
(( cursor_column > 1 )) && echo | |
# check if user has ~/.git-prompt.sh loaded and run | |
# __git_ps1 if possible | |
local git_ps1="" | |
if declare -F __git_ps1 &>/dev/null; then | |
git_ps1=$(__git_ps1) | |
git_ps1="${git_ps1//[(]}" | |
git_ps1="${git_ps1//[]]}" | |
git_ps1="${git_ps1//[)]}" | |
if [ -n "${git_ps1}" ]; then | |
repo_name=$(basename -s .git $(git config --get remote.origin.url)) | |
git_ps1="${git_ps1##*( )}" | |
# colour branch name depending on state | |
# make the symbols easier to see. | |
local git_branch_color="${GREY}" | |
# staged files, committed but not pushed | |
if [[ "${git_ps1}" =~ "+" ]]; then | |
git_branch_color="${CYAN}" | |
git_ps1="${git_ps1//[+]}" | |
git_symbols+=" ${CYAN}staged${RESET}" | |
fi | |
# if there is something stashed | |
if [[ "${git_ps1}" =~ "$" ]]; then | |
git_branch_color="${GREEN}" | |
git_ps1="${git_ps1//[$]}" | |
git_symbols+=" ${GREEN}stashed${RESET}" | |
fi | |
# unstaged/uncommitted files (dirty) | |
if [[ "${git_ps1}" =~ "*" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[*]}" | |
git_symbols+=" ${YELLOW}unstaged${RESET}" | |
fi | |
# branch is diverged | |
if [[ "${git_ps1}" =~ "<>" ]]; then | |
git_branch_color="${RED}" | |
git_ps1="${git_ps1//[<>]}" | |
git_symbols+=" ${RED}diverged${RESET}" | |
# branch is ahead | |
elif [[ "${git_ps1}" =~ ">" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[>]}" | |
git_symbols+=" ${YELLOW}ahead${RESET}" | |
# branch is behind | |
elif [[ "${git_ps1}" =~ "<" ]]; then | |
git_branch_color="${YELLOW}" | |
git_ps1="${git_ps1//[<]}" | |
git_symbols+=" ${YELLOW}behind${RESET}" | |
fi | |
# untracked files | |
if [[ "${git_ps1}" =~ "%" ]]; then | |
git_branch_color="${RED}" | |
git_ps1="${git_ps1//[%]}" | |
git_symbols+=" ${RED}untracked${RESET}" | |
fi | |
# no changes | |
if [ -z "${git_symbols}" ]; then | |
git_symbols=" up-to-date" | |
fi | |
git_ps1="${git_ps1//=} ${info_bracket_color}=>${RESET}" | |
git_branch="${info_bracket_color}[[git:${RESET}${git_branch_color}${git_ps1}${RESET}${git_symbols}${RESET}${info_bracket_color}]]${RESET}" | |
fi | |
fi | |
local terraform_workspace="" | |
local terraform_workspace_color="${GREY}" | |
if [ -f ".terraform/environment" ]; then | |
[ -n "${TF_WORKSPACE}" ] && terraform_workspace=${TF_WORKSPACE} || terraform_workspace=$(cat .terraform/environment) | |
case ${terraform_workspace} in | |
*dev*|*qa*|*test*) terraform_workspace_color=${CYAN};; | |
*uat*|*stag*) terraform_workspace_color=${YELLOW};; | |
*prod*) terraform_workspace_color=${YELLOW};; | |
esac | |
terraform_workspace="${info_bracket_color}<<${RESET}${terraform_workspace_color}${terraform_workspace}${RESET}${info_bracket_color}>>${RESET}" | |
fi | |
local az_account="" | |
if [ -d "${HOME}/.azure" ]; then | |
az_account=$(az account show --query name -otsv 2>/dev/null) | |
[ -n "${az_account}" ] && az_account="${info_bracket_color}[az:${YELLOW}${az_account}${RESET}]" | |
fi | |
local kube_context="" | |
if [ -f "${HOME}/.kube/config" ] || [ -n "${KUBECONFIG}" ]; then | |
kube_context=$(kubectl config current-context) | |
local kube_context_color="${GREY}" | |
case ${kube_context} in | |
*dev*|*qa*|*test*|*nonprod*) kube_context_color=${CYAN};; | |
*uat*|*stg*|*stag*) kube_context_color=${YELLOW};; | |
*prd*|*prod*) kube_context_color=${YELLOW};; | |
esac | |
kube_context="${info_bracket_color}{k8s:${RESET}${kube_context_color}${kube_context}${RESET}${info_bracket_color}}${RESET}" | |
fi | |
case ${EXIT_CODE} in | |
0) exit_code="${sym_check_mark}" | |
exit_code_color="${BGREEN}" | |
;; | |
127) exit_code="${sym_warning}" # command not found | |
exit_code_color="${BIRED}" | |
;; | |
130) exit_code="${sym_cancel}" # control-c | |
exit_code_color="${BYELLOW}" | |
;; | |
148) exit_code="${sym_sparkle}" # backgrounded command | |
exit_code_color="${BCYAN}" | |
;; | |
*) exit_code="${EXIT_CODE}" # other error code | |
exit_code_color="${RED}" | |
;; | |
esac | |
# backgrounded and paused jobs in the current session. | |
local jobs_details="" | |
local bg_jobs=$(jobs -r | wc -l ) | |
(( bg_jobs > 0 )) && jobs_details="${YELLOW}${sym_background_process}${RESET}${sym_colon}${WHITE}${bg_jobs}${RESET} " | |
(( bg_jobs > 3 )) && jobs_details="${RED}${sym_background_process}${RESET}${sym_colon}${WHITE}${bg_jobs}${RESET} " | |
local st_jobs=$(jobs -s | wc -l ) | |
(( st_jobs > 0 )) && (( st_jobs <= 3 )) && jobs_details+="${LTGREY}${sym_background_process}${RESET}${sym_colon}${WHITE}${st_jobs}${RESET}" | |
(( st_jobs > 3 )) && jobs_details+="${LTGREY}${sym_background_process}${RESET}${sym_colon}${WHITE}${st_jobs}${RESET}" | |
# assemble the prompt | |
[ -n "${az_account}" ] && PS1+="${az_account} " | |
[ -n "${kube_context}" ] && PS1+="${kube_context}" | |
[ -n "${git_branch}" ] && PS1+="${git_branch}" | |
[ -n "${terraform_workspace}" ] && { [ -n "${PS1}" ] && PS1+=" "; PS1+="${terraform_workspace}"; } | |
[ -n "${jobs_details}" ] && { [ -n "${PS1}" ] && PS1+=" "; PS1+="${jobs_details}"; } | |
[ -n "${PS1}" ] && PS1+="\n" | |
PS1+="${LTGREY}${sym_arrow} ${RESET}" | |
PS1+="${exit_code_color}${exit_code} ${RESET}" | |
PS1+="${bracket_color}[${RESET}${user}@${host}${bracket_color}][${RESET}${workdir}${bracket_color}]${RESET}$ " | |
} | |
configure_ps1() { | |
# configure PROMPT_COMMAND which is executed each time before PS1 | |
export PROMPT_COMMAND='command_prompt; (( disablehist )) || { history -a; history -c; history -r;}' | |
} | |
configure_aliases() { | |
export CMD_OPTS="--color=auto" | |
export LS_OPTS="-F ${CMD_OPTS}" | |
# set aliases if unset | |
## Colorize the ls output ## | |
alias ls &>/dev/null || alias ls='/bin/ls ${LS_OPTS}' | |
# Colorize less | |
alias less="less -r" | |
## Use a long listing format ## | |
alias ll &>/dev/null || alias ll='/bin/ls -la ${LS_OPTS}' | |
## Show hidden files ## | |
alias l. &>/dev/null || alias l.='/bin/ls -d .* ${LS_OPTS}' | |
alias dir &>/dev/null || alias dir='/bin/ls --format=vertical ${LS_OPTS}' | |
alias vdir &>/dev/null || alias vdir='/bin/ls --format=long ${LS_OPTS}' | |
alias grep &>/dev/null || alias grep='grep ${CMD_OPTS}' | |
alias fgrep &>/dev/null || alias fgrep='fgrep ${CMD_OPTS}' | |
alias egrep &>/dev/null || alias egrep='egrep ${CMD_OPTS}' | |
# Terraform | |
if type -P tfswitch &>/dev/null; then | |
[ ! -d "$HOME/bin" ] && mkdir -p "$HOME/bin" | |
alias tfswitch &>/dev/null || alias tfswitch="tfswitch --bin=$HOME/bin/terraform" | |
fi | |
if type -P terraform &>/dev/null; then | |
alias tf &>/dev/null || alias tf="terraform" | |
fi | |
# Show current network connections to servers | |
alias ipview="netstat -anpl | grep :80 | awk {'print \$5'} | cut -d\":\" -f1 | sort | uniq -c | sort -n | sed -e 's/^ *//' -e 's/ *\$//'" | |
# Show local open listeners | |
alias openports='netstat -nape --inet' | |
# Weather | |
alias weather &>/dev/null || alias weather="curl http://wttr.in/" | |
# Public IP | |
alias whatismyip="dig +short myip.opendns.com @resolver1.opendns.com" | |
if [ "${OS}" == "Linux" ]; then | |
alias ubupdate="sudo apt update && sudo apt dist-upgrade -y --auto-remove" | |
fi | |
} | |
configure_git() { | |
local git_prompt_sh="" | |
# Git | |
if type git &>/dev/null; then | |
if [ ! "$(git config --global credential.helper)" == "cache" ]; then | |
git config --global credential.helper 'cache --timeout=3600' ${git_credential_type} | |
alias gitdisablecache="git config --global --unset credential.helper" | |
fi | |
if [ -f /usr/lib/git-core/git-sh-prompt ]; then | |
git_prompt_sh="/usr/lib/git-core/git-sh-prompt" | |
else | |
git_prompt_sh="${HOME}/.git-prompt.sh" | |
if [ ! -f "${git_prompt_sh}" ]; then | |
loginfo "git-prompt.sh not found, installing... this is only needed once..." | |
curl -sL -o "${git_prompt_sh}" "https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh" | |
chmod +x "${git_prompt_sh}" | |
fi | |
fi | |
# if .git-prompt.sh exists, set options and execute it | |
if [ -n "${git_prompt_sh}" ]; then | |
GIT_PS1_SHOWDIRTYSTATE=true # this can get sluggish for large repositories | |
GIT_PS1_SHOWUNTRACKEDFILES=true # this runs a git ls-files command and can be rather sluggish | |
GIT_PS1_SHOWSTASHSTATE=true | |
GIT_PS1_SHOWUPSTREAM="auto" | |
GIT_PS1_HIDE_IF_PWD_IGNORED=true | |
GIT_PS1_SHOWCOLORHINTS=true | |
GIT_PS1_STATESEPARATOR="" | |
. "${git_prompt_sh}" | |
fi | |
fi | |
} | |
configure_path() { | |
local bin_dir="" | |
local man_dir="" | |
# Configure common paths | |
if [ "${OS}" == "Darwin" ]; then | |
# Local Bin | |
pathadd -p "/usr/local/bin" | |
# Core Utils, if installed | |
bin_dir="/usr/local/opt/coreutils/libexec/gnubin" | |
pathadd -p "${bin_dir}" && pathadd -p "${bin_dir%/*}/gnuman" "MANPATH" | |
# GNU Diff Utils, if installed | |
bin_dir="/usr/local/opt/diffutils/bin" | |
pathadd -p "${bin_dir}" && pathadd -p "${bin_dir%/*}/share/man" "MANPATH" | |
# GNU Tar, if installed | |
bin_dir="/usr/local/opt/gnu-tar/libexec/gnubin" | |
pathadd -p "${bin_dir}" && pathadd -p "${bin_dir%/*}/gnuman" "MANPATH" | |
# Python Config | |
if type -P python &>/dev/null; then | |
local python_bin=$(readlink -f $(type -P python)) | |
man_dir=${python_bin%%/*/*} | |
pathadd -p "${man_dir}" "MANPATH" | |
if [ ! -e "/usr/local/bin/python" ]; then | |
bin_dir="${python_bin%%/*}" | |
pathadd "${bin_dir}" | |
fi | |
bin_vers+=($(python --version)) | |
fi | |
# Ruby installed by brew | |
bin_dir="/usr/local/opt/ruby/bin" | |
pathadd -p "${bin_dir}" && pathadd -p "${bin_dir%/*}/share/man" "MANPATH" | |
bin_vers+=($(ruby --version | awk '{printf "%s %s\n", $1, $2}')) | |
# Maven, if installed | |
export MAVEN_HOME=/usr/local/apache-maven | |
pathadd "${MAVEN_HOME}/bin" || unset MAVEN_HOME | |
# Android Debug Bridge | |
bin_dir=$(readlink -f ~/Applications/platform-tools) | |
pathadd -p "${bin_dir}" | |
git_credential_type="osxkeychain" | |
else | |
# Python Config | |
if type python3 &>/dev/null; then | |
python3_ver=$(python3 -V | awk '{print $2}') | |
pathadd -p "/usr/lib/python${python3_ver%.*}/dist-packages" "PYTHONPATH" | |
pathadd -p "~/.local/lib/python${python3_ver%.*}/site-packages" "PYTHONPATH" | |
unset python3_ver | |
fi | |
bin_vers+=($(python --version)) | |
# Android Debug Bridge | |
bin_dir=/opt/local/platform-tools | |
pathadd -p "${bin_dir}" | |
export LOCAL_BIN=~/.local/bin | |
pathadd "${LOCAL_BIN}" || unset LOCAL_BIN | |
fi | |
# MySQL, if installed | |
bin_dir=/usr/local/mysql/bin | |
pathadd "${bin_dir}" | |
export GOROOT=/usr/local/go | |
pathadd "${GOROOT}/bin" && bin_vers+=($(go version | awk '{print $3}' | sed 's/go\(.*\)/go \1/g')) || unset GOROOT | |
pathadd "${HOME}/bin" | |
} | |
init_bash() { | |
# Set INTERACTIVE to 1 if interactive, otherwise 0 | |
[[ $- =~ i ]] && INTERACTIVE=1 | |
get_os_info | |
# Disable the bell in interactive shell | |
if (( INTERACTIVE )); then | |
MACHINE_TYPE="host" | |
if [ "${OS}" == "Darwin" ]; then | |
ioreg -l | grep -q -E "Manufacturer" | grep -E "VirtualBox|Oracle|VMware|Parallels" && MACHINE_TYPE="vm" | |
else | |
grep -q '^flags.*\ hypervisor\ ' /proc/cpuinfo && MACHINE_TYPE="vm" | |
fi | |
loginfo "Initializing ${MACHINE_TYPE} env..." | |
hostnamectl status | grep "Chassis" | grep "vm" &>/dev/null && MACHINE_TYPE="vm" || MACHINE_TYPE="host" | |
bind "set bell-style visible" | |
[[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]] && REMOTE=1 || REMOTE=0 | |
[ -n "${LANG}" ] || export LANG="en_US.UTF-8" | |
# Get DPI of screen | |
if type -P xrandr &>/dev/null && [ "${MACHINE_TYPE}" == "host" ] && (( REMOTE == 0 )); then | |
read -r X Y < <(xrandr | grep -w connected | awk '{print $(NF-2)" "$(NF)}') | |
X="${X//[[:alpha:]]}" | |
Y="${Y//[[:alpha:]]}" | |
fi | |
read -r ROWS COLUMNS < <(stty size) | |
[ -z "$COLUMNS" ] && COLUMNS=140 | |
# Make bash check it's window size after a process completes | |
shopt -s checkwinsize | |
# Make bash fix common folder name spelling mistakes (ect vs. etc) | |
shopt -s cdspell | |
## History settings | |
# Expand the history size | |
export HISTFILESIZE=20000 | |
export HISTSIZE=5000 | |
# Don't put duplicate lines in the history and do not add lines that start with a space | |
export HISTCONTROL=erasedups:ignoredups:ignorespace | |
# Set the default editor | |
type vim &>/dev/null && export EDITOR=vim || export EDITOR=vi | |
export VISUAL=$EDITOR | |
configure_tools | |
configure_path | |
configure_colors | |
configure_aliases | |
configure_command_line_completion | |
configure_git | |
configure_ps1 | |
else | |
unset -f command_prompt | |
fi | |
# remove bashrc functions from env to avoid clutter | |
unset -f pathadd | |
unset -f get_os_info | |
unset -f configure_colors | |
unset -f configure_command_line_completion | |
unset -f configure_tools | |
unset -f configure_aliases | |
unset -f configure_git | |
unset -f configure_ps1 | |
unset -f configure_path | |
unset -f init_bash | |
unset -f logmsg | |
unset -f logcrit | |
unset -f logwarn | |
unset -f logok | |
unset -f loginfo | |
unset -f logdebug | |
unset INTERACTIVE | |
} | |
init_bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment