Bash: Unify color output. Prompt refactoring.

This commit is contained in:
Artem Sapegin 2012-12-12 16:52:29 +04:00
parent 3d02d94960
commit 3ccdb8b7e2
3 changed files with 52 additions and 75 deletions

View File

@ -1,3 +1,8 @@
# Print cyan underlined header
function header() {
echo -e "$UNDERLINE$CYAN$1$NOCOLOR"
}
# Create a new directory and enter it # Create a new directory and enter it
function md() { function md() {
mkdir -p "$@" && cd "$@" mkdir -p "$@" && cd "$@"
@ -5,7 +10,7 @@ function md() {
# Find shorthand # Find shorthand
function f() { function f() {
find . -name "$1" find . -name "$1"
} }
# Get gzipped file size # Get gzipped file size
@ -61,22 +66,13 @@ function extract() {
# https://github.com/steckel/Git-Nyan-Graph/blob/master/nyan.sh # https://github.com/steckel/Git-Nyan-Graph/blob/master/nyan.sh
# If you want big animated version: `telnet miku.acm.uiuc.edu` # If you want big animated version: `telnet miku.acm.uiuc.edu`
function nyan() { 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
echo -en $RED'-_-_-_-_-_-_-_' echo -en $RED'-_-_-_-_-_-_-_'
echo -e $RESET$BOLD',------,'$RESET echo -e $NOCOLOR$BOLD',------,'$NOCOLOR
echo -en $YELLOW'_-_-_-_-_-_-_-' echo -en $YELLOW'_-_-_-_-_-_-_-'
echo -e $RESET$BOLD'| /\_/\\'$RESET
echo -en $GREEN'-_-_-_-_-_-_-' echo -en $GREEN'-_-_-_-_-_-_-'
echo -e $RESET$BOLD'~|__( ^ .^)'$RESET echo -e $NOCOLOR$BOLD'~|__( ^ .^)'$NOCOLOR
echo -en $CYAN'-_-_-_-_-_-_-' echo -en $CYAN'-_-_-_-_-_-_-'
echo -e $RESET$BOLD'"" ""'$RESET
echo echo
} }
@ -98,8 +94,7 @@ add-ssh-host() {
hostname=$2 hostname=$2
identifier=$3 identifier=$3
if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]] if [[ "$identifier" == "" ]] || [[ "$username" == "" ]] || [[ "$hostname" == "" ]]; then
then
echo "Usage: configure_ssh_host <username> <hostname> <identifier>" echo "Usage: configure_ssh_host <username> <hostname> <identifier>"
else else
if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then if [ ! -f "$HOME/.ssh/$identifier.id_rsa" ]; then
@ -156,8 +151,7 @@ function git-bitbucket() {
# USAGE: git-fork <original-author> # USAGE: git-fork <original-author>
function git-fork() { function git-fork() {
user=$1 user=$1
if [[ "$user" == "" ]] if [[ "$user" == "" ]]; then
then
echo "Usage: git-fork <original-author>" echo "Usage: git-fork <original-author>"
else else
repo=`basename "$(pwd)"` repo=`basename "$(pwd)"`

View File

@ -1,38 +1,15 @@
# Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile # Inspired by: https://github.com/dreadatour/dotfiles/blob/master/.bash_profile
# Setup color variables # User color
color_is_on= case $(id -u) in
color_red= 0) USER_COLOR=$RED ;; # root
color_green= *) USER_COLOR=$GREEN ;;
color_yellow= esac
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)"
# Set user color # Some kind of optimization - check if Git installed only on config load
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
PS1_GIT_BIN=$(which git 2>/dev/null) PS1_GIT_BIN=$(which git 2>/dev/null)
function prompt_command { function prompt_command() {
local PS1_GIT= local PS1_GIT=
local GIT_BRANCH= local GIT_BRANCH=
local GIT_DIRTY= local GIT_DIRTY=
@ -45,26 +22,26 @@ function prompt_command {
PWDNAME="~${PWD:${#HOME}}" PWDNAME="~${PWD:${#HOME}}"
fi fi
# Parse git status and get git variables # Parse Git status and get Git variables
if [[ ! -z "$PS1_GIT_BIN" ]]; then if [[ ! -z "$PS1_GIT_BIN" ]]; then
# Check if we are in git repo # Check if we are in Git repo
local CUR_DIR="$PWD" local CUR_DIR="$PWD"
while [[ ! -d "${CUR_DIR}/.git" ]] && [[ ! "${CUR_DIR}" == "/" ]] && [[ ! "${CUR_DIR}" == "~" ]] && [[ ! "${CUR_DIR}" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done while [[ ! -d "$CUR_DIR/.git" ]] && [[ ! "$CUR_DIR" == "/" ]] && [[ ! "$CUR_DIR" == "~" ]] && [[ ! "$CUR_DIR" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
if [[ -d "${CUR_DIR}/.git" ]]; then if [[ -d "$CUR_DIR/.git" ]]; then
# Get git branch # Get Git branch
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)" GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
if [[ ! -z "$GIT_BRANCH" ]]; then if [[ ! -z "$GIT_BRANCH" ]]; then
GIT_BRANCH="${GIT_BRANCH#refs/heads/}" GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
# Get git status # Get Git status
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)" local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1 [[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
fi fi
fi fi
fi fi
# Build B&W prompt for git # Build B&W prompt for Git
[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #${GIT_BRANCH}" [[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #$GIT_BRANCH"
# Calculate prompt length # Calculate prompt length
local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3)) local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3))
@ -75,35 +52,27 @@ function prompt_command {
# Strip working directory name # Strip working directory name
PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}" PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}"
else else
# Else calculate fillsize # Calculate fillsize
local fillsize=$(($COLUMNS-$PS1_length)) local fillsize=$(($COLUMNS-$PS1_length))
FILL="$color_gray" FILL=$GRAY
while [[ $fillsize -gt 0 ]]; do FILL="${FILL}"; fillsize=$(($fillsize-1)); done while [[ $fillsize -gt 0 ]]; do FILL=$FILL"─"; fillsize=$(($fillsize-1)); done
FILL="${FILL}${color_off}" FILL=$FILL$NOCOLOR
fi fi
if $color_is_on; then # Git status for prompt
# Git status for prompt if [ ! -z "$GIT_BRANCH" ]; then
if [ ! -z "$GIT_BRANCH" ]; then local BRANCH_COLOR=$GREEN
if [ -z "$GIT_DIRTY" ]; then [ ! -z $GIT_DIRTY ] && BRANCH_COLOR=$RED
PS1_GIT=" #${color_green}${GIT_BRANCH}${color_off}" PS1_GIT=" #$BRANCH_COLOR$GIT_BRANCH$NOCOLOR"
else
PS1_GIT=" #${color_red}${GIT_BRANCH}${color_off}"
fi
fi
fi fi
# Set new color prompt # Set new color prompt
PS1="${color_user}${USER}${color_off}@${color_yellow}${HOSTNAME}${color_off}:${color_white}${PWDNAME}${color_off}${PS1_GIT} ${FILL}\n→ " PS1="$USER_COLOR$USER$NOCOLOR@$YELLOW$HOSTNAME$NOCOLOR:$WHITE$PWDNAME$NOCOLOR$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}"
# Terminal title # Terminal title
TITLE=`basename ${PWDNAME}` local TITLE=$(basename $PWDNAME)
[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="${TITLE} \xE2\x80\x94 ${HOSTNAME}" [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && TITLE="$TITLE \xE2\x80\x94 $HOSTNAME"
echo -ne "\033]0;${TITLE}"; echo -ne "\007" echo -ne "\033]0;$TITLE"; echo -ne "\007"
} }
# Set prompt command (title update and color prompt) # Set prompt command (title update and color prompt)

View File

@ -37,6 +37,20 @@ PATH="/usr/local/bin:$PATH"
command -v brew >/dev/null 2>&1 && PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH" command -v brew >/dev/null 2>&1 && PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
export 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 # Load prompt and aliases
for file in ~/dotfiles/includes/{bash_prompt,bash_aliases,bash_functions}; do for file in ~/dotfiles/includes/{bash_prompt,bash_aliases,bash_functions}; do
[ -r "$file.bash" ] && source "$file.bash" [ -r "$file.bash" ] && source "$file.bash"