From 8af8a11034816f54dec1332ac43e44320db9cb48 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 31 Jul 2013 17:46:34 +1200 Subject: Improve comments on shell scripts --- bash/bashrc.d/ls.bash | 2 +- bash/bashrc.d/prompt.bash | 53 ++++++++++++++++++++++++++++++++++++++++++++--- bash/bashrc.d/tmux.bash | 6 ++++++ sh/profile | 4 ++-- sh/profile.d/keychain.sh | 4 +++- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/bash/bashrc.d/ls.bash b/bash/bashrc.d/ls.bash index 6ce1d750..7395224d 100644 --- a/bash/bashrc.d/ls.bash +++ b/bash/bashrc.d/ls.bash @@ -14,7 +14,7 @@ alias ls="ls $(__lsopts)" # Unset helper function unset __lsopts -# Apply color and exclusions to ls and grep +# Define and store appropriate colors for ls if command -v dircolors >/dev/null; then eval "$(dircolors --sh)" fi diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index 1be284a7..a648fe94 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -1,9 +1,12 @@ # Frontend to controlling prompt prompt() { + + # Variables for use only within this function local -i ret=$? local -i colors=$(tput colors 2>/dev/null) local color reset branch state info url root + # Figure out how many colors we have to work with if [[ $colors -ge 256 ]]; then color='\[\e[38;5;10m\]' reset='\[\e[0m\]' @@ -12,6 +15,7 @@ prompt() { reset='\[\e[0m\]' fi + # What's done next depends on the first argument to the function case "$1" in # Turn complex coloured prompt on @@ -29,76 +33,119 @@ prompt() { # Git prompt function git) + + # Exit if inside a .git directory if $(git rev-parse --is-inside-git-dir 2>/dev/null); then return 1 fi + + # Exit if not inside a working tree if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then return 1 fi + + # Read the repository's status to refresh its info; ignore all the + # output git status &>/dev/null + + # Figure out the branch to show for HEAD, whether a symbolic + # reference or a short SHA-1; chop off any leading path branch=$(git symbolic-ref --quiet HEAD 2>/dev/null) \ || branch=$(git rev-parse --short HEAD 2>/dev/null) \ || branch='unknown' branch=${branch##*/} + + # If there are staged changes in the working tree, add a plus sign + # to the state if ! git diff --quiet --ignore-submodules --cached; then state=${state}+ fi + + # If there are any modified tracked files in the working tree, add + # an exclamation mark to the state if ! git diff-files --quiet --ignore-submodules --; then state=${state}! fi + + # If there are any stashed changes, add a circumflex to the state if $(git rev-parse --verify refs/stash &>/dev/null); then state=${state}^ fi + + # If there are any new unignored files in the working tree, add a + # question mark to the state if [ -n "$(git ls-files --others --exclude-standard)" ]; then state=${state}? fi + + # Print the status in brackets with a git: prefix printf '(git:%s)' "${branch:-unknown}${state}" ;; # Mercurial prompt function hg) + + # Exit if not inside a Mercurial tree if ! branch="$(hg branch 2>/dev/null)"; then return 1 fi + + # If there are changes in the tree, add an exclamation mark to the + # state if [[ -n "$(hg status 2>/dev/null)" ]]; then state="!" fi + + # Print the status in brackets with an hg: prefix printf '(hg:%s)' "${branch:-unknown}${state}" ;; # 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")" + + # 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 branch=${url/$root} branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} + + # If there are changes in the working directory, add an exclamation + # mark to the state if [[ -n "$(svn status 2>/dev/null)" ]]; then state="!" fi + + # Print the state in brackets with an svn: prefix printf '(svn:%s)' "${branch:-unknown}${state}" ;; - # VCS wrapper prompt function + # VCS wrapper prompt function; print the first relevant prompt, if any vcs) prompt git || prompt svn || prompt hg ;; - # Return status prompt function + # Show the return status of the last command in angle brackets return) if [[ $ret -ne 0 ]]; then printf '<%d>' ${ret} fi ;; - # Job count prompt function + # Show the count of background jobs in curly brackets jobs) if [[ -n "$(jobs)" ]]; then printf '{%d}' $(jobs | sed -n '$=') diff --git a/bash/bashrc.d/tmux.bash b/bash/bashrc.d/tmux.bash index ee47e7fc..c7785cd3 100644 --- a/bash/bashrc.d/tmux.bash +++ b/bash/bashrc.d/tmux.bash @@ -1,7 +1,13 @@ # Attach to existing tmux session rather than create a new one if possible tmux() { + + # If any arguments to function, just run tmux as normal with them if [[ -n "$@" ]]; then command tmux "$@" + + # If no arguments, attempt to attach to an existing session; if that fails, + # run the tmux binary with no arguments, which by default creates a new + # session else command tmux attach -d &>/dev/null || command tmux fi diff --git a/sh/profile b/sh/profile index 447a9645..813fb7cc 100644 --- a/sh/profile +++ b/sh/profile @@ -7,13 +7,13 @@ export EDITOR VISUAL PAGER='less' export PAGER -# Add ~/.local/bin to PATH +# Add ~/.local/bin to PATH if it exists if [ -d "$HOME/.local/bin" ]; then PATH="$HOME/.local/bin:$PATH" fi export PATH -# Load any supplementary scripts +# Load all supplementary scripts in ~/.profile.d if [ -d "$HOME/.profile.d" ]; then for file in $HOME/.profile.d/*; do . $file diff --git a/sh/profile.d/keychain.sh b/sh/profile.d/keychain.sh index 01fe8711..6ce780fb 100644 --- a/sh/profile.d/keychain.sh +++ b/sh/profile.d/keychain.sh @@ -1,8 +1,10 @@ -# Keychain +# ssh-askpass setup if command -v ssh-askpass >/dev/null; then SSH_ASKPASS=$(which ssh-askpass) export SSH_ASKPASS fi + +# keychain setup if command -v keychain >/dev/null; then eval $(keychain --eval --ignore-missing --quiet id_dsa id_rsa id_ecsda) fi -- cgit v1.2.3