From 2636ef9bdd0821e27e6d890b1e0964707b1f32ff Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Tue, 20 Sep 2016 11:20:17 +1200 Subject: Block output/error from prompt tput(1)/git(1) Should make things just a little bit faster --- bash/bashrc.d/prompt.bash | 204 +++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 102 deletions(-) (limited to 'bash/bashrc.d/prompt.bash') diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index e7e285a7..b3a26c89 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -31,46 +31,43 @@ prompt() { # Add terminating "$" or "#" sign PS1=$PS1'\$' - # Count available colors - local -i colors - colors=$( { - tput colors || tput Co - } 2>/dev/null ) - - # Prepare reset code - local reset - reset=$( { - tput sgr0 || tput me - } 2>/dev/null ) - - # Decide prompt color formatting based on color availability - local format - - # Check if we have non-bold bright green available - if ((colors >= 16)) ; then - format=$( { - : "${PROMPT_COLOR:=10}" - tput setaf "$PROMPT_COLOR" || - tput setaf "$PROMPT_COLOR" 0 0 || - tput AF "$PROMPT_COLOR" || - tput AF "$PROMPT_COLOR" 0 0 - } 2>/dev/null ) - - # If we have only eight colors, use bold green - elif ((colors >= 8)) ; then - format=$( { - : "${PROMPT_COLOR:=2}" - tput setaf "$PROMPT_COLOR" || - tput AF "$PROMPT_COLOR" - tput bold || tput md - } 2>/dev/null ) - - # Otherwise, we just try bold - else - format=$( { - tput bold || tput md - } 2>/dev/null ) - fi + # Declare variables to contain terminal control strings + local format reset + + # Disregard output and error from these tput(1) calls + { + # Count available colors + local -i colors + colors=$(tput colors || tput Co) + + # Prepare reset code + reset=$(tput sgr0 || tput me) + + # Check if we have non-bold bright green available + if ((colors >= 16)) ; then + format=$( + : "${PROMPT_COLOR:=10}" + tput setaf "$PROMPT_COLOR" || + tput setaf "$PROMPT_COLOR" 0 0 || + tput AF "$PROMPT_COLOR" || + tput AF "$PROMPT_COLOR" 0 0 + ) + + # If we have only eight colors, use bold green + elif ((colors >= 8)) ; then + format=$( + : "${PROMPT_COLOR:=2}" + tput setaf "$PROMPT_COLOR" || + tput AF "$PROMPT_COLOR" + tput bold || tput md + ) + + # Otherwise, we just try bold + else + format=$(tput bold || tput md) + fi + + } >/dev/null 2>&1 # String it all together PS1='\['"$format"'\]'"$PS1"'\['"$reset"'\] ' @@ -90,69 +87,72 @@ prompt() { # Git prompt function 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) - [[ $iswt = true ]] || return - - # Refresh index so e.g. git-diff-files(1) is accurate - git update-index --refresh >/dev/null 2>&1 - - # Find a local branch, remote branch, or tag (annotated or not), or - # failing all of that just show the short commit ID, in that order - # of preference; if none of that works, bail out - local name - name=$( { - git symbolic-ref --quiet HEAD || - git describe --tags --exact-match HEAD || - git rev-parse --short HEAD - } 2>/dev/null) || return - name=${name##*/} - [[ -n $name ]] || return - - # Check various files in .git to flag processes - local proc - [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && - proc=${proc:+$proc,}'REBASE' - [[ -f .git/MERGE_HEAD ]] && - proc=${proc:+$proc,}'MERGE' - [[ -f .git/CHERRY_PICK_HEAD ]] && - proc=${proc:+$proc,}'PICK' - [[ -f .git/REVERT_HEAD ]] && - proc=${proc:+$proc,}'REVERT' - [[ -f .git/BISECT_LOG ]] && - proc=${proc:+$proc,}'BISECT' - - # Collect symbols representing repository state - local state - - # Upstream HEAD has commits after local HEAD; we're "behind" - local -i behind - behind=$(git rev-list --count 'HEAD..@{u}' 2>/dev/null) - ((behind)) && state=${state}'<' - - # Local HEAD has commits after upstream HEAD; we're "ahead" - local -i ahead - ahead=$(git rev-list --count '@{u}..HEAD' 2>/dev/null) - ((ahead)) && state=${state}'>' - - # Tracked files are modified - git diff-files --no-ext-diff --quiet || - state=${state}'!' - - # Changes are staged - git diff-index --cached --no-ext-diff --quiet HEAD 2>/dev/null || - state=${state}'+' - - # There are some untracked and unignored files - git ls-files --directory --error-unmatch --exclude-standard \ - --no-empty-directory --others -- ':/*' >/dev/null 2>&1 && - state=${state}'?' - # There are stashed changes - git rev-parse --quiet --verify refs/stash >/dev/null && - state=${state}'^' + # Wrap as compound command; we don't want to see output from any of + # these git(1) calls + { + # Bail if we're not in a work tree--or, implicitly, if we don't + # have git(1). + [[ -n $(git rev-parse --is-inside-work-tree) ]] || + return + + # Refresh index so e.g. git-diff-files(1) is accurate + git update-index --refresh + + # Find a local branch, remote branch, or tag (annotated or + # not), or failing all of that just show the short commit ID, + # in that order of preference; if none of that works, bail out + local name + name=$( + git symbolic-ref --quiet HEAD || + git describe --tags --exact-match HEAD || + git rev-parse --short HEAD + ) || return + name=${name##*/} + [[ -n $name ]] || return + + # Check various files in .git to flag processes + local proc + [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && + proc=${proc:+$proc,}'REBASE' + [[ -f .git/MERGE_HEAD ]] && + proc=${proc:+$proc,}'MERGE' + [[ -f .git/CHERRY_PICK_HEAD ]] && + proc=${proc:+$proc,}'PICK' + [[ -f .git/REVERT_HEAD ]] && + proc=${proc:+$proc,}'REVERT' + [[ -f .git/BISECT_LOG ]] && + proc=${proc:+$proc,}'BISECT' + + # Collect symbols representing repository state + local state + + # Upstream HEAD has commits after local HEAD; we're "behind" + (($(git rev-list --count 'HEAD..@{u}'))) && + state=${state}'<' + + # Local HEAD has commits after upstream HEAD; we're "ahead" + (($(git rev-list --count '@{u}..HEAD'))) && + state=${state}'>' + + # Tracked files are modified + git diff-files --no-ext-diff --quiet || + state=${state}'!' + + # Changes are staged + git diff-index --cached --no-ext-diff --quiet HEAD || + state=${state}'+' + + # There are some untracked and unignored files + git ls-files --directory --error-unmatch --exclude-standard \ + --no-empty-directory --others -- ':/*' && + state=${state}'?' + + # There are stashed changes + git rev-parse --quiet --verify refs/stash && + state=${state}'^' + + } >/dev/null 2>&1 # Print the status in brackets; add a git: prefix only if there # might be another VCS prompt (because PROMPT_VCS is set) -- cgit v1.2.3