view rc/bashrc @ 668:b535a060087b

A note on srcery color modification
author nanaya <me@nanaya.pro>
date Sat, 11 Dec 2021 19:03:39 +0900
parents 35c26d9dcaa7
children 71254a6ddb4c
line wrap: on
line source

#!/usr/bin/env bash

case "${-}" in
  *i*) ;;
  *) return;;
esac

if [ "${EC_BASH_LOADED}" != "yes" ]; then
  EC_BASH_LOADED="yes"
else
  return
fi

[ -f "${HOME}/.ec.bash.before" ] && . "${HOME}/.ec.bash.before"

_org_path="${PATH}"

# ensure correct window size at all time
# noticeably in FreebSD + tmux + vim:
# 1. start tmux
# 2. start vim in the session
# 3. split pane
# 4. quit vim
# 5. wrong COLUMNS or LINES
shopt -s checkwinsize

set +H

# Basic PATH
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/games"

_os="$(uname -s)"
_os_r="$(uname -r)"

# pkgsrc
if test -d /usr/pkg; then
  PATH="${PATH}:/usr/pkg/bin:/usr/pkg/sbin"
  if test -d /usr/pkg/java/openjdk7; then
    # OpenJDK 7 in pkgsrc
    PATH="${PATH}:/usr/pkg/java/openjdk7/bin"
  fi
fi

# OpenBSD X11
test -d /usr/X11R6/bin && PATH="${PATH}:/usr/X11R6/bin"
# PostgreSQL Yum
# Defaults to last version available.
for _pgbindir in /usr/pgsql-*/bin; do test; done
test -d "${_pgbindir}" && PATH="${_pgbindir}:${PATH}"
unset _pgbindir

# PostgreSQL EnterpriseDB
# Defaults to last version available.
for _pgbindir in /opt/PostgreSQL/*/bin; do test; done
test -d "${_pgbindir}" && PATH="${_pgbindir}:${PATH}"
unset _pgbindir

# Nuke currently defined functions
unset -f $(set | grep ' \(\)$' | cut -d ' ' -f 1)

# Nuke bash-completion
complete -r

# Nuke aliases
unalias -a

shopt -s histappend
HISTSIZE=20000
HISTFILESIZE=$HISTSIZE
HISTCONTROL=ignorespace:ignoredups:erasedups
PROMPT_COMMAND='history -a; history -c; history -r'

export PAGER=more
export EDITOR=vi
export LANG="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
# The en_US.UTF-8 causes weird sorting.
# (not really, but I prefer C based sort.)
export LC_COLLATE=C
export LC_MESSAGES=C
export CLICOLOR=
export LSCOLORS="gxfxcxdxbxegedabagacad"
export LS_COLORS=
export BLOCKSIZE=1K
export BLOCK_SIZE=si

alias ls="ls -F"
alias rd="rdesktop -g 1280x600 -K -a 15 -x m -z -P -r sound:off -r clipboard:CLIPBOARD -5"
alias man="LANG=C LC_ALL=C LC_CTYPE=C man"
alias be="bundle exec"
alias ll="ls -l"
alias la="ls -A"

_has_gnu_ls=
_has_gnu_grep=
_ls="ls"
_grep="grep"
_has_posix_grep=y

if [ SunOS = "${_os}" ]; then
  if [ -f /usr/share/lib/terminfo/s/screen ]; then
    export TERMINFO=/usr/share/lib/terminfo
  elif [ -f /opt/csw/share/terminfo/s/screen ]; then
    export TERMINFO=/opt/csw/share/terminfo
  else
    unset TERMINFO
    export TERM=dtterm
  fi

  [ xterm = "${TERM}" ] && export TERM=xterm-256color
fi

case "${_os}" in
  Linux)
    _has_gnu_ls=y
    _has_gnu_grep=y
  ;;
  SunOS)
    _has_posix_grep=n
    [ -x /usr/gnu/bin/ls ] && _has_gnu_ls=y
    [ -x /usr/gnu/bin/grep ] && _has_gnu_grep=y
    # Higher priority directories
    for i in {/opt/csw,/usr/gnu}/{,s}bin; do
      [ -d "${i}" ] && PATH="${i}:${PATH}"
    done
    for i in /usr/{sfw,xpg4}/bin; do
      [ -d "${i}" ] && PATH="${PATH}:${i}"
    done
    [ -d "/opt/csw/gnu" ] && PATH="/opt/csw/gnu:${PATH}"
    [ -d "/opt/local/sbin" ] && PATH="/opt/local/sbin:${PATH}"
    [ -d "/opt/local/bin" ] && PATH="/opt/local/bin:${PATH}"
    alias ping="ping -s"
    alias ping6="ping -A inet6"
    if command -v ggrep > /dev/null 2>&1; then
      _has_gnu_grep=y
      _grep="ggrep"
    fi
    if command -v gls > /dev/null 2>&1; then
      _has_gnu_ls=y
      _ls="gls"
    fi
  ;;
  FreeBSD)
    # Multi CPU awesomeness.
    alias top="top -P"
    # FreeBSD grep is GNU grep
    _has_gnu_grep=y
  ;;
  OpenBSD)
    # GNU ls is broken in 5.2. Prioritize colorls instead.
    if command -v colorls > /dev/null 2>&1; then
      alias ls="colorls -F"
    elif command -v gls > /dev/null 2>&1; then
      _has_gnu_ls=y
      _ls="gls"
    fi
  ;;
  NetBSD|DragonFly)
    if command -v gls > /dev/null 2>&1; then
      _has_gnu_ls=y
      _ls="gls"
    elif command -v colorls > /dev/null 2>&1; then
      alias ls="colorls -F"
    fi
    alias grep='grep --color=auto'
  ;;
  CYGWIN*)
    export PATH="${PATH}:${_org_path}"
  ;;
  MSYS_NT-*)
    _has_gnu_ls=y
    export PATH="${PATH}:${_org_path}"
  ;;
esac

if [ "${_has_gnu_ls}" = y ]; then
  alias ls="${_ls} -F --color=auto"
  LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:';
  export LS_COLORS
fi
if [ "${_has_gnu_grep}" = y ]; then
  alias grep="${_grep} --color=auto"
  _has_posix_grep=y

  alias cgrep="${_grep} --color=always"
  alias cegrep="cgrep -E"
  alias cfgrep="cgrep -F"
fi
if [ "${_has_posix_grep}" = y ]; then
  alias fgrep="grep -F"
  alias egrep="grep -E"
fi

# FIXME: missing test for hg and xargs -0
_xargs_grep="${_grep}"
[ "${_has_gnu_grep}" = y ] && _xargs_grep="${_grep} --color=always"
alias hggrep="hg locate -0 | xargs -0 ${_xargs_grep}"
unset _xargs_grep

unset _has_gnu_ls
unset _has_gnu_grep
unset _has_posix_grep
unset _ls
unset _grep

# Custom application I usually install and safe to prioritize
for i in {"${HOME}/apps","/opt"{,"/${USER:-$LOGNAME}"}}/{tmux,ruby19,python26,node,tarsnap,symon,nginx,p7zip,composer}/{bin,sbin}; do
  [ -d "${i}" ] && PATH="${i}:${PATH}"
done
# New(?) rubygems bin PATH
for i in {"${HOME}/apps","/opt"{,"/${USER:-$LOGNAME}"}}/ruby19/lib/ruby/gems/1.9.1/bin; do
  [ -d "${i}" ] && PATH="${i}:${PATH}"
done

# MySQL in common locations highest priority since new = better. Also no compatibility problem.
# Cluster is superset of Enterprise/GPL so it gets highest priority.
# And use only one.
for mydir in {"${HOME}",/apps,/opt}/mysql/{{cluster,enterprise}/mysql/,}bin; do
  if [ -d "${mydir}" ]; then
    PATH="${mydir}:${PATH}"
    break
  fi
done

# Mercurial is optional since it will be inconsistent with ssh ... hg if this one gets priority.
# And use only one.
for hgdir in {"${HOME}/apps","/opt"}/mercurial{,/bin}; do
  hgbin="${hgdir}/hg"
  if [ -x "${hgbin}" ]; then
    PATH="${PATH}:${hgdir}"
    alias nhg="${hgbin}"
    break
  fi
done

for supbindir in {"${HOME}/apps","/opt"}/supervisor{,d}/bin; do
  if [ -d "${supbindir}" ]; then
    PATH="${PATH}:${supbindir}"
    break
  fi
done

PS1='\u@\h:\w\$ '
case "${TERM}" in
  xterm*|screen*|dtterm*)
    PROMPT_COMMAND='echo -ne "\033]0;'"${USER:-${LOGNAME:-$USERNAME}}@$(uname -n)"': ${PWD}\007";'"${PROMPT_COMMAND}"
    if [ "$(id -u)" = "0" ]; then
      PS1='\[\e[1;31m\]\u@\h:\w\$\[\e[0m\] '
    elif [ "$(uname)" != "DragonFly" ]; then
      # DragonFly's Bash is a bit buggy.
      PS1='\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;32m\]\h\[\e[0m\]:\[\e[0;36m\]\w\[\e[0m\]\$ '
    fi
  ;;
esac

# Priority PATHs. Always enable.
PATH="${HOME}/bin:${HOME}/.ec.bin:/opt/local/bin:/opt/local/sbin:${PATH}"

if command -v vim > /dev/null 2>&1; then
  alias vi=vim
  export EDITOR=vim
fi

if command -v less > /dev/null 2>&1; then
  export PAGER=less
  export LESS=-FMRXginsz-2
fi

# Test if we have GNU rm prefixed by g (esp if on non-GNU system).
# Usually on old Solaris and {Net,Open,DragonFly}BSD.
if command -v grm > /dev/null 2>&1; then
  _rm="grm"
else
  _rm="rm"
fi
# Test if the specified rm has "-I" parameter since it's much better.
if "${_rm}" -If "/tmp/.nonexistent.$(date '+%Y%m%d%H%M%S')" 2> /dev/null; then
  alias rm="${_rm} -I"
else
  alias rm="${_rm} -i"
fi
unset _rm

if command -v curl > /dev/null 2>&1; then
  alias 0p="gzip | curl 'https://0paste.com/pastes.txt' -F 'paste[paste_gzip]=@-'"
  alias 0pp="gzip | curl 'https://0paste.com/pastes.txt' -F 'paste[is_private]=1' -F 'paste[paste_gzip]=@-'"
fi

if command -v tmux > /dev/null 2>&1; then
  case "${_os}" in
  OpenBSD)
    case "${_os_r}" in
    6.[5-9]|[7-9].[0-9]|[1-9][0-9][0-9]*.[0-9])
      _tmux_conf_ver=2.9
    ;;
    *)
      # probably not accurate enough but I don't want to dig through all the previous versions.
      _tmux_conf_ver=2.2
    ;;
    esac
  ;;
  *)
    case "$(tmux -V 2> /dev/null)" in
    "tmux 2.9"|"tmux 2.9"[a-z]|"tmux "[3-9].[0-9]|"tmux "[3-9].[0-9][a-z]|"tmux "[1-9][0-9].[0-9]|"tmux "[1-9][0-9].[0-9][a-z]) _tmux_conf_ver=2.9;;
    "tmux 2."[2-8]) _tmux_conf_ver=2.2;;
    "tmux 1.9"|"tmux 2."[0-1]) _tmux_conf_ver=1.9;;
    "tmux 1."[4-8]) _tmux_conf_ver=1.4;;
    *) _tmux_conf_ver=1.3;;
    esac
  ;;
  esac
  alias tmux="tmux -f '""${HOME}""/.tmux-""${_tmux_conf_ver}"".conf'"
  unset _tmux_conf_ver
fi

unset _os
unset _os_r

# reset cursor shape
# reference: https://conemu.github.io/en/AnsiEscapeCodes.html#Terminal_modes
printf '\033[2 q'

[ -f "${HOME}/.ec.bash.after" ] && . "${HOME}/.ec.bash.after"