Last active
March 29, 2020 17:53
-
-
Save tezvi/2eb6a4d2fef2a81586c161ed38b9ee52 to your computer and use it in GitHub Desktop.
Add current GIT branch and Symfony environment identifier to your Bash prompt
This file contains hidden or 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
# Bash prompt for GIT and Symfony support | |
# | |
# Author: Andrej Vitez <[email protected]> | |
# | |
# Add this file to your homedir .bash_profile file or create a new one under | |
# /etc/profile.d/git-symfony.sh | |
# This script will add current git branch to your Bash prompt. Also it will try to extract | |
# Symfony APP_ENV environment variable and add it to Bash prompt. | |
# | |
# If no GIT or Symfony data is available in current directory context a default Bash prompt | |
# will be used. | |
# | |
# Example prompt: | |
# tezvi@hostname:/opt/app [feature/APP-1234-add-new-stuff] [sf:dev] | |
# $ █ | |
# Skip all for noninteractive shells. | |
[ ! -t 0 ] && return | |
export CLICOLOR=1 | |
export LSCOLORS=Gxfxcxdxbxegedabagacad | |
COLOR_RED="\033[31m" | |
COLOR_WHITE="\033[0;33m" | |
COLOR_GREEN="\033[0;32m" | |
COLOR_PURPLE="\033[0;32m" | |
COLOR_NONE="\033[0m" | |
function get_git_branch() { | |
GIT_BRANCH_MAXLEN=50 | |
GIT_BRANCH="$(git symbolic-ref --short HEAD 2>/dev/null)" | |
if [[ ! -z "$GIT_BRANCH" ]]; then | |
if [[ "${#GIT_BRANCH}" -gt "${GIT_BRANCH_MAXLEN}" ]]; then | |
GIT_BRANCH="${GIT_BRANCH:0:${GIT_BRANCH_MAXLEN}}*" | |
fi | |
GIT_BRANCH=" ${COLOR_RED}[${GIT_BRANCH}]${COLOR_NONE}" | |
fi | |
echo -e "$GIT_BRANCH" | |
} | |
function get_symfony_env() { | |
ENV_DIR="$(pwd)" | |
while [[ "$ENV_DIR" != "/" ]]; do | |
ENV_PATH="$ENV_DIR/.env" | |
if [[ -d "$ENV_DIR/vendor" && -f "$ENV_PATH" ]]; then | |
break | |
fi | |
ENV_DIR="$(dirname $ENV_DIR)" | |
done | |
if [[ ! -f "$ENV_PATH" ]]; then | |
return | |
fi | |
SF_ENV=$(cat ${ENV_PATH} 2> /dev/null | grep APP_ENV | cut -f2 -d=) | |
if [[ ! -z "$SF_ENV" ]]; then | |
if [[ "${SF_ENV}" == "prod" ]]; then | |
SF_ENV="${COLOR_RED}${SF_ENV}${COLOR_NONE}" | |
fi | |
SF_ENV=" ${COLOR_WHITE}[sf:${COLOR_NONE}${SF_ENV}${COLOR_WHITE}]${COLOR_NONE}" | |
fi | |
echo -e "$SF_ENV" | |
} | |
function generate_ps1() { | |
GIT_BRANCH="$(get_git_branch)" | |
SF_ENV="$(get_symfony_env)" | |
if [[ "$GIT_BRANCH" != "" || "$SF_ENV" != "" ]]; then | |
PS1="$(get_git_branch)$(get_symfony_env)\n\$ " | |
else | |
PS1="\$ " | |
fi | |
echo -e "$PS1" | |
} | |
PS1="${COLOR_GREEN}\u@\h${COLOR_NONE}:\w\$(generate_ps1)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment