diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-21 21:09:15 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-21 21:25:15 +1200 |
commit | 7ddb8084297c5bf30db73e9b9058d71f52991901 (patch) | |
tree | 8b07077307ae6209d9e094270dbcc65b5e7e80c7 /bash | |
parent | Restructure ed() to test,cache features on login (diff) | |
download | dotfiles-7ddb8084297c5bf30db73e9b9058d71f52991901.tar.gz dotfiles-7ddb8084297c5bf30db73e9b9058d71f52991901.zip |
Many stylistic fixes/optimisations
In particular, limit the verbose >=2.05 test to just one file: .bashrc
Diffstat (limited to 'bash')
-rw-r--r-- | bash/bash_logout | 14 | ||||
-rw-r--r-- | bash/bash_profile | 21 | ||||
-rw-r--r-- | bash/bashrc | 24 | ||||
-rw-r--r-- | bash/bashrc.d/completion.bash | 3 | ||||
-rw-r--r-- | bash/bashrc.d/keep.bash | 15 | ||||
-rw-r--r-- | bash/bashrc.d/prompt.bash | 47 |
6 files changed, 34 insertions, 90 deletions
diff --git a/bash/bash_logout b/bash/bash_logout index afb088b8..911b4f66 100644 --- a/bash/bash_logout +++ b/bash/bash_logout @@ -1,14 +1,2 @@ -# Ensure we're using at least version 2.05. Weird arithmetic syntax needed here -# due to leading zeroes and trailing letters in some 2.x version numbers (e.g. -# 2.05a). -if ! [ -n "$BASH_VERSINFO" ] ; then - return -elif ((BASH_VERSINFO[0] == 2)) && - ((10#${BASH_VERSINFO[1]%%[![:digit:]]*} < 5)) ; then - return -fi - # Clear console if possible when logging out -if ((SHLVL == 1)) ; then - clear_console -q 2>/dev/null -fi +[ "$SHLVL" = 1 ] && clear_console -q 2>/dev/null diff --git a/bash/bash_profile b/bash/bash_profile index 69350102..69e812a0 100644 --- a/bash/bash_profile +++ b/bash/bash_profile @@ -1,19 +1,6 @@ # Load ~/.profile regardless of shell version -if [ -e "$HOME"/.profile ] ; then - . "$HOME"/.profile -fi +[ -e "$HOME"/.profile ] && . "$HOME"/.profile -# Ensure we're using at least version 2.05. Weird arithmetic syntax needed here -# due to leading zeroes and trailing letters in some 2.x version numbers (e.g. -# 2.05a). -if ! [ -n "$BASH_VERSINFO" ] ; then - return -elif ((BASH_VERSINFO[0] == 2)) && - ((10#${BASH_VERSINFO[1]%%[![:digit:]]*} < 5)) ; then - return -fi - -# If ~/.bashrc exists, source that too; the test for interactivity is in there -if [[ -f $HOME/.bashrc ]] ; then - source "$HOME"/.bashrc -fi +# If ~/.bashrc exists, source that too; the tests for both interactivity and +# >=2.05a (for features like [[) are in there +[ -f $HOME/.bashrc ] && . "$HOME"/.bashrc diff --git a/bash/bashrc b/bash/bashrc index e7973562..c2055feb 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -1,12 +1,10 @@ # Ensure we're using at least version 2.05. Weird arithmetic syntax needed here # due to leading zeroes and trailing letters in some 2.x version numbers (e.g. # 2.05a). -if ! [ -n "$BASH_VERSINFO" ] ; then +[ -n "$BASH_VERSINFO" ] || return +((BASH_VERSINFO[0] == 2)) && + ((10#${BASH_VERSINFO[1]%%[![:digit:]]*} < 5)) && return -elif ((BASH_VERSINFO[0] == 2)) && - ((10#${BASH_VERSINFO[1]%%[![:digit:]]*} < 5)) ; then - return -fi # Make sure the shell is interactive case $- in @@ -15,9 +13,7 @@ case $- in esac # Don't do anything if running a restricted shell -if shopt -q restricted_shell ; then - return -fi +shopt -q restricted_shell && return # Keep around four thousand lines of history in file HISTFILESIZE=$((1 << 12)) @@ -89,22 +85,16 @@ if ((BASH_VERSINFO[0] >= 4)) ; then # Warn me about stopped jobs when exiting; only if >=4.1 due to bug # <https://lists.gnu.org/archive/html/bug-bash/2009-02/msg00176.html> - if ((BASH_VERSINFO[1] >= 1)) ; then - shopt -s checkjobs - fi + ((BASH_VERSINFO[1] >= 1)) && shopt -s checkjobs # Expand variables in directory completion; only available since 4.3 - if ((BASH_VERSINFO[1] >= 3)) ; then - shopt -s direxpand - fi + ((BASH_VERSINFO[1] >= 3)) && shopt -s direxpand fi # If COMP_WORDBREAKS has a value, strip all colons from it; this allows # completing filenames correctly, since an unquoted colon is not a syntactic # character: <http://tiswww.case.edu/php/chet/bash/FAQ> (E13) -if [[ -n $COMP_WORDBREAKS ]] ; then - COMP_WORDBREAKS=${COMP_WORDBREAKS//:} -fi +[[ -n $COMP_WORDBREAKS ]] && COMP_WORDBREAKS=${COMP_WORDBREAKS//:} # Load POSIX shell functions, Bash-specific scripts, and Bash completion files, # in that order diff --git a/bash/bashrc.d/completion.bash b/bash/bashrc.d/completion.bash index c120878d..1246ba31 100644 --- a/bash/bashrc.d/completion.bash +++ b/bash/bashrc.d/completion.bash @@ -40,8 +40,11 @@ complete -A function -A variable unset # If we have dynamic completion loading (Bash>=4.0), use it if ((BASH_VERSINFO[0] >= 4)) ; then + + # Handler tries to load appropriate completion for commands _completion_loader() { [[ -n $1 ]] || return + local compspec compspec=$HOME/.bash_completion.d/$1.bash [[ -f $compspec ]] || return source "$compspec" >/dev/null 2>&1 && return 124 diff --git a/bash/bashrc.d/keep.bash b/bash/bashrc.d/keep.bash index b1c8bea4..fb4b8bde 100644 --- a/bash/bashrc.d/keep.bash +++ b/bash/bashrc.d/keep.bash @@ -45,9 +45,7 @@ keep() { # -h given; means show help h) - while IFS= read -r line ; do - printf '%s\n' "$line" - done <<EOF + cat <<EOF $FUNCNAME: Keep variables and functions in shell permanently by writing them to named scripts iterated on shell start, in \$BASHKEEP (defaults to ~/.bashkeep.d). @@ -145,10 +143,7 @@ EOF } # Load any existing scripts in bashkeep -if [[ -d ${BASHKEEP:-"$HOME"/.bashkeep.d} ]] ; then - for bashkeep in "${BASHKEEP:-"$HOME"/.bashkeep.d}"/*.bash ; do - [[ -e $bashkeep ]] || continue - source "$bashkeep" - done - unset -v bashkeep -fi +for bashkeep in "${BASHKEEP:-"$HOME"/.bashkeep.d}"/*.bash ; do + [[ -e $bashkeep ]] && source "$bashkeep" +done +unset -v bashkeep diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index 0264b7f9..e61b69d5 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -1,12 +1,6 @@ # Frontend to controlling prompt prompt() { - # If no arguments, print the prompt strings as they are - if ! (($#)) ; then - declare -p PS1 PS2 PS3 PS4 - return - fi - # What's done next depends on the first argument to the function case $1 in @@ -19,9 +13,7 @@ prompt() { PROMPT_COMMAND='PROMPT_RETURN=$? ; history -a' # If Bash 4.0 is available, trim very long paths in prompt - if ((BASH_VERSINFO[0] >= 4)) ; then - PROMPT_DIRTRIM=4 - fi + ((BASH_VERSINFO[0] >= 4)) && PROMPT_DIRTRIM=4 # Basic prompt shape PS1='\u@\h:\w' @@ -101,11 +93,8 @@ prompt() { git) # Bail if we're not in a work tree--or, implicitly, if we don't # have git(1). - local iswt - iswt=$(git rev-parse --is-inside-work-tree 2>/dev/null) - if [[ $iswt != true ]] ; then - return 1 - fi + [[ $(git rev-parse --is-inside-work-tree 2>/dev/null) = true ]] || + return # Attempt to determine git branch, bail if we can't local branch @@ -113,9 +102,7 @@ prompt() { git symbolic-ref --quiet HEAD || git rev-parse --short HEAD } 2>/dev/null ) - if [[ ! -n $branch ]] ; then - return 1 - fi + [[ -n $branch ]] || return branch=${branch##*/} # Refresh index so e.g. git-diff-files(1) is accurate @@ -198,12 +185,8 @@ prompt() { # Add appropriate state flags local -a state - if ((modified)) ; then - state[${#state[@]}]='!' - fi - if ((untracked)) ; then - state[${#state[@]}]='?' - fi + ((modified)) && state[${#state[@]}]='!' + ((untracked)) && state[${#state[@]}]='?' # Print the state in brackets with an svn: prefix (IFS= ; printf '(svn:%s%s)' \ @@ -214,17 +197,13 @@ prompt() { vcs) local vcs for vcs in "${PROMPT_VCS[@]:-git}" ; do - if prompt "$vcs" ; then - return - fi + prompt "$vcs" && return done ;; # Show return status of previous command in angle brackets, if not zero ret) - if ((PROMPT_RETURN > 0)) ; then - printf '<%u>' "$PROMPT_RETURN" - fi + ((PROMPT_RETURN)) && printf '<%u>' "$PROMPT_RETURN" ;; # Show the count of background jobs in curly brackets, if not zero @@ -233,9 +212,12 @@ prompt() { while read ; do ((jobc++)) done < <(jobs -p) - if ((jobc > 0)) ; then - printf '{%u}' "$jobc" - fi + ((jobc)) && printf '{%u}' "$jobc" + ;; + + # No argument given, print prompt strings and vars + '') + declare -p PS1 PS2 PS3 PS4 ;; # Print error @@ -243,7 +225,6 @@ prompt() { printf '%s: Unknown command %s\n' "$FUNCNAME" "$1" >&2 return 2 ;; - esac } |