Bash: Unify color output. Prompt refactoring.
This commit is contained in:
		| @@ -1,3 +1,8 @@ | ||||
| # Print cyan underlined header  | ||||
| function header() { | ||||
| 	echo -e "$UNDERLINE$CYAN$1$NOCOLOR" | ||||
| } | ||||
|  | ||||
| # Create a new directory and enter it | ||||
| function md() { | ||||
| 	mkdir -p "$@" && cd "$@" | ||||
| @@ -5,7 +10,7 @@ function md() { | ||||
|  | ||||
| # Find shorthand | ||||
| function f() { | ||||
|     find . -name "$1" | ||||
| 	find . -name "$1" | ||||
| } | ||||
|  | ||||
| # Get gzipped file size | ||||
| @@ -61,22 +66,13 @@ function extract() { | ||||
| # https://github.com/steckel/Git-Nyan-Graph/blob/master/nyan.sh | ||||
| # If you want big animated version: `telnet miku.acm.uiuc.edu` | ||||
| function nyan() { | ||||
| 	e='\033' | ||||
| 	RESET="$e[0m" | ||||
| 	BOLD="$e[1m" | ||||
| 	CYAN="$e[0;96m" | ||||
| 	RED="$e[0;91m" | ||||
| 	YELLOW="$e[0;93m" | ||||
| 	GREEN="$e[0;92m" | ||||
| 	echo | ||||
| 	echo -en $RED'-_-_-_-_-_-_-_' | ||||
| 	echo -e $RESET$BOLD',------,'$RESET | ||||
| 	echo -e $NOCOLOR$BOLD',------,'$NOCOLOR | ||||
| 	echo -en $YELLOW'_-_-_-_-_-_-_-' | ||||
| 	echo -e $RESET$BOLD'|   /\_/\\'$RESET | ||||
| 	echo -en $GREEN'-_-_-_-_-_-_-' | ||||
| 	echo -e $RESET$BOLD'~|__( ^ .^)'$RESET | ||||
| 	echo -e $NOCOLOR$BOLD'~|__( ^ .^)'$NOCOLOR | ||||
| 	echo -en $CYAN'-_-_-_-_-_-_-' | ||||
| 	echo -e $RESET$BOLD'""  ""'$RESET | ||||
| 	echo | ||||
| } | ||||
|  | ||||
| @@ -98,8 +94,7 @@ add-ssh-host() { | ||||
| 	hostname=$2 | ||||
| 	identifier=$3 | ||||
|  | ||||
| 	if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]] | ||||
| 	then | ||||
| 	if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]]; then | ||||
| 		echo "Usage: configure_ssh_host <username> <hostname> <identifier>" | ||||
| 	else | ||||
| 		if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then | ||||
| @@ -156,8 +151,7 @@ function git-bitbucket() { | ||||
| # USAGE: git-fork <original-author> | ||||
| function git-fork() { | ||||
| 	user=$1 | ||||
| 	if [[ "$user" == "" ]] | ||||
| 	then | ||||
| 	if [[ "$user" == "" ]]; then | ||||
| 		echo "Usage: git-fork <original-author>" | ||||
| 	else | ||||
| 		repo=`basename "$(pwd)"` | ||||
|   | ||||
| @@ -1,38 +1,15 @@ | ||||
| # Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile | ||||
|  | ||||
| # Setup color variables | ||||
| color_is_on= | ||||
| color_red= | ||||
| color_green= | ||||
| color_yellow= | ||||
| color_blue= | ||||
| color_white= | ||||
| color_gray= | ||||
| color_bg_red= | ||||
| color_off= | ||||
| if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then | ||||
| 	color_is_on=true | ||||
| 	color_red="\[$(/usr/bin/tput setaf 1)\]" | ||||
| 	color_green="\[$(/usr/bin/tput setaf 2)\]" | ||||
| 	color_yellow="\[$(/usr/bin/tput setaf 3)\]" | ||||
| 	color_blue="\[$(/usr/bin/tput setaf 6)\]" | ||||
| 	color_white="\[$(/usr/bin/tput setaf 7)\]" | ||||
| 	color_gray="\[$(/usr/bin/tput setaf 8)\]" | ||||
| 	color_off="\[$(/usr/bin/tput sgr0)\]" | ||||
| 	color_error="$(/usr/bin/tput setab 1)$(/usr/bin/tput setaf 7)" | ||||
| 	color_error_off="$(/usr/bin/tput sgr0)" | ||||
| # User color | ||||
| case $(id -u) in | ||||
| 	0) USER_COLOR=$RED ;;  # root | ||||
| 	*) USER_COLOR=$GREEN ;; | ||||
| esac | ||||
|  | ||||
| 	# Set user color | ||||
| 	case `id -u` in | ||||
| 		0) color_user=$color_red ;; | ||||
| 		*) color_user=$color_green ;; | ||||
| 	esac | ||||
| fi | ||||
|  | ||||
| # Some kind of optimization - check if git installed only on config load | ||||
| # Some kind of optimization - check if Git installed only on config load | ||||
| PS1_GIT_BIN=$(which git 2>/dev/null) | ||||
|  | ||||
| function prompt_command { | ||||
| function prompt_command() { | ||||
| 	local PS1_GIT= | ||||
| 	local GIT_BRANCH= | ||||
| 	local GIT_DIRTY= | ||||
| @@ -45,26 +22,26 @@ function prompt_command { | ||||
| 		PWDNAME="~${PWD:${#HOME}}" | ||||
| 	fi | ||||
|  | ||||
| 	# Parse git status and get git variables | ||||
| 	# Parse Git status and get Git variables | ||||
| 	if [[ ! -z "$PS1_GIT_BIN" ]]; then | ||||
| 		# Check if we are in git repo | ||||
| 		# Check if we are in Git repo | ||||
| 		local CUR_DIR="$PWD" | ||||
| 		while [[ ! -d "${CUR_DIR}/.git" ]] && [[ ! "${CUR_DIR}" == "/" ]] && [[ ! "${CUR_DIR}" == "~" ]] && [[ ! "${CUR_DIR}" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done | ||||
| 		if [[ -d "${CUR_DIR}/.git" ]]; then | ||||
| 			# Get git branch | ||||
| 		while [[ ! -d "$CUR_DIR/.git" ]] && [[ ! "$CUR_DIR" == "/" ]] && [[ ! "$CUR_DIR" == "~" ]] && [[ ! "$CUR_DIR" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done | ||||
| 		if [[ -d "$CUR_DIR/.git" ]]; then | ||||
| 			# Get Git branch | ||||
| 			GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)" | ||||
| 			if [[ ! -z "$GIT_BRANCH" ]]; then | ||||
| 				GIT_BRANCH="${GIT_BRANCH#refs/heads/}" | ||||
|  | ||||
| 				# Get git status | ||||
| 				# Get Git status | ||||
| 				local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)" | ||||
| 				[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1 | ||||
| 			fi | ||||
| 		fi | ||||
| 	fi | ||||
|  | ||||
| 	# Build B&W prompt for git | ||||
| 	[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #${GIT_BRANCH}" | ||||
| 	# Build B&W prompt for Git | ||||
| 	[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #$GIT_BRANCH" | ||||
|  | ||||
| 	# Calculate prompt length | ||||
| 	local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3)) | ||||
| @@ -75,35 +52,27 @@ function prompt_command { | ||||
| 		# Strip working directory name | ||||
| 		PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}" | ||||
| 	else | ||||
| 		# Else calculate fillsize | ||||
| 		# Calculate fillsize | ||||
| 		local fillsize=$(($COLUMNS-$PS1_length)) | ||||
| 		FILL="$color_gray" | ||||
| 		while [[ $fillsize -gt 0 ]]; do FILL="${FILL}─"; fillsize=$(($fillsize-1)); done | ||||
| 		FILL="${FILL}${color_off}" | ||||
| 		FILL=$GRAY | ||||
| 		while [[ $fillsize -gt 0 ]]; do FILL=$FILL"─"; fillsize=$(($fillsize-1)); done | ||||
| 		FILL=$FILL$NOCOLOR | ||||
| 	fi | ||||
|  | ||||
| 	if $color_is_on; then | ||||
| 		# Git status for prompt | ||||
| 		if [ ! -z "$GIT_BRANCH" ]; then | ||||
| 			if [ -z "$GIT_DIRTY" ]; then | ||||
| 				PS1_GIT=" #${color_green}${GIT_BRANCH}${color_off}" | ||||
| 			else | ||||
| 				PS1_GIT=" #${color_red}${GIT_BRANCH}${color_off}" | ||||
| 			fi | ||||
| 		fi | ||||
| 	# Git status for prompt | ||||
| 	if [ ! -z "$GIT_BRANCH" ]; then | ||||
| 		local BRANCH_COLOR=$GREEN | ||||
| 		[ ! -z $GIT_DIRTY ] && BRANCH_COLOR=$RED | ||||
| 		PS1_GIT=" #$BRANCH_COLOR$GIT_BRANCH$NOCOLOR" | ||||
| 	fi | ||||
|  | ||||
| 	# Set new color prompt | ||||
| 	PS1="${color_user}${USER}${color_off}@${color_yellow}${HOSTNAME}${color_off}:${color_white}${PWDNAME}${color_off}${PS1_GIT} ${FILL}\n→ " | ||||
|  | ||||
| 	# Get cursor position and add new line if we're not in first column | ||||
| 	echo -en "\033[6n" && read -sdR CURPOS | ||||
| 	[[ "${CURPOS##*;}" -gt 1 ]] && echo "${color_error}●${color_error_off}" | ||||
| 	PS1="$USER_COLOR$USER$NOCOLOR@$YELLOW$HOSTNAME$NOCOLOR:$WHITE$PWDNAME$NOCOLOR$PS1_GIT $FILL\n→ " | ||||
|  | ||||
| 	# Terminal title | ||||
| 	TITLE=`basename ${PWDNAME}` | ||||
| 	[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="${TITLE} \xE2\x80\x94 ${HOSTNAME}" | ||||
| 	echo -ne "\033]0;${TITLE}"; echo -ne "\007" | ||||
| 	local TITLE=$(basename $PWDNAME) | ||||
| 	[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="$TITLE \xE2\x80\x94 $HOSTNAME" | ||||
| 	echo -ne "\033]0;$TITLE"; echo -ne "\007" | ||||
| } | ||||
|  | ||||
| # Set prompt command (title update and color prompt) | ||||
|   | ||||
| @@ -37,6 +37,20 @@ PATH="/usr/local/bin:$PATH" | ||||
| command -v brew >/dev/null 2>&1 && PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH" | ||||
| export PATH | ||||
|  | ||||
| # Colors | ||||
| RED="$(tput setaf 1)" | ||||
| GREEN="$(tput setaf 2)" | ||||
| YELLOW="$(tput setaf 3)" | ||||
| BLUE="$(tput setaf 4)" | ||||
| MAGENTA="$(tput setaf 5)" | ||||
| CYAN="$(tput setaf 6)" | ||||
| WHITE="$(tput setaf 7)" | ||||
| GRAY="$(tput setaf 8)" | ||||
| BOLD="$(tput bold)" | ||||
| UNDERLINE="$(tput sgr 0 1)" | ||||
| INVERT="$(tput sgr 1 0)" | ||||
| NOCOLOR="$(tput sgr0)" | ||||
|  | ||||
| # Load prompt and aliases | ||||
| for file in ~/dotfiles/includes/{bash_prompt,bash_aliases,bash_functions}; do | ||||
| 	[ -r "$file.bash" ] && source "$file.bash" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Artem Sapegin
					Artem Sapegin