util/bash/bashrc

537 lines
18 KiB
Bash

#! /bin/bash
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -u mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s histappend histreedit
shopt -s extglob # useful for programmable completion
bind "set completion-ignore-case on"
bind "set show-all-if-ambiguous on"
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm*|rxvt*) color_prompt=yes;;
esac
export PATH=/opt/tools/arm-unknown-linux-androideabi/bin:$PATH
export PATH=/opt/tools/x86_64-unknown-linux-gnu/bin:$PATH
export PATH=/opt/gcc-arm-none-eabi-4_9-2014q4/bin:$PATH
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
export PATH="$HOME/.cabal/bin:$PATH"
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=no
fi
fi
# Normal Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
NC="\e[m" # Color Reset
COLOR_RED="\033[0;31m"
COLOR_YELLOW="\033[0;33m"
COLOR_GREEN="\033[0;32m"
COLOR_OCHRE="\033[38;5;95m"
COLOR_BLUE="\033[0;34m"
COLOR_WHITE="\033[0;37m"
COLOR_RESET="\033[0m"
ALERT=${BWhite}${On_Red} # Bold White on red background
# Test connection type:
if [ -n "${SSH_CONNECTION}" ]; then
CNX=${Green} # Connected on remote machine, via ssh (good).
elif [[ "${DISPLAY%%:0*}" != "" ]]; then
CNX=${ALERT} # Connected on remote machine, not via ssh (bad).
else
CNX=${BCyan} # Connected on local machine.
fi
# Test user type:
if [[ ${USER} == "root" ]]; then
SU=${Red} # User is root.
elif [[ ${USER} != $(logname) ]]; then
SU=${BRed} # User is not login user.
else
SU=${BCyan} # User is normal (well ... most of us are).
fi
NCPU=$(grep -c 'processor' /proc/cpuinfo) # Number of CPUs
SLOAD=$(( 100*${NCPU} )) # Small load
MLOAD=$(( 200*${NCPU} )) # Medium load
XLOAD=$(( 400*${NCPU} )) # Xlarge load
# Returns system load as percentage, i.e., '40' rather than '0.40)'.
function load()
{
local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
# System load of the current host.
echo $((10#$SYSLOAD)) # Convert to decimal.
}
# Returns a color indicating system load.
function load_color()
{
local SYSLOAD=$(load)
if [ ${SYSLOAD} -gt ${XLOAD} ]; then
echo -en ${ALERT}
elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
echo -en ${Red}
elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
echo -en ${BRed}
else
echo -en ${Green}
fi
}
# Returns a color according to free disk space in $PWD.
function disk_color()
{
if [ ! -w "${PWD}" ] ; then
echo -en ${Red}
# No 'write' privilege in the current directory.
elif [ -s "${PWD}" ] ; then
local used=$(command df -P "$PWD" |
awk 'END {print $5} {sub(/%/,"")}')
if [ ${used} -gt 95 ]; then
echo -en ${ALERT} # Disk almost full (>95%).
elif [ ${used} -gt 90 ]; then
echo -en ${BRed} # Free disk space almost gone.
else
echo -en ${Green} # Free disk space is ok.
fi
else
echo -en ${Cyan}
# Current directory is size '0' (like /proc, /sys etc).
fi
}
# Returns a color according to running/suspended jobs.
function job_color()
{
if [ $(jobs -s | wc -l) -gt "0" ]; then
echo -en ${BRed}
elif [ $(jobs -r | wc -l) -gt "0" ] ; then
echo -en ${BCyan}
fi
}
function git_color {
local git_status="$(git status 2> /dev/null)"
if [[ ! $git_status =~ "working directory clean" ]]; then
echo -e $COLOR_GREEN
elif [[ $git_status =~ "Your branch is ahead of" ]]; then
echo -e $COLOR_YELLOW
elif [[ $git_status =~ "nothing to commit" ]]; then
echo -e $COLOR_GREEN
else
echo -e $COLOR_GREEN
fi
}
function git_branch {
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
echo "($branch)"
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
echo "($commit)"
else
echo ""
fi
}
## https://github.com/twolfson/sexy-bash-prompt
if tput setaf 1 &> /dev/null; then
# Reset the shell from our `if` check
tput sgr0 &> /dev/null
# If you would like to customize your colors, use
# # Attribution: http://linuxtidbits.wordpress.com/2008/08/11/output-color-on-bash-scripts/
# for i in $(seq 0 $(tput colors)); do
# echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
# done
# Save common color actions
prompt_bold="$(tput bold)"
prompt_reset="$(tput sgr0)"
# If the terminal supports at least 256 colors, write out our 256 color based set
if [[ "$(tput colors)" -ge 256 ]] &> /dev/null; then
prompt_user_color="$prompt_bold$(tput setaf 27)" # BOLD BLUE
prompt_preposition_color="$prompt_bold$(tput setaf 7)" # BOLD WHITE
prompt_device_color="$prompt_bold$(tput setaf 39)" # BOLD CYAN
prompt_dir_color="$prompt_bold$(tput setaf 76)" # BOLD GREEN
prompt_git_status_color="$prompt_bold$(tput setaf 154)" # BOLD YELLOW
prompt_git_progress_color="$prompt_bold$(tput setaf 9)" # BOLD RED
else
# Otherwise, use colors from our set of 8
prompt_user_color="$prompt_bold$(tput setaf 4)" # BOLD BLUE
prompt_preposition_color="$prompt_bold$(tput setaf 7)" # BOLD WHITE
prompt_device_color="$prompt_bold$(tput setaf 6)" # BOLD CYAN
prompt_dir_color="$prompt_bold$(tput setaf 2)" # BOLD GREEN
prompt_git_status_color="$prompt_bold$(tput setaf 3)" # BOLD YELLOW
prompt_git_progress_color="$prompt_bold$(tput setaf 1)" # BOLD RED
fi
prompt_symbol_color="$prompt_bold" # BOLD
else
# Otherwise, use ANSI escape sequences for coloring
# If you would like to customize your colors, use
# DEV: 30-39 lines up 0-9 from `tput`
# for i in $(seq 0 109); do
# echo -n -e "\033[1;${i}mText$(tput sgr0) "
# echo "\033[1;${i}m"
# done
prompt_reset="\033[m"
prompt_user_color="\033[1;34m" # BLUE
prompt_preposition_color="\033[1;37m" # WHITE
prompt_device_color="\033[1;36m" # CYAN
prompt_dir_color="\033[1;32m" # GREEN
prompt_git_status_color="\033[1;33m" # YELLOW
prompt_git_progress_color="\033[1;31m" # RED
prompt_symbol_color="" # NORMAL
fi
# Apply any color overrides that have been set in the environment
if [[ -n "$PROMPT_USER_COLOR" ]]; then prompt_user_color="$PROMPT_USER_COLOR"; fi
if [[ -n "$PROMPT_PREPOSITION_COLOR" ]]; then prompt_preposition_color="$PROMPT_PREPOSITION_COLOR"; fi
if [[ -n "$PROMPT_DEVICE_COLOR" ]]; then prompt_device_color="$PROMPT_DEVICE_COLOR"; fi
if [[ -n "$PROMPT_DIR_COLOR" ]]; then prompt_dir_color="$PROMPT_DIR_COLOR"; fi
if [[ -n "$PROMPT_GIT_STATUS_COLOR" ]]; then prompt_git_status_color="$PROMPT_GIT_STATUS_COLOR"; fi
if [[ -n "$PROMPT_GIT_PROGRESS_COLOR" ]]; then prompt_git_progress_color="$PROMPT_GIT_PROGRESS_COLOR"; fi
if [[ -n "$PROMPT_SYMBOL_COLOR" ]]; then prompt_symbol_color="$PROMPT_SYMBOL_COLOR"; fi
# Set up symbols
prompt_synced_symbol=""
prompt_dirty_synced_symbol="*"
prompt_unpushed_symbol="△"
prompt_dirty_unpushed_symbol="▲"
prompt_unpulled_symbol="▽"
prompt_dirty_unpulled_symbol="▼"
prompt_unpushed_unpulled_symbol="⬡"
prompt_dirty_unpushed_unpulled_symbol="⬢"
# Apply symbol overrides that have been set in the environment
# DEV: Working unicode symbols can be determined via the following gist
# **WARNING: The following gist has 64k lines and may freeze your browser**
# https://gist.github.com/twolfson/9cc7968eb6ee8b9ad877
if [[ -n "$PROMPT_SYNCED_SYMBOL" ]]; then prompt_synced_symbol="$PROMPT_SYNCED_SYMBOL"; fi
if [[ -n "$PROMPT_DIRTY_SYNCED_SYMBOL" ]]; then prompt_dirty_synced_symbol="$PROMPT_DIRTY_SYNCED_SYMBOL"; fi
if [[ -n "$PROMPT_UNPUSHED_SYMBOL" ]]; then prompt_unpushed_symbol="$PROMPT_UNPUSHED_SYMBOL"; fi
if [[ -n "$PROMPT_DIRTY_UNPUSHED_SYMBOL" ]]; then prompt_dirty_unpushed_symbol="$PROMPT_DIRTY_UNPUSHED_SYMBOL"; fi
if [[ -n "$PROMPT_UNPULLED_SYMBOL" ]]; then prompt_unpulled_symbol="$PROMPT_UNPULLED_SYMBOL"; fi
if [[ -n "$PROMPT_DIRTY_UNPULLED_SYMBOL" ]]; then prompt_dirty_unpulled_symbol="$PROMPT_DIRTY_UNPULLED_SYMBOL"; fi
if [[ -n "$PROMPT_UNPUSHED_UNPULLED_SYMBOL" ]]; then prompt_unpushed_unpulled_symbol="$PROMPT_UNPUSHED_UNPULLED_SYMBOL"; fi
if [[ -n "$PROMPT_DIRTY_UNPUSHED_UNPULLED_SYMBOL" ]]; then prompt_dirty_unpushed_unpulled_symbol="$PROMPT_DIRTY_UNPUSHED_UNPULLED_SYMBOL"; fi
function prompt_get_git_branch() {
# On branches, this will return the branch name
# On non-branches, (no branch)
ref="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ "$ref" != "" ]]; then
echo "$ref"
else
echo "(no branch)"
fi
}
function prompt_get_git_progress() {
# Detect in-progress actions (e.g. merge, rebase)
# https://github.com/git/git/blob/v1.9-rc2/wt-status.c#L1199-L1241
git_dir="$(git rev-parse --git-dir)"
# git merge
if [[ -f "$git_dir/MERGE_HEAD" ]]; then
echo " [merge]"
elif [[ -d "$git_dir/rebase-apply" ]]; then
# git am
if [[ -f "$git_dir/rebase-apply/applying" ]]; then
echo " [am]"
# git rebase
else
echo " [rebase]"
fi
elif [[ -d "$git_dir/rebase-merge" ]]; then
# git rebase --interactive/--merge
echo " [rebase]"
elif [[ -f "$git_dir/CHERRY_PICK_HEAD" ]]; then
# git cherry-pick
echo " [cherry-pick]"
fi
if [[ -f "$git_dir/BISECT_LOG" ]]; then
# git bisect
echo " [bisect]"
fi
if [[ -f "$git_dir/REVERT_HEAD" ]]; then
# git revert --no-commit
echo " [revert]"
fi
}
prompt_is_branch1_behind_branch2 () {
# $ git log origin/master..master -1
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# Find the first log (if any) that is in branch1 but not branch2
first_log="$(git log $1..$2 -1 2> /dev/null)"
# Exit with 0 if there is a first log, 1 if there is not
[[ -n "$first_log" ]]
}
prompt_branch_exists () {
# List remote branches | # Find our branch and exit with 0 or 1 if found/not found
git branch --remote 2> /dev/null | grep --quiet "$1"
}
prompt_parse_git_ahead () {
# Grab the local and remote branch
branch="$(prompt_get_git_branch)"
remote_branch="origin/$branch"
# $ git log origin/master..master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# If the remote branch is behind the local branch
# or it has not been merged into origin (remote branch doesn't exist)
if (prompt_is_branch1_behind_branch2 "$remote_branch" "$branch" ||
! prompt_branch_exists "$remote_branch"); then
# echo our character
echo 1
fi
}
prompt_parse_git_behind () {
# Grab the branch
branch="$(prompt_get_git_branch)"
remote_branch="origin/$branch"
# $ git log master..origin/master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# If the local branch is behind the remote branch
if prompt_is_branch1_behind_branch2 "$branch" "$remote_branch"; then
# echo our character
echo 1
fi
}
function prompt_parse_git_dirty() {
# If the git status has *any* changes (e.g. dirty), echo our character
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then
echo 1
fi
}
function prompt_is_on_git() {
git rev-parse 2> /dev/null
}
function prompt_get_git_status() {
# Grab the git dirty and git behind
dirty_branch="$(prompt_parse_git_dirty)"
branch_ahead="$(prompt_parse_git_ahead)"
branch_behind="$(prompt_parse_git_behind)"
# Iterate through all the cases and if it matches, then echo
if [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
echo "$prompt_dirty_unpushed_unpulled_symbol"
elif [[ "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
echo "$prompt_unpushed_unpulled_symbol"
elif [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 ]]; then
echo "$prompt_dirty_unpushed_symbol"
elif [[ "$branch_ahead" == 1 ]]; then
echo "$prompt_unpushed_symbol"
elif [[ "$dirty_branch" == 1 && "$branch_behind" == 1 ]]; then
echo "$prompt_dirty_unpulled_symbol"
elif [[ "$branch_behind" == 1 ]]; then
echo "$prompt_unpulled_symbol"
elif [[ "$dirty_branch" == 1 ]]; then
echo "$prompt_dirty_synced_symbol"
else # clean
echo "$prompt_synced_symbol"
fi
}
prompt_get_git_info () {
# Grab the branch
branch="$(prompt_get_git_branch)"
# If there are any branches
if [[ "$branch" != "" ]]; then
# Echo the branch
output="$branch"
# Add on the git status
output="$output$(prompt_get_git_status)"
# Echo our output
echo "$output"
fi
}
# Symbol displayed at the line of every prompt
function prompt_get_prompt_symbol() {
# If we are root, display `#`. Otherwise, `$`
if [[ "$UID" == 0 ]]; then
echo "#"
else
echo "\$"
fi
}
# Adds some text in the terminal frame (if applicable).
NON_VISABLE_HOST=(simon-alten ares simon-laptop)
#Now we construct the prompt.
PROMPT_COMMAND="history -a"
case ${TERM} in
*term* | rxvt* | linux | xterm*)
PS1="\[\$(load_color)\][\A\[${NC}\] "
# Time of day (with load info):
PS1="\[\$(load_color)\][\A\[${NC}\] "
# User@Host (with connection type info):
PS1=${PS1}"\[${SU}\]\u\[${NC}\]"
if [[ ! ${NON_VISABLE_HOST[*]} =~ $HOSTNAME ]]; then
PS1=${PS1}"\[${Purple}\] ($HOSTNAME)\[${NC}\]"
fi
# Git status
PS1=${PS1}"\$(prompt_is_on_git && \
echo -n \" \[$prompt_git_status_color\]\$(prompt_get_git_info)\" && \
echo -n \"\[$prompt_git_progress_color\]\$(prompt_get_git_progress)\" && \
echo -n \"\[$prompt_preposition_color\]\")\[$prompt_reset\] "
# PWD (with 'disk space' info):
PS1=${PS1}"\[\$(disk_color)\]\W]\[${NC}\]"
# Prompt (with 'job' info):
PS1=${PS1}"\[\$(job_color)\] >\[${NC}\] "
# Set title of current xterm:
PS1=${PS1}"\[\e]0;[\u@\h] \w\a\]"
;;
*)
PS1="(\A \u@\h \W) > " # --> PS1="(\A \u@\h \w) > "
# --> Shows full pathname of current dir.
;;
esac
export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
export HISTIGNORE="&:bg:fg:ll:h"
export HISTTIMEFORMAT="$(echo -e ${BCyan})[%d/%m %H:%M:%S]$(echo -e ${NC}) "
export HISTCONTROL=ignoredups
export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
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
fi
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
export WORKON_HOME=~/.Envs