diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2013-09-01 00:48:44 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2013-09-01 00:48:44 +1200 |
commit | 11a6ae1b208691b9f894ef172c267004cace81e6 (patch) | |
tree | 128344a8d6cea9bee9add353cb75b6c025f68c40 /bash | |
parent | Fix incorrect array index for VCS states (diff) | |
download | dotfiles-11a6ae1b208691b9f894ef172c267004cace81e6.tar.gz dotfiles-11a6ae1b208691b9f894ef172c267004cace81e6.zip |
More sensible use of local
Don't declare integers/arrays, just use them. Also includes a minor
scope fix -- don't need to count number of colors on every call to
prompt(), just for `prompt on`.
Diffstat (limited to 'bash')
-rw-r--r-- | bash/bashrc.d/cd.bash | 3 | ||||
-rw-r--r-- | bash/bashrc.d/grep.bash | 8 | ||||
-rw-r--r-- | bash/bashrc.d/ls.bash | 8 | ||||
-rw-r--r-- | bash/bashrc.d/prompt.bash | 44 | ||||
-rw-r--r-- | bash/bashrc.d/ssh.bash | 8 |
5 files changed, 38 insertions, 33 deletions
diff --git a/bash/bashrc.d/cd.bash b/bash/bashrc.d/cd.bash index edc5c6f1..0f1eba24 100644 --- a/bash/bashrc.d/cd.bash +++ b/bash/bashrc.d/cd.bash @@ -1,8 +1,7 @@ # If given two arguments to cd, replace the first with the second in $PWD, # emulating a Zsh function that I often find useful; preserves options too cd() { - local OPTIND=0 opt - local -a opts + local opt opts OPTIND=0 while getopts elP opt; do opts[${#opts[@]}]=-$opt done diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash index 5ba77ffd..dc26e80e 100644 --- a/bash/bashrc.d/grep.bash +++ b/bash/bashrc.d/grep.bash @@ -1,12 +1,12 @@ # Function returns calculated options for grep grepopts() { - # Declare options array - local -a grepopts - # Snarf the output of `grep --help` into a variable local grephelp=$(grep --help 2>/dev/null) + # Start collecting available options + local grepopts=() + # Add option to ignore binary files grepopts[${#grepopts[@]}]='-I' @@ -22,7 +22,7 @@ grepopts() { # If the --color option is available and we have a terminal that supports # at least eight colors, add --color=auto to the options - local -i colors=$(tput colors) + local colors=$(tput colors) if [[ $grephelp == *--color* ]] && ((colors >= 8)); then grepopts[${#grepopts[@]}]='--color=auto' fi diff --git a/bash/bashrc.d/ls.bash b/bash/bashrc.d/ls.bash index e74d48d8..a67194b5 100644 --- a/bash/bashrc.d/ls.bash +++ b/bash/bashrc.d/ls.bash @@ -1,15 +1,15 @@ # Function returns calculated options for ls lsopts() { - # Declare options array - local -a lsopts - # Snarf the output of `ls --help` into a variable local lshelp=$(ls --help 2>/dev/null) + # Start collecting available options + local lsopts=() + # If the --color option is available and we have a terminal that supports # at least eight colors, add --color=auto to the options - local -i colors=$(tput colors) + local colors=$(tput colors) if [[ $lshelp == *--color* ]] && ((colors >= 8)); then lsopts[${#lsopts[@]}]='--color=auto' fi diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index 046c88b8..ea336d9a 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -1,30 +1,28 @@ # Frontend to controlling prompt prompt() { - # Variables for use only within this function - local color reset branch info url root - local -i colors=$(tput colors) - local -a state=() - # What's done next depends on the first argument to the function case $1 in # Turn complex, colored prompt on on) + # Set up pre-prompt command and prompt format PROMPT_COMMAND='ret=$? ; history -a' PS1='\[\a\][\u@\h:\w]$(prompt vcs)$(prompt job)$(prompt ret)\$' + # Count available colors + local colors=$(tput colors) + # Check if we have non-bold bright green available + local color if ((colors > 8)); then color=$(tput setaf 10) - - # If we don't, fall back to the bold green else color=$(tput setaf 2)$(tput bold) fi # Reset color and attributes - reset=$(tput sgr0) + local reset=$(tput sgr0) # String it all together PS1="\\[$color\\]$PS1\\[$reset\\] " @@ -38,16 +36,15 @@ prompt() { # Git prompt function git) - # Exit if inside a .git directory - local isgd=$(git rev-parse --is-inside-git-dir 2>/dev/null) - if [[ $isgd == true ]]; then + local gitdir=$(git rev-parse --is-inside-git-dir 2>/dev/null) + if [[ $gitdir == true ]]; then return 1 fi # Exit if not inside a working tree - local iswt=$(git rev-parse --is-inside-work-tree 2>/dev/null) - if [[ $iswt != true ]]; then + local worktree=$(git rev-parse --is-inside-work-tree 2>/dev/null) + if [[ $worktree != true ]]; then return 1 fi @@ -59,11 +56,15 @@ prompt() { # Figure out the branch to show for HEAD, whether a symbolic # reference or a short SHA-1; chop off any leading path + local branch branch=$(git symbolic-ref --quiet HEAD 2>/dev/null) \ || branch=$(git rev-parse --short HEAD 2>/dev/null) \ || branch='unknown' branch=${branch##*/} + # Start collecting working copy state flags + local state=() + # If there are staged changes in the working tree, add a plus sign # to the state if ! git diff --quiet --ignore-submodules --cached; then @@ -94,12 +95,15 @@ prompt() { # Mercurial prompt function hg) - # Exit if not inside a Mercurial tree + local branch if ! branch=$(hg branch 2>/dev/null); then return 1 fi + # Start collecting working copy state flags + local state=() + # If there are changes in the tree, add an exclamation mark to the # state if [[ -n $(hg status 2>/dev/null) ]]; then @@ -113,25 +117,28 @@ prompt() { # Subversion prompt function svn) - # Exit if not inside a Subversion working copy if ! svn info &>/dev/null; then return 1 fi # Determine the repository URL and root directory - info=$(svn info 2>/dev/null) - url=$(awk -F': ' '$1 == "URL" {print $2}' <<<"$info") - root=$(awk -F': ' '$1 == "Repository Root" {print $2}' <<<"$info") + local info=$(svn info 2>/dev/null) + local url=$(awk -F': ' '$1 == "URL" {print $2}' <<<"$info") + local root=$(awk -F': ' '$1 == "Repository Root" {print $2}' <<<"$info") # Remove the root from the URL to get what's hopefully the branch # name, removing leading slashes and the 'branches' prefix, and any # trailing content after a slash + local branch branch=${url/$root} branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} + # Start collecting working copy state flags + local state=() + # If there are changes in the working directory, add an exclamation # mark to the state if [[ -n $(svn status 2>/dev/null) ]]; then @@ -153,7 +160,6 @@ prompt() { if ((ret > 0)); then printf '<%d>' "$ret" fi - unset ret ;; # Show the count of background jobs in curly brackets diff --git a/bash/bashrc.d/ssh.bash b/bash/bashrc.d/ssh.bash index 76b77cec..e4ca1dec 100644 --- a/bash/bashrc.d/ssh.bash +++ b/bash/bashrc.d/ssh.bash @@ -1,9 +1,8 @@ # Commpletion for ssh/sftp/ssh-copy-id with config hostnames _ssh() { - local word config hosts option value - word=${COMP_WORDS[COMP_CWORD]} - config=$HOME/.ssh/config - hosts=() + local config=$HOME/.ssh/config + local hosts=() + local word=${COMP_WORDS[COMP_CWORD]} # Bail if the configuration file is illegible if [[ ! -r $config ]]; then @@ -11,6 +10,7 @@ _ssh() { fi # Read hostnames from the file, no asterisks + local option value while read -r option value _; do if [[ $option == Host && $value != *'*'* ]]; then hosts[${#hosts[@]}]=$value |