Refactor .bashrc so we don't define so many functions

This commit is contained in:
Fernando Schauenburg 2019-10-30 17:16:17 +01:00
parent 51ff9f8fdd
commit cce6134b94

View file

@ -1,8 +1,10 @@
# Return immediately if non-interactive (makes FTP clients happy) # Return immediately if non-interactive (makes FTP clients happy)
[[ "$-" == *i* ]] || return [[ "$-" == *i* ]] || return
bashrc_customize_environment() { ##############################################################################
[ -z "$BACKGROUND" ] && export BACKGROUND="dark" # Customize environment
##############################################################################
export EDITOR="vim" export EDITOR="vim"
export LANG="en_US.UTF-8" export LANG="en_US.UTF-8"
export LANGUAGE="en_US" export LANGUAGE="en_US"
@ -12,6 +14,62 @@ bashrc_customize_environment() {
export LESSHISTFILE=/dev/null export LESSHISTFILE=/dev/null
export PAGER=less export PAGER=less
# Find out where Homebrew performs installations. If Homebrew is not
# installed (e.g. running on Linux), assume /usr/local for our
# installations.
prefix=/usr/local
if command -v brew &>/dev/null; then
prefix=$(brew --prefix)
fi
# Prevent path_helper from messing with the PATH when starting tmux.
# See: https://superuser.com/a/583502
if [ "$(uname)" == "Darwin" ]; then
PATH=""
source /etc/profile
fi
# Add custom bin dirs to PATH if they exist and are not already in PATH.
while read bindir; do
if [ -d "$bindir" ] && [[ ":$PATH:" != *":$bindir:"* ]]; then
PATH="$bindir:$PATH"
fi
done <<EOS
$prefix/bin
$prefix/opt/man-db/libexec/bin
$prefix/opt/coreutils/libexec/gnubin
$prefix/opt/gnu-sed/libexec/gnubin
$HOME/.local/bin
$HOME/bin
EOS
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
export MANPATH=$(MANPATH= manpath)
while read mandir; do
if [ -d "$mandir" ] && [[ ":$MANPATH:" != *":$mandir:"* ]]; then
MANPATH="$mandir:$MANPATH"
fi
done <<EOS
$prefix/share/man
$prefix/opt/man-db/libexec/man
$prefix/opt/coreutils/libexec/gnuman
$prefix/opt/gnu-sed/libexec/gnuman
$HOME/.local/share/man
EOS
unset prefix bindir mandir
##############################################################################
# Customize shell options & variables
##############################################################################
shopt -s cdspell checkwinsize globstar histappend nocaseglob
# Prevent overwriting files by mistake with output redirection.
set -o noclobber
# Eternal bash history (from https://stackoverflow.com/a/19533853) # Eternal bash history (from https://stackoverflow.com/a/19533853)
HISTCONTROL=erasedups HISTCONTROL=erasedups
HISTFILESIZE= HISTFILESIZE=
@ -38,87 +96,77 @@ bashrc_customize_environment() {
Blue="0;34" Blue_RGB="268BD2" # 4/4 blue 33 #0087ff 55 -10 -45 38 139 210 205 82 82 Blue="0;34" Blue_RGB="268BD2" # 4/4 blue 33 #0087ff 55 -10 -45 38 139 210 205 82 82
Cyan="0;36" Cyan_RGB="2AA198" # 6/6 cyan 37 #00afaf 60 -35 -05 42 161 152 175 74 63 Cyan="0;36" Cyan_RGB="2AA198" # 6/6 cyan 37 #00afaf 60 -35 -05 42 161 152 175 74 63
Green="0;32" Green_RGB="859900" # 2/2 green 64 #5f8700 60 -20 65 133 153 0 68 100 60 Green="0;32" Green_RGB="859900" # 2/2 green 64 #5f8700 60 -20 65 133 153 0 68 100 60
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM=verbose
PS2="... "
PROMPT_COMMAND=bashrc_set_prompt
bashrc_set_prompt() {
local exit_code=$?
local level=$SHLVL
local prompt=$(printf '\$%.0s' $(seq 1 $level))
local pyvenv=""
if ! [ -z "$VIRTUAL_ENV" ]; then
pyvenv=" "$(basename "$VIRTUAL_ENV" 2>/dev/null)
fi
local stopped_jobs="" running_jobs=""
local running=$(jobs -r | wc -l) stopped=$(jobs -s | wc -l)
if [ ${running} -gt 0 ]; then running_jobs=" run:${running}"; fi
if [ ${stopped} -gt 0 ]; then stopped_jobs=" stp:${stopped}"; fi
local color="$Cyan"
if [ $EUID -eq 0 ]; then
# root user
prompt=$(printf '#%.0s' $(seq 1 $level))
color="$Orange"
elif [ -n "$SSH_CLIENT" ]; then
# SSH connection
color="$Yellow"
fi
local user_host_color="\[\033[${color}m\]"
local pwd_color="\[\033[${Blue}m\]"
local exit_code_color="\[\033[${Red}m\]"
local git_color="\[\033[${Green}m\]"
local env_color="\[\033[${Magenta}m\]"
local running_color="\[\033[${Orange}m\]"
local stopped_color="\[\033[${Orange}m\]"
local default_color="\[\033[0m\]"
PS1="\n[" # [
PS1+="$user_host_color\u@\h " # user @ host
PS1+="$pwd_color\w" # pwd
PS1+="$git_color$(__git_ps1 ' %s')" # git status (if in repo)
PS1+="$env_color$pyvenv" # python virtual env (if active)
PS1+="$running_color$running_jobs" # background running jobs (if any)
PS1+="$stopped_color$stopped_jobs" # background stopped jobs (if any)
PS1+="$default_color" # back to default color
PS1+="]\n" # ]
if [[ $exit_code != 0 ]]; then
PS1+="$exit_code_color$exit_code " # last exit code if non-zero
PS1+="$default_color" # back to default color
fi
PS1+="$prompt " # prompt
} }
bashrc_customize_shell_options() { ##############################################################################
# Prevent overwriting files by mistake with output redirection. # Customize shell aliases
set -o noclobber ##############################################################################
shopt -s cdspell checkwinsize globstar histappend nocaseglob
}
bashrc_customize_paths() {
# Find out where Homebrew performs installations. If Homebrew is not
# installed (e.g. running on Linux), assume /usr/local for our
# installations.
local prefix=/usr/local
if command -v brew &>/dev/null; then
prefix=$(brew --prefix)
fi
# Prevent path_helper from messing with the PATH when starting tmux.
# See: https://superuser.com/a/583502
if [ "$(uname)" == "Darwin" ]; then
PATH=""
source /etc/profile
fi
# Add custom bin dirs to PATH if they exist and are not already in PATH.
local p
while read p; do
if [ -d "$p" ] && [[ ":$PATH:" != *":$p:"* ]]; then
PATH="$p:$PATH"
fi
done <<EOS
$prefix/bin
$prefix/opt/man-db/libexec/bin
$prefix/opt/coreutils/libexec/gnubin
$prefix/opt/gnu-sed/libexec/gnubin
$HOME/.local/bin
$HOME/bin
EOS
# Prepend custom man directories to MANPATH if they exist, so that we get
# correct man page entries when multiple versions of a command are
# available.
export MANPATH=$(MANPATH= manpath)
while read p; do
if [ -d "$p" ] && [[ ":$MANPATH:" != *":$p:"* ]]; then
MANPATH="$p:$MANPATH"
fi
done <<EOS
$prefix/share/man
$prefix/opt/man-db/libexec/man
$prefix/opt/coreutils/libexec/gnuman
$prefix/opt/gnu-sed/libexec/gnuman
$HOME/.local/share/man
EOS
}
bashrc_update_colors() {
export BACKGROUND="$1"
if [ -n "$TMUX" ] && [ -f "$HOME/.tmux.conf" ]; then
tmux set-environment -g BACKGROUND "$BACKGROUND"
tmux source-file "$HOME/.tmux.conf"
fi
bashrc_customize_terminal_colors
bashrc_customize_prompt
bashrc_customize_ls
}
bashrc_customize_aliases() {
alias light='bashrc_update_colors "light"'
alias dark='bashrc_update_colors "dark"'
# Make `ls` group directories first if supported. # Make `ls` group directories first if supported.
if ls --group-directories-first >/dev/null 2>&1; then if ls --group-directories-first &>/dev/null; then
alias ls="ls -hF --group-directories-first --color=auto" # GNU alias ls="ls -hF --group-directories-first --color=auto" # GNU
else else
alias ls="ls -hF -G" # BSD alias ls="ls -hF -G" # BSD
fi fi
# Force `ls` to use color output (e.g. for piping into `less`). # Force `ls` to use color output (e.g. for piping into `less`).
if ls --color=auto >/dev/null 2>&1; then if ls --color=auto &>/dev/null; then
alias lsc="ls --color=always" # GNU alias lsc="ls --color=always" # GNU
else else
alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" # BSD alias lsc="/usr/bin/env CLICOLOR_FORCE=1 ls" # BSD
@ -135,6 +183,7 @@ bashrc_customize_aliases() {
alias fgrep="fgrep --color=auto"; alias fgrep="fgrep --color=auto";
alias path='echo $PATH | tr -s ":" "\n"' alias path='echo $PATH | tr -s ":" "\n"'
alias mpath='echo $MANPATH | tr -s ":" "\n"' alias mpath='echo $MANPATH | tr -s ":" "\n"'
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'
# A few options to get public IP address on command line. The dig solution # A few options to get public IP address on command line. The dig solution
# below using the OpenDNS resolver doesn't work when connected to # below using the OpenDNS resolver doesn't work when connected to
@ -144,21 +193,16 @@ bashrc_customize_aliases() {
#alias myip="curl https://ifconfig.me" #alias myip="curl https://ifconfig.me"
#alias myip="dig +short myip.opendns.com @resolver1.opendns.com" #alias myip="dig +short myip.opendns.com @resolver1.opendns.com"
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date' alias light='bashrc_update_colors light'
alias tree="bashrc_tree" alias dark='bashrc_update_colors dark'
alias ltree="bashrc_paged_tree" bashrc_update_colors() {
} export BACKGROUND="$1"
bashrc_tree() { if [ -n "$TMUX" ] && [ -f "$HOME/.tmux.conf" ]; then
# The 'tree' alias is not available here, so this calls the actual program. tmux set-environment -g BACKGROUND "$BACKGROUND"
tree -F --dirsfirst -I '.git|Spotlight-V100|.fseventsd' "$@" tmux source-file "$HOME/.tmux.conf"
} fi
bashrc_paged_tree() {
bashrc_tree -C "$@" | less -R
}
bashrc_customize_terminal_colors() {
# Format string for sending an OSC (Operating System Commmand) to the terminal. # Format string for sending an OSC (Operating System Commmand) to the terminal.
if [ -n "$TMUX" ]; then if [ -n "$TMUX" ]; then
# http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324 # http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324
@ -212,84 +256,50 @@ bashrc_customize_terminal_colors() {
printf "$format" "11;#$background" # Background printf "$format" "11;#$background" # Background
printf "$format" "12;#$Red_RGB" # Cursor printf "$format" "12;#$Red_RGB" # Cursor
fi fi
}
bashrc_set_prompt() {
local exit_code=$?
local level=$SHLVL
local prompt=$(printf '\$%.0s' $(seq 1 $level))
local pyvenv=""
if ! [ -z "$VIRTUAL_ENV" ]; then
pyvenv=" "$(basename "$VIRTUAL_ENV" 2>/dev/null)
fi
local stopped_jobs="" running_jobs=""
local running=$(jobs -r | wc -l) stopped=$(jobs -s | wc -l)
if [ ${running} -gt 0 ]; then running_jobs=" run:${running}"; fi
if [ ${stopped} -gt 0 ]; then stopped_jobs=" stp:${stopped}"; fi
local color="$Cyan"
if [ $EUID -eq 0 ]; then
# root user
prompt=$(printf '#%.0s' $(seq 1 $level))
color="$Orange"
elif [ -n "$SSH_CLIENT" ]; then
# SSH connection
color="$Yellow"
fi
local user_host_color="\[\033[${color}m\]"
local pwd_color="\[\033[${Blue}m\]"
local exit_code_color="\[\033[${Red}m\]"
local git_color="\[\033[${Green}m\]"
local env_color="\[\033[${Magenta}m\]"
local running_color="\[\033[${Orange}m\]"
local stopped_color="\[\033[${Orange}m\]"
local default_color="\[\033[0m\]"
PS1="\n[" # [
PS1+="$user_host_color\u@\h " # user @ host
PS1+="$pwd_color\w" # pwd
PS1+="$git_color$(__git_ps1 ' %s')" # git status (if in repo)
PS1+="$env_color$pyvenv" # python virtual env (if active)
PS1+="$running_color$running_jobs" # background running jobs (if any)
PS1+="$stopped_color$stopped_jobs" # background stopped jobs (if any)
PS1+="$default_color" # back to default color
PS1+="]\n" # ]
if [[ $exit_code != 0 ]]; then
PS1+="$exit_code_color$exit_code " # last exit code if non-zero
PS1+="$default_color" # back to default color
fi
PS1+="$prompt " # prompt
}
bashrc_customize_prompt() {
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="verbose"
PROMPT_COMMAND="bashrc_set_prompt"
PS2=". "
}
bashrc_customize_ls() {
local ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND" local ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND"
if type dircolors &>/dev/null && [ -f $ls_colors ]; then if type dircolors &>/dev/null && [ -f $ls_colors ]; then
eval "$(dircolors $ls_colors)" eval "$(dircolors $ls_colors)"
fi fi
} }
bashrc_source_completion_helpers() { alias tree="bashrc_tree"
if [ -d /usr/local/etc/bash_completion.d ]; then bashrc_tree() {
local f # The 'tree' alias is not available here, so this calls the actual program.
for f in /usr/local/etc/bash_completion.d/*; do tree -F --dirsfirst -I '.git|Spotlight-V100|.fseventsd' "$@"
source "$f" }
done
fi alias ltree="bashrc_paged_tree"
bashrc_paged_tree() {
bashrc_tree -C "$@" | less -R
}
##############################################################################
# Add shell functions
##############################################################################
# Combined mkdir and cd
mkcd() { mkdir -p -- "$1" && cd -P -- "$1"; }
# Colorized `man`
man() {
if [ "$BACKGROUND" = "dark" ]; then
local standout="$Base02;44" bold="$Yellow" underline="$Base3;4"
else
local standout="$Base02;46" bold="$Blue" underline="$Base00;4"
fi
env \
LESS_TERMCAP_so=$(echo -ne "\033[${standout}m") \
LESS_TERMCAP_md=$(echo -ne "\033[${bold}m") \
LESS_TERMCAP_us=$(echo -ne "\033[${underline}m") \
LESS_TERMCAP_se=$'\033[0m' \
LESS_TERMCAP_me=$'\033[0m' \
LESS_TERMCAP_ue=$'\033[0m' \
GROFF_NO_SGR=1 \
man "$@"
} }
# Print the solarized palette (for testing)
solarized() { solarized() {
local names=(Base02 Red Green Yellow Blue Magenta Cyan Base2 local names=(Base02 Red Green Yellow Blue Magenta Cyan Base2
Base03 Orange Base01 Base00 Base0 Violet Base1 Base3) Base03 Orange Base01 Base00 Base0 Violet Base1 Base3)
@ -315,38 +325,19 @@ colortest() {
printf '\033[0m' printf '\033[0m'
} }
# Combined mkdir and cd ##############################################################################
mkcd() { mkdir -p -- "$1" && cd -P -- "$1"; } # Run external cusomizations
##############################################################################
# Colorized `man`
man() {
if [ "$BACKGROUND" = "dark" ]; then
local standout="$Base02;44" bold="$Yellow" underline="$Base3;4"
else
local standout="$Base02;46" bold="$Blue" underline="$Base00;4"
fi
env \
LESS_TERMCAP_so=$(echo -ne "\033[${standout}m") \
LESS_TERMCAP_md=$(echo -ne "\033[${bold}m") \
LESS_TERMCAP_us=$(echo -ne "\033[${underline}m") \
LESS_TERMCAP_se=$'\033[0m' \
LESS_TERMCAP_me=$'\033[0m' \
LESS_TERMCAP_ue=$'\033[0m' \
GROFF_NO_SGR=1 \
man "$@"
}
# Apply customizations
stty -ixon # disable ctrl-s and ctrl-q stty -ixon # disable ctrl-s and ctrl-q
bashrc_customize_environment bashrc_update_colors "${BACKGROUND:-dark}"
bashrc_customize_shell_options
bashrc_customize_paths # Enable available completion helpers
bashrc_customize_aliases if [ -d /usr/local/etc/bash_completion.d ]; then
bashrc_customize_terminal_colors for completion in /usr/local/etc/bash_completion.d/*; do
bashrc_customize_prompt source "$completion"
bashrc_customize_ls done
bashrc_source_completion_helpers fi
# Source a local bashrc if available # Source a local bashrc if available
if [ -f "$HOME/.bashrc.local" ]; then if [ -f "$HOME/.bashrc.local" ]; then