From 0e0e0d30351f5b0e6e61510b7aacc33e23f85de9 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 20 Sep 2021 12:52:16 -0700 Subject: [PATCH 01/11] lib/theme: Fix a *few* SC2154 These variables are referenced by themes already linted. --- themes/base.theme.bash | 43 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index d7479b3f..3404c8c6 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -94,10 +94,10 @@ RBFU_THEME_PROMPT_SUFFIX='|' : "${SVN_EXE:=$SCM_SVN}" function _bash_it_appearance_scm_init() { - GIT_EXE="$(type -P $SCM_GIT || true)" - P4_EXE="$(type -P $SCM_P4 || true)" - HG_EXE="$(type -P $SCM_HG || true)" - SVN_EXE="$(type -P $SCM_SVN || true)" + GIT_EXE="$(type -P "$SCM_GIT" || true)" + P4_EXE="$(type -P "$SCM_P4" || true)" + HG_EXE="$(type -P "$SCM_HG" || true)" + SVN_EXE="$(type -P "$SCM_SVN" || true)" # Check for broken SVN exe that is caused by some versions of Xcode. # See https://github.com/Bash-it/bash-it/issues/1612 for more details. @@ -235,7 +235,7 @@ function git_prompt_minimal_info { } function git_prompt_vars { - if ${SCM_GIT_USE_GITSTATUS} && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT}" == "ok-sync" ]]; then + if "${SCM_GIT_USE_GITSTATUS:-false}" && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT:-}" == "ok-sync" ]]; then # we can use faster gitstatus # use this variable in githelpers and below to choose gitstatus output SCM_GIT_GITSTATUS_RAN=true @@ -259,8 +259,8 @@ function git_prompt_vars { fi if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - commits_behind=${VCS_STATUS_COMMITS_BEHIND} - commits_ahead=${VCS_STATUS_COMMITS_AHEAD} + commits_behind=${VCS_STATUS_COMMITS_BEHIND?} + commits_ahead=${VCS_STATUS_COMMITS_AHEAD?} else IFS=$'\t' read -r commits_behind commits_ahead <<< "$(_git-upstream-behind-ahead)" fi @@ -276,7 +276,7 @@ function git_prompt_vars { if [[ "${SCM_GIT_SHOW_STASH_INFO}" = "true" ]]; then local stash_count if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - stash_count=${VCS_STATUS_STASHES} + stash_count=${VCS_STATUS_STASHES?} else stash_count="$(git stash list 2> /dev/null | wc -l | tr -d ' ')" fi @@ -286,9 +286,9 @@ function git_prompt_vars { SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN} if ! _git-hide-status; then if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - untracked_count=${VCS_STATUS_NUM_UNTRACKED} - unstaged_count=${VCS_STATUS_NUM_UNSTAGED} - staged_count=${VCS_STATUS_NUM_STAGED} + untracked_count=${VCS_STATUS_NUM_UNTRACKED?} + unstaged_count=${VCS_STATUS_NUM_UNSTAGED?} + staged_count=${VCS_STATUS_NUM_STAGED?} else IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$(_git-status-counts)" fi @@ -429,7 +429,7 @@ function rbenv_version_prompt { } function rbfu_version_prompt { - if [[ $RBFU_RUBY_VERSION ]]; then + if [[ -n "${RBFU_RUBY_VERSION:-}" ]]; then echo -e "${RBFU_THEME_PROMPT_PREFIX}${RBFU_RUBY_VERSION}${RBFU_THEME_PROMPT_SUFFIX}" fi } @@ -445,12 +445,12 @@ function chruby_version_prompt { if ! chruby | grep -q '\*'; then ruby_version="${ruby_version} (system)" fi - echo -e "${CHRUBY_THEME_PROMPT_PREFIX}${ruby_version}${CHRUBY_THEME_PROMPT_SUFFIX}" + echo -e "${CHRUBY_THEME_PROMPT_PREFIX:-}${ruby_version}${CHRUBY_THEME_PROMPT_SUFFIX:-}" fi } function ruby_version_prompt { - if [[ "${THEME_SHOW_RUBY_PROMPT}" = "true" ]]; then + if [[ "${THEME_SHOW_RUBY_PROMPT:-}" == "true" ]]; then echo -e "$(rbfu_version_prompt)$(rbenv_version_prompt)$(rvm_version_prompt)$(chruby_version_prompt)" fi } @@ -464,21 +464,22 @@ function k8s_namespace_prompt { } function virtualenv_prompt { - if [[ -n "$VIRTUAL_ENV" ]]; then - virtualenv=$(basename "$VIRTUAL_ENV") + if [[ -n "${VIRTUAL_ENV:-}" ]]; then + virtualenv="${VIRTUAL_ENV##*/}" echo -e "$VIRTUALENV_THEME_PROMPT_PREFIX$virtualenv$VIRTUALENV_THEME_PROMPT_SUFFIX" fi } function condaenv_prompt { - if [[ $CONDA_DEFAULT_ENV ]]; then - echo -e "${CONDAENV_THEME_PROMPT_PREFIX}${CONDA_DEFAULT_ENV}${CONDAENV_THEME_PROMPT_SUFFIX}" + if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then + echo -e "${CONDAENV_THEME_PROMPT_PREFIX:-}${CONDA_DEFAULT_ENV}${CONDAENV_THEME_PROMPT_SUFFIX:-}" fi } function py_interp_prompt { + local py_version py_version=$(python --version 2>&1 | awk 'NR==1{print "py-"$2;}') || return - echo -e "${PYTHON_THEME_PROMPT_PREFIX}${py_version}${PYTHON_THEME_PROMPT_SUFFIX}" + echo -e "${PYTHON_THEME_PROMPT_PREFIX:-}${py_version}${PYTHON_THEME_PROMPT_SUFFIX:-}" } function python_version_prompt { @@ -506,7 +507,7 @@ function clock_char { function clock_prompt { CLOCK_COLOR=${THEME_CLOCK_COLOR:-"$normal"} CLOCK_FORMAT=${THEME_CLOCK_FORMAT:-"%H:%M:%S"} - [ -z "$THEME_SHOW_CLOCK" ] && THEME_SHOW_CLOCK=${THEME_CLOCK_CHECK:-"true"} + [[ -z "${THEME_SHOW_CLOCK:-}" ]] && THEME_SHOW_CLOCK=${THEME_CLOCK_CHECK:-"true"} SHOW_CLOCK=$THEME_SHOW_CLOCK if [[ "${SHOW_CLOCK}" = "true" ]]; then @@ -576,7 +577,7 @@ if ! _command_exists battery_percentage; then fi function aws_profile { - if [[ $AWS_DEFAULT_PROFILE ]]; then + if [[ -n "${AWS_DEFAULT_PROFILE:-}" ]]; then echo -e "${AWS_DEFAULT_PROFILE}" else echo -e "default" From fbc5d0a5af042bde489d68894c04213003ed6292 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 26 Jan 2022 10:56:59 -0800 Subject: [PATCH 02/11] lib/p4helpers: `shfmt` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit My apologies to future `git blame` hunters ♥ --- clean_files.txt | 1 + themes/p4helpers.theme.bash | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 54180c19..26b059da 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -160,6 +160,7 @@ themes/easy themes/essential themes/modern themes/norbu +themes/p4helpers.theme.bash themes/pete themes/powerline themes/pure diff --git a/themes/p4helpers.theme.bash b/themes/p4helpers.theme.bash index 27a777ac..30b520cc 100644 --- a/themes/p4helpers.theme.bash +++ b/themes/p4helpers.theme.bash @@ -1,18 +1,18 @@ -#!/usr/bin/env bash +# shellcheck shell=bash function _p4-opened { - timeout 2.0s p4 opened -s 2> /dev/null + timeout 2.0s p4 opened -s 2> /dev/null } function _p4-opened-counts { - # Return the following counts seperated by tabs: - # - count of opened files - # - count of pending changesets (other than defaults) - # - count of files in the default changeset - # - count of opened files in add mode - # - count of opened files in edit mode - # - count of opened files in delete mode - _p4-opened | awk ' + # Return the following counts seperated by tabs: + # - count of opened files + # - count of pending changesets (other than defaults) + # - count of files in the default changeset + # - count of opened files in add mode + # - count of opened files in edit mode + # - count of opened files in delete mode + _p4-opened | awk ' BEGIN { opened=0; type_array["edit"]=0; From 6bacd5fb1c7a4844cc8c742ffdd3fe56ede3f062 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 3 Mar 2022 22:54:08 -0800 Subject: [PATCH 03/11] lib/githelpers: `shfmt` && `shellcheck` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit My apologies to future `git blame` hunters ♥ --- clean_files.txt | 1 + themes/githelpers.theme.bash | 225 ++++++++++++++++++----------------- 2 files changed, 116 insertions(+), 110 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 26b059da..15c91227 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -158,6 +158,7 @@ themes/candy themes/command_duration.theme.bash themes/easy themes/essential +themes/githelpers.theme.bash themes/modern themes/norbu themes/p4helpers.theme.bash diff --git a/themes/githelpers.theme.bash b/themes/githelpers.theme.bash index 5ef18e9a..2fbb7e8a 100644 --- a/themes/githelpers.theme.bash +++ b/themes/githelpers.theme.bash @@ -1,99 +1,106 @@ -#!/usr/bin/env bash +# shellcheck shell=bash -function _git-symbolic-ref { - git symbolic-ref -q HEAD 2> /dev/null +function _git-symbolic-ref() { + git symbolic-ref -q HEAD 2> /dev/null } # When on a branch, this is often the same as _git-commit-description, # but this can be different when two branches are pointing to the # same commit. _git-branch is used to explicitly choose the checked-out # branch. -function _git-branch { - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - test -n "${VCS_STATUS_LOCAL_BRANCH}" && echo "${VCS_STATUS_LOCAL_BRANCH}" || return 1 - else - git symbolic-ref -q --short HEAD 2> /dev/null || return 1 - fi +function _git-branch() { + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + if [[ -n "${VCS_STATUS_LOCAL_BRANCH:-}" ]]; then + echo "${VCS_STATUS_LOCAL_BRANCH}" + else + return 1 + fi + else + git symbolic-ref -q --short HEAD 2> /dev/null || return 1 + fi } -function _git-tag { - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - test -n "${VCS_STATUS_TAG}" && echo "${VCS_STATUS_TAG}" - else - git describe --tags --exact-match 2> /dev/null - fi +function _git-tag() { + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + if [[ -n "${VCS_STATUS_TAG:-}" ]]; then + echo "${VCS_STATUS_TAG}" + fi + else + git describe --tags --exact-match 2> /dev/null + fi } -function _git-commit-description { - git describe --contains --all 2> /dev/null +function _git-commit-description() { + git describe --contains --all 2> /dev/null } -function _git-short-sha { - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - echo ${VCS_STATUS_COMMIT:0:7} - else - git rev-parse --short HEAD - fi +function _git-short-sha() { + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + echo "${VCS_STATUS_COMMIT:0:7}" + else + git rev-parse --short HEAD + fi } # Try the checked-out branch first to avoid collision with branches pointing to the same ref. -function _git-friendly-ref { - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - _git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus - else - _git-branch || _git-tag || _git-commit-description || _git-short-sha - fi +function _git-friendly-ref() { + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + _git-branch || _git-tag || _git-short-sha # there is no tag based describe output in gitstatus + else + _git-branch || _git-tag || _git-commit-description || _git-short-sha + fi } -function _git-num-remotes { - git remote | wc -l +function _git-num-remotes() { + git remote | wc -l } -function _git-upstream { - local ref - ref="$(_git-symbolic-ref)" || return 1 - git for-each-ref --format="%(upstream:short)" "${ref}" +function _git-upstream() { + local ref + ref="$(_git-symbolic-ref)" || return 1 + git for-each-ref --format="%(upstream:short)" "${ref}" } -function _git-upstream-remote { - local upstream - upstream="$(_git-upstream)" || return 1 +function _git-upstream-remote() { + local upstream branch + upstream="$(_git-upstream)" || return 1 - local branch - branch="$(_git-upstream-branch)" || return 1 - echo "${upstream%"/${branch}"}" + branch="$(_git-upstream-branch)" || return 1 + echo "${upstream%"/${branch}"}" } -function _git-upstream-branch { - local ref - ref="$(_git-symbolic-ref)" || return 1 +function _git-upstream-branch() { + local ref + ref="$(_git-symbolic-ref)" || return 1 - # git versions < 2.13.0 do not support "strip" for upstream format - # regex replacement gives the wrong result for any remotes with slashes in the name, - # so only use when the strip format fails. - git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///" + # git versions < 2.13.0 do not support "strip" for upstream format + # regex replacement gives the wrong result for any remotes with slashes in the name, + # so only use when the strip format fails. + git for-each-ref --format="%(upstream:strip=3)" "${ref}" 2> /dev/null || git for-each-ref --format="%(upstream)" "${ref}" | sed -e "s/.*\/.*\/.*\///" } -function _git-upstream-behind-ahead { - git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null +function _git-upstream-behind-ahead() { + git rev-list --left-right --count "$(_git-upstream)...HEAD" 2> /dev/null } -function _git-upstream-branch-gone { - [[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]] +function _git-upstream-branch-gone() { + [[ "$(git status -s -b | sed -e 's/.* //')" == "[gone]" ]] } -function _git-hide-status { - [[ "$(git config --get bash-it.hide-status)" == "1" ]] +function _git-hide-status() { + [[ "$(git config --get bash-it.hide-status)" == "1" ]] } -function _git-status { - local git_status_flags= - [[ "${SCM_GIT_IGNORE_UNTRACKED}" = "true" ]] && git_status_flags='-uno' || true - git status --porcelain ${git_status_flags} 2> /dev/null +function _git-status() { + local git_status_flags= + if [[ "${SCM_GIT_IGNORE_UNTRACKED:-}" == "true" ]]; then + git_status_flags='-uno' + fi + git status --porcelain "${git_status_flags:---}" 2> /dev/null } -function _git-status-counts { - _git-status | awk ' +function _git-status-counts() { + _git-status | awk ' BEGIN { untracked=0; unstaged=0; @@ -116,60 +123,58 @@ function _git-status-counts { }' } -function _git-remote-info { +function _git-remote-info() { + local same_branch_name="" branch_prefix + # prompt handling only, reimplement because patching the routine below gets ugly + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + [[ "${VCS_STATUS_REMOTE_NAME?}" == "" ]] && return + [[ "${VCS_STATUS_LOCAL_BRANCH?}" == "${VCS_STATUS_REMOTE_BRANCH?}" ]] && same_branch_name=true + # no multiple remote support in gitstatusd + if [[ "${SCM_GIT_SHOW_REMOTE_INFO:-}" == "true" || "${SCM_GIT_SHOW_REMOTE_INFO:-}" == "auto" ]]; then + if [[ ${same_branch_name:-} != "true" ]]; then + remote_info="${VCS_STATUS_REMOTE_NAME?}/${VCS_STATUS_REMOTE_BRANCH?}" + else + remote_info="${VCS_STATUS_REMOTE_NAME?}" + fi + elif [[ ${same_branch_name:-} != "true" ]]; then + remote_info="${VCS_STATUS_REMOTE_BRANCH?}" + fi + if [[ -n "${remote_info:-}" ]]; then + # no support for gone remote branches in gitstatusd + branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}" + echo "${branch_prefix}${remote_info:-}" + fi + else + [[ "$(_git-upstream)" == "" ]] && return - # prompt handling only, reimplement because patching the routine below gets ugly - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - [[ "${VCS_STATUS_REMOTE_NAME}" == "" ]] && return - [[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && local same_branch_name=true - local same_branch_name= - [[ "${VCS_STATUS_LOCAL_BRANCH}" == "${VCS_STATUS_REMOTE_BRANCH}" ]] && same_branch_name=true - # no multiple remote support in gitstatusd - if [[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" || "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]]; then - if [[ "${same_branch_name}" != "true" ]]; then - remote_info="${VCS_STATUS_REMOTE_NAME}/${VCS_STATUS_REMOTE_BRANCH}" - else - remote_info="${VCS_STATUS_REMOTE_NAME}" - fi - elif [[ ${same_branch_name} != "true" ]]; then - remote_info="${VCS_STATUS_REMOTE_BRANCH}" - fi - if [[ -n "${remote_info:-}" ]];then - # no support for gone remote branches in gitstatusd - local branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}" - echo "${branch_prefix}${remote_info}" - fi - else - [[ "$(_git-upstream)" == "" ]] && return - - [[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && local same_branch_name=true - local same_branch_name= - [[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true - if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" && "$(_git-num-remotes)" -ge 2) || - "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then - if [[ "${same_branch_name}" != "true" ]]; then - remote_info="\$(_git-upstream)" - else - remote_info="$(_git-upstream-remote)" - fi - elif [[ ${same_branch_name} != "true" ]]; then - remote_info="\$(_git-upstream-branch)" - fi - if [[ -n "${remote_info:-}" ]];then - local branch_prefix - if _git-upstream-branch-gone; then - branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX}" - else - branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX}" - fi - echo "${branch_prefix}${remote_info}" - fi - fi + [[ "$(_git-branch)" == "$(_git-upstream-branch)" ]] && same_branch_name=true + if [[ ("${SCM_GIT_SHOW_REMOTE_INFO}" == "auto" && "$(_git-num-remotes)" -ge 2) || + "${SCM_GIT_SHOW_REMOTE_INFO}" == "true" ]]; then + if [[ ${same_branch_name:-} != "true" ]]; then + # shellcheck disable=SC2016 + remote_info='$(_git-upstream)' + else + remote_info="$(_git-upstream-remote)" + fi + elif [[ ${same_branch_name:-} != "true" ]]; then + # shellcheck disable=SC2016 + remote_info='$(_git-upstream-branch)' + fi + if [[ -n "${remote_info:-}" ]]; then + local branch_prefix + if _git-upstream-branch-gone; then + branch_prefix="${SCM_THEME_BRANCH_GONE_PREFIX:-}" + else + branch_prefix="${SCM_THEME_BRANCH_TRACK_PREFIX:-}" + fi + echo "${branch_prefix}${remote_info:-}" + fi + fi } # Unused by bash-it, present for API compatibility -function git_status_summary { - awk ' +function git_status_summary() { + awk ' BEGIN { untracked=0; unstaged=0; From 1d73537dbfb927324b81626d57ed7befc8126168 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 26 Jan 2022 10:58:44 -0800 Subject: [PATCH 04/11] lib/theme: `shfmt` && `shellcheck` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit My apologies to future `git blame` hunters ♥ Use the "short" host name by default (`\h`), not the fully qualified domain name (`\H`)... lib/theme: don't redefine battery_char() Combine the two definitions for `battery_char()` so the second one doesn't just overwrite the first one. Do one or the other, not both. Don't evaluate if `battery_percentage()` is available at load time, evaluate it at run time. Don't run `date` for `$THEME_TIME_FORMAT`, use `\D{fmt}`. --- themes/base.theme.bash | 282 ++++++++++++++++++++--------------------- 1 file changed, 138 insertions(+), 144 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 3404c8c6..1706eba4 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -6,11 +6,11 @@ CLOCK_CHAR_THEME_PROMPT_SUFFIX='' CLOCK_THEME_PROMPT_PREFIX='' CLOCK_THEME_PROMPT_SUFFIX='' -THEME_PROMPT_HOST='\H' +THEME_PROMPT_HOST='\h' SCM= -SCM_CHECK=${SCM_CHECK:=true} +: "${SCM_CHECK:=true}" SCM_THEME_PROMPT_DIRTY=' ✗' SCM_THEME_PROMPT_CLEAN=' ✓' @@ -30,15 +30,15 @@ SCM_THEME_CHAR_SUFFIX='' : "${THEME_CHECK_SUDO:=false}" : "${THEME_BATTERY_PERCENTAGE_CHECK:=true}" -SCM_GIT_SHOW_DETAILS=${SCM_GIT_SHOW_DETAILS:=true} -SCM_GIT_SHOW_REMOTE_INFO=${SCM_GIT_SHOW_REMOTE_INFO:=auto} -SCM_GIT_IGNORE_UNTRACKED=${SCM_GIT_IGNORE_UNTRACKED:=false} -SCM_GIT_SHOW_CURRENT_USER=${SCM_GIT_SHOW_CURRENT_USER:=false} -SCM_GIT_SHOW_MINIMAL_INFO=${SCM_GIT_SHOW_MINIMAL_INFO:=false} -SCM_GIT_SHOW_STASH_INFO=${SCM_GIT_SHOW_STASH_INFO:=true} -SCM_GIT_SHOW_COMMIT_COUNT=${SCM_GIT_SHOW_COMMIT_COUNT:=true} -SCM_GIT_USE_GITSTATUS=${SCM_GIT_USE_GITSTATUS:=false} -SCM_GIT_GITSTATUS_RAN=${SCM_GIT_GITSTATUS_RAN:=false} +: "${SCM_GIT_SHOW_DETAILS:=true}" +: "${SCM_GIT_SHOW_REMOTE_INFO:=auto}" +: "${SCM_GIT_IGNORE_UNTRACKED:=false}" +: "${SCM_GIT_SHOW_CURRENT_USER:=false}" +: "${SCM_GIT_SHOW_MINIMAL_INFO:=false}" +: "${SCM_GIT_SHOW_STASH_INFO:=true}" +: "${SCM_GIT_SHOW_COMMIT_COUNT:=true}" +: "${SCM_GIT_USE_GITSTATUS:=false}" +: "${SCM_GIT_GITSTATUS_RAN:=false}" SCM_GIT='git' SCM_GIT_CHAR='±' @@ -73,9 +73,9 @@ NVM_THEME_PROMPT_SUFFIX='|' RVM_THEME_PROMPT_PREFIX=' |' RVM_THEME_PROMPT_SUFFIX='|' -THEME_SHOW_RUBY_PROMPT=${THEME_SHOW_RUBY_PROMPT:=true} +: "${THEME_SHOW_RUBY_PROMPT:=true}" -THEME_SHOW_USER_HOST=${THEME_SHOW_USER_HOST:=false} +: "${THEME_SHOW_USER_HOST:=false}" USER_HOST_THEME_PROMPT_PREFIX='' USER_HOST_THEME_PROMPT_SUFFIX='' @@ -94,10 +94,10 @@ RBFU_THEME_PROMPT_SUFFIX='|' : "${SVN_EXE:=$SCM_SVN}" function _bash_it_appearance_scm_init() { - GIT_EXE="$(type -P "$SCM_GIT" || true)" - P4_EXE="$(type -P "$SCM_P4" || true)" - HG_EXE="$(type -P "$SCM_HG" || true)" - SVN_EXE="$(type -P "$SCM_SVN" || true)" + GIT_EXE="$(type -P "${SCM_GIT}" || true)" + P4_EXE="$(type -P "${SCM_P4}" || true)" + HG_EXE="$(type -P "${SCM_HG}" || true)" + SVN_EXE="$(type -P "${SCM_SVN}" || true)" # Check for broken SVN exe that is caused by some versions of Xcode. # See https://github.com/Bash-it/bash-it/issues/1612 for more details. @@ -107,35 +107,36 @@ function _bash_it_appearance_scm_init() { SVN_EXE="" fi fi + return 0 } _bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init') -function scm { - if [[ "$SCM_CHECK" = false ]]; then - SCM=$SCM_NONE +function scm() { + if [[ "$SCM_CHECK" == false ]]; then + SCM="$SCM_NONE" elif [[ -f .git/HEAD ]] && [[ -x "$GIT_EXE" ]]; then - SCM=$SCM_GIT + SCM="$SCM_GIT" elif [[ -d .hg ]] && [[ -x "$HG_EXE" ]]; then - SCM=$SCM_HG + SCM="$SCM_HG" elif [[ -d .svn ]] && [[ -x "$SVN_EXE" ]]; then - SCM=$SCM_SVN + SCM="$SCM_SVN" elif [[ -x "$GIT_EXE" ]] && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then - SCM=$SCM_GIT + SCM="$SCM_GIT" elif [[ -x "$HG_EXE" ]] && [[ -n "$(hg root 2> /dev/null)" ]]; then - SCM=$SCM_HG + SCM="$SCM_HG" elif [[ -x "$SVN_EXE" ]] && [[ -n "$(svn info --show-item wc-root 2> /dev/null)" ]]; then - SCM=$SCM_SVN + SCM="$SCM_SVN" elif [[ -x "$P4_EXE" ]] && [[ -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then - SCM=$SCM_P4 + SCM="$SCM_P4" else - SCM=$SCM_NONE + SCM="$SCM_NONE" fi } -scm_prompt() { +function scm_prompt() { local CHAR CHAR="$(scm_char)" - local format=${SCM_PROMPT_FORMAT:-'[%s%s]'} + local format="${SCM_PROMPT_FORMAT:-'[%s%s]'}" if [[ "${CHAR}" != "$SCM_NONE_CHAR" ]]; then # shellcheck disable=2059 @@ -143,22 +144,22 @@ scm_prompt() { fi } -function scm_prompt_char { - if [[ -z $SCM ]]; then scm; fi +function scm_prompt_char() { + if [[ -z "$SCM" ]]; then scm; fi if [[ $SCM == "$SCM_GIT" ]]; then - SCM_CHAR=$SCM_GIT_CHAR + SCM_CHAR="$SCM_GIT_CHAR" elif [[ $SCM == "$SCM_P4" ]]; then - SCM_CHAR=$SCM_P4_CHAR + SCM_CHAR="$SCM_P4_CHAR" elif [[ $SCM == "$SCM_HG" ]]; then - SCM_CHAR=$SCM_HG_CHAR + SCM_CHAR="$SCM_HG_CHAR" elif [[ $SCM == "$SCM_SVN" ]]; then - SCM_CHAR=$SCM_SVN_CHAR + SCM_CHAR="$SCM_SVN_CHAR" else - SCM_CHAR=$SCM_NONE_CHAR + SCM_CHAR="$SCM_NONE_CHAR" fi } -function scm_prompt_vars { +function scm_prompt_vars() { scm scm_prompt_char SCM_DIRTY=0 @@ -169,19 +170,19 @@ function scm_prompt_vars { [[ $SCM == "$SCM_SVN" ]] && svn_prompt_vars && return } -function scm_prompt_info { +function scm_prompt_info() { scm scm_prompt_char scm_prompt_info_common } -function scm_prompt_char_info { +function scm_prompt_char_info() { scm_prompt_char echo -ne "${SCM_THEME_CHAR_PREFIX}${SCM_CHAR}${SCM_THEME_CHAR_SUFFIX}" scm_prompt_info_common } -function scm_prompt_info_common { +function scm_prompt_info_common() { SCM_DIRTY=0 SCM_STATE='' @@ -202,22 +203,22 @@ function scm_prompt_info_common { { [[ ${SCM} == "${SCM_SVN}" ]] && svn_prompt_info && return; } || true } -function terraform_workspace_prompt { +function terraform_workspace_prompt() { if _command_exists terraform; then - if [ -d .terraform ]; then + if [[ -d .terraform ]]; then echo -e "$(terraform workspace show 2> /dev/null)" fi fi } -function active_gcloud_account_prompt { +function active_gcloud_account_prompt() { if _command_exists gcloud; then echo -e "$(gcloud config list account --format "value(core.account)" 2> /dev/null)" fi } -function git_prompt_minimal_info { - SCM_STATE=${SCM_THEME_PROMPT_CLEAN} +function git_prompt_minimal_info() { + SCM_STATE="${SCM_THEME_PROMPT_CLEAN}" _git-hide-status && return @@ -225,16 +226,16 @@ function git_prompt_minimal_info { if [[ -n "$(_git-status | tail -n1)" ]]; then SCM_DIRTY=1 - SCM_STATE=${SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" fi # Output the git prompt - SCM_PREFIX=${SCM_THEME_PROMPT_PREFIX} - SCM_SUFFIX=${SCM_THEME_PROMPT_SUFFIX} + SCM_PREFIX="${SCM_THEME_PROMPT_PREFIX}" + SCM_SUFFIX="${SCM_THEME_PROMPT_SUFFIX}" echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" } -function git_prompt_vars { +function git_prompt_vars() { if "${SCM_GIT_USE_GITSTATUS:-false}" && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT:-}" == "ok-sync" ]]; then # we can use faster gitstatus # use this variable in githelpers and below to choose gitstatus output @@ -251,99 +252,99 @@ function git_prompt_vars { local detached_prefix if _git-tag &> /dev/null; then - detached_prefix=${SCM_THEME_TAG_PREFIX} + detached_prefix="${SCM_THEME_TAG_PREFIX}" else - detached_prefix=${SCM_THEME_DETACHED_PREFIX} + detached_prefix="${SCM_THEME_DETACHED_PREFIX}" fi SCM_BRANCH="${detached_prefix}\$(_git-friendly-ref)" fi - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - commits_behind=${VCS_STATUS_COMMITS_BEHIND?} - commits_ahead=${VCS_STATUS_COMMITS_AHEAD?} + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + commits_behind="${VCS_STATUS_COMMITS_BEHIND?}" + commits_ahead="${VCS_STATUS_COMMITS_AHEAD?}" else IFS=$'\t' read -r commits_behind commits_ahead <<< "$(_git-upstream-behind-ahead)" fi if [[ "${commits_ahead}" -gt 0 ]]; then SCM_BRANCH+="${SCM_GIT_AHEAD_BEHIND_PREFIX_CHAR}${SCM_GIT_AHEAD_CHAR}" - [[ "${SCM_GIT_SHOW_COMMIT_COUNT}" = "true" ]] && SCM_BRANCH+="${commits_ahead}" + [[ "${SCM_GIT_SHOW_COMMIT_COUNT}" == "true" ]] && SCM_BRANCH+="${commits_ahead}" fi if [[ "${commits_behind}" -gt 0 ]]; then SCM_BRANCH+="${SCM_GIT_AHEAD_BEHIND_PREFIX_CHAR}${SCM_GIT_BEHIND_CHAR}" - [[ "${SCM_GIT_SHOW_COMMIT_COUNT}" = "true" ]] && SCM_BRANCH+="${commits_behind}" + [[ "${SCM_GIT_SHOW_COMMIT_COUNT}" == "true" ]] && SCM_BRANCH+="${commits_behind}" fi - if [[ "${SCM_GIT_SHOW_STASH_INFO}" = "true" ]]; then + if [[ "${SCM_GIT_SHOW_STASH_INFO}" == "true" ]]; then local stash_count if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - stash_count=${VCS_STATUS_STASHES?} + stash_count="${VCS_STATUS_STASHES?}" else stash_count="$(git stash list 2> /dev/null | wc -l | tr -d ' ')" fi [[ "${stash_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_STASH_CHAR_PREFIX}${stash_count}${SCM_GIT_STASH_CHAR_SUFFIX}" fi - SCM_STATE=${GIT_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN} + SCM_STATE="${GIT_THEME_PROMPT_CLEAN:-${SCM_THEME_PROMPT_CLEAN:-}}" if ! _git-hide-status; then - if [[ "${SCM_GIT_GITSTATUS_RAN}" == "true" ]]; then - untracked_count=${VCS_STATUS_NUM_UNTRACKED?} - unstaged_count=${VCS_STATUS_NUM_UNSTAGED?} - staged_count=${VCS_STATUS_NUM_STAGED?} + if [[ "${SCM_GIT_GITSTATUS_RAN:-}" == "true" ]]; then + untracked_count="${VCS_STATUS_NUM_UNTRACKED?}" + unstaged_count="${VCS_STATUS_NUM_UNSTAGED?}" + staged_count="${VCS_STATUS_NUM_STAGED?}" else IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$(_git-status-counts)" fi if [[ "${untracked_count}" -gt 0 || "${unstaged_count}" -gt 0 || "${staged_count}" -gt 0 ]]; then SCM_DIRTY=1 - if [[ "${SCM_GIT_SHOW_DETAILS}" = "true" ]]; then + if [[ "${SCM_GIT_SHOW_DETAILS}" == "true" ]]; then [[ "${staged_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_STAGED_CHAR}${staged_count}" && SCM_DIRTY=3 [[ "${unstaged_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_UNSTAGED_CHAR}${unstaged_count}" && SCM_DIRTY=2 [[ "${untracked_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_UNTRACKED_CHAR}${untracked_count}" && SCM_DIRTY=1 fi - SCM_STATE=${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" fi fi # no if for gitstatus here, user extraction is not supported by it [[ "${SCM_GIT_SHOW_CURRENT_USER}" == "true" ]] && SCM_BRANCH+="$(git_user_info)" - SCM_PREFIX=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} - SCM_SUFFIX=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} + SCM_PREFIX="${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" + SCM_SUFFIX="${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" SCM_CHANGE=$(_git-short-sha 2> /dev/null || echo "") } -function p4_prompt_vars { +function p4_prompt_vars() { IFS=$'\t' read -r \ opened_count non_default_changes default_count \ add_file_count edit_file_count delete_file_count \ <<< "$(_p4-opened-counts)" if [[ "${opened_count}" -gt 0 ]]; then SCM_DIRTY=1 - SCM_STATE=${SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" [[ "${opened_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_OPENED_CHAR}${opened_count}" [[ "${non_default_changes}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_CHANGES_CHAR}${non_default_changes}" [[ "${default_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_DEFAULT_CHAR}${default_count}" else SCM_DIRTY=0 - SCM_STATE=${SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" fi - SCM_PREFIX=${P4_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} - SCM_SUFFIX=${P4_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} + SCM_PREFIX="${P4_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" + SCM_SUFFIX="${P4_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" } -function svn_prompt_vars { +function svn_prompt_vars() { if [[ -n $(svn status | head -c1 2> /dev/null) ]]; then SCM_DIRTY=1 - SCM_STATE=${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" else SCM_DIRTY=0 - SCM_STATE=${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN} + SCM_STATE="${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}" fi - SCM_PREFIX=${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} - SCM_SUFFIX=${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} - SCM_BRANCH=$(svn info --show-item=url 2> /dev/null | awk -F/ '{ for (i=0; i<=NF; i++) { if ($i == "branches" || $i == "tags" ) { print $(i+1); break }; if ($i == "trunk") { print $i; break } } }') || return - SCM_CHANGE=$(svn info --show-item=revision 2> /dev/null) + SCM_PREFIX="${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" + SCM_SUFFIX="${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" + SCM_BRANCH="$(svn info --show-item=url 2> /dev/null | awk -F/ '{ for (i=0; i<=NF; i++) { if ($i == "branches" || $i == "tags" ) { print $(i+1); break }; if ($i == "trunk") { print $i; break } } }')" || return + SCM_CHANGE="$(svn info --show-item=revision 2> /dev/null)" } # this functions returns absolute location of .hg directory if one exists @@ -353,7 +354,7 @@ function svn_prompt_vars { # - lets say we cd into ~/Projects/Foo/Bar # - .hg is located in ~/Projects/Foo/.hg # - get_hg_root starts at ~/Projects/Foo/Bar and sees that there is no .hg directory, so then it goes into ~/Projects/Foo -function get_hg_root { +function get_hg_root() { local CURRENT_DIR="${PWD}" while [[ "${CURRENT_DIR:-/}" != "/" ]]; do @@ -366,29 +367,29 @@ function get_hg_root { done } -function hg_prompt_vars { +function hg_prompt_vars() { if [[ -n $(hg status 2> /dev/null) ]]; then SCM_DIRTY=1 - SCM_STATE=${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY} + SCM_STATE="${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" else SCM_DIRTY=0 - SCM_STATE=${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN} + SCM_STATE="${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}" fi - SCM_PREFIX=${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} - SCM_SUFFIX=${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} + SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" + SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" HG_ROOT=$(get_hg_root) - if [ -f "$HG_ROOT/branch" ]; then + if [[ -f "$HG_ROOT/branch" ]]; then # Mercurial holds it's current branch in .hg/branch file SCM_BRANCH=$(< "${HG_ROOT}/branch") - local bookmark=${HG_ROOT}/bookmarks.current - [[ -f ${bookmark} ]] && SCM_BRANCH+=:$(< "${bookmark}") + local bookmark="${HG_ROOT}/bookmarks.current" + [[ -f "${bookmark}" ]] && SCM_BRANCH+=:$(< "${bookmark}") else SCM_BRANCH=$(hg summary 2> /dev/null | grep branch: | awk '{print $2}') fi - if [ -f "$HG_ROOT/dirstate" ]; then + if [[ -f "$HG_ROOT/dirstate" ]]; then # Mercurial holds various information about the working directory in .hg/dirstate file. More on http://mercurial.selenic.com/wiki/DirState SCM_CHANGE=$(hexdump -vn 10 -e '1/1 "%02x"' "$HG_ROOT/dirstate" | cut -c-12) else @@ -396,7 +397,7 @@ function hg_prompt_vars { fi } -function nvm_version_prompt { +function nvm_version_prompt() { local node if _is_function nvm; then node=$(nvm current 2> /dev/null) @@ -405,36 +406,36 @@ function nvm_version_prompt { fi } -function node_version_prompt { +function node_version_prompt() { echo -e "$(nvm_version_prompt)" } -function rvm_version_prompt { - if which rvm &> /dev/null; then - rvm=$(rvm-prompt) || return - if [ -n "$rvm" ]; then +function rvm_version_prompt() { + if _command_exists rvm; then + rvm="$(rvm-prompt)" || return + if [[ -n "$rvm" ]]; then echo -e "$RVM_THEME_PROMPT_PREFIX$rvm$RVM_THEME_PROMPT_SUFFIX" fi fi } -function rbenv_version_prompt { +function rbenv_version_prompt() { if which rbenv &> /dev/null; then rbenv=$(rbenv version-name) || return rbenv commands | grep -q gemset && gemset=$(rbenv gemset active 2> /dev/null) && rbenv="$rbenv@${gemset%% *}" - if [ "$rbenv" != "system" ]; then + if [[ "$rbenv" != "system" ]]; then echo -e "$RBENV_THEME_PROMPT_PREFIX$rbenv$RBENV_THEME_PROMPT_SUFFIX" fi fi } -function rbfu_version_prompt { +function rbfu_version_prompt() { if [[ -n "${RBFU_RUBY_VERSION:-}" ]]; then echo -e "${RBFU_THEME_PROMPT_PREFIX}${RBFU_RUBY_VERSION}${RBFU_THEME_PROMPT_SUFFIX}" fi } -function chruby_version_prompt { +function chruby_version_prompt() { if _is_function chruby; then if _is_function chruby_auto; then chruby_auto @@ -449,81 +450,81 @@ function chruby_version_prompt { fi } -function ruby_version_prompt { +function ruby_version_prompt() { if [[ "${THEME_SHOW_RUBY_PROMPT:-}" == "true" ]]; then echo -e "$(rbfu_version_prompt)$(rbenv_version_prompt)$(rvm_version_prompt)$(chruby_version_prompt)" fi } -function k8s_context_prompt { +function k8s_context_prompt() { echo -e "$(kubectl config current-context 2> /dev/null)" } -function k8s_namespace_prompt { +function k8s_namespace_prompt() { echo -e "$(kubectl config view --minify --output 'jsonpath={..namespace}' 2> /dev/null)" } -function virtualenv_prompt { +function virtualenv_prompt() { if [[ -n "${VIRTUAL_ENV:-}" ]]; then virtualenv="${VIRTUAL_ENV##*/}" echo -e "$VIRTUALENV_THEME_PROMPT_PREFIX$virtualenv$VIRTUALENV_THEME_PROMPT_SUFFIX" fi } -function condaenv_prompt { +function condaenv_prompt() { if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then echo -e "${CONDAENV_THEME_PROMPT_PREFIX:-}${CONDA_DEFAULT_ENV}${CONDAENV_THEME_PROMPT_SUFFIX:-}" fi } -function py_interp_prompt { +function py_interp_prompt() { local py_version - py_version=$(python --version 2>&1 | awk 'NR==1{print "py-"$2;}') || return + py_version="$(python --version 2>&1 | awk 'NR==1{print "py-"$2;}')" || return echo -e "${PYTHON_THEME_PROMPT_PREFIX:-}${py_version}${PYTHON_THEME_PROMPT_SUFFIX:-}" } -function python_version_prompt { +function python_version_prompt() { echo -e "$(virtualenv_prompt)$(condaenv_prompt)$(py_interp_prompt)" } -function git_user_info { +function git_user_info() { # support two or more initials, set by 'git pair' plugin - SCM_CURRENT_USER=$(git config user.initials | sed 's% %+%') + SCM_CURRENT_USER="$(git config user.initials | sed 's% %+%')" # if `user.initials` weren't set, attempt to extract initials from `user.name` [[ -z "${SCM_CURRENT_USER}" ]] && SCM_CURRENT_USER=$(printf "%s" "$(for word in $(git config user.name | PERLIO=:utf8 perl -pe '$_=lc'); do printf "%s" "${word:0:1}"; done)") [[ -n "${SCM_CURRENT_USER}" ]] && printf "%s" "$SCM_THEME_CURRENT_USER_PREFFIX$SCM_CURRENT_USER$SCM_THEME_CURRENT_USER_SUFFIX" } -function clock_char { - CLOCK_CHAR=${THEME_CLOCK_CHAR:-"⌚"} - CLOCK_CHAR_COLOR=${THEME_CLOCK_CHAR_COLOR:-"$normal"} - SHOW_CLOCK_CHAR=${THEME_SHOW_CLOCK_CHAR:-"true"} +function clock_char() { + local clock_char clock_char_color show_clock_char + clock_char="${THEME_CLOCK_CHAR:-⌚}" + clock_char_color="${THEME_CLOCK_CHAR_COLOR:-${normal:-}}" + show_clock_char="${THEME_SHOW_CLOCK_CHAR:-"true"}" - if [[ "${SHOW_CLOCK_CHAR}" = "true" ]]; then - echo -e "${CLOCK_CHAR_COLOR}${CLOCK_CHAR_THEME_PROMPT_PREFIX}${CLOCK_CHAR}${CLOCK_CHAR_THEME_PROMPT_SUFFIX}" + if [[ "${show_clock_char}" == "true" ]]; then + echo -e "${clock_char_color}${CLOCK_CHAR_THEME_PROMPT_PREFIX}${clock_char}${CLOCK_CHAR_THEME_PROMPT_SUFFIX}" fi } -function clock_prompt { - CLOCK_COLOR=${THEME_CLOCK_COLOR:-"$normal"} - CLOCK_FORMAT=${THEME_CLOCK_FORMAT:-"%H:%M:%S"} - [[ -z "${THEME_SHOW_CLOCK:-}" ]] && THEME_SHOW_CLOCK=${THEME_CLOCK_CHECK:-"true"} - SHOW_CLOCK=$THEME_SHOW_CLOCK +function clock_prompt() { + local CLOCK_COLOR="${THEME_CLOCK_COLOR:-${normal?}}" + local CLOCK_FORMAT="${THEME_CLOCK_FORMAT:-"%H:%M:%S"}" + local SHOW_CLOCK="${THEME_SHOW_CLOCK:-${THEME_CLOCK_CHECK:-true}}" + local CLOCK_STRING="\D{${CLOCK_FORMAT}}" - if [[ "${SHOW_CLOCK}" = "true" ]]; then - CLOCK_STRING=$(date +"${CLOCK_FORMAT}") + if [[ "${SHOW_CLOCK}" == "true" ]]; then echo -e "${CLOCK_COLOR}${CLOCK_THEME_PROMPT_PREFIX}${CLOCK_STRING}${CLOCK_THEME_PROMPT_SUFFIX}" fi } -function user_host_prompt { - if [[ "${THEME_SHOW_USER_HOST}" = "true" ]]; then +function user_host_prompt() { + if [[ "${THEME_SHOW_USER_HOST}" == "true" ]]; then echo -e "${USER_HOST_THEME_PROMPT_PREFIX}\u@\h${USER_HOST_THEME_PROMPT_SUFFIX}" fi } # backwards-compatibility -function git_prompt_info { +function git_prompt_info() { _git-hide-status && return git_prompt_vars echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" @@ -534,7 +535,7 @@ function p4_prompt_info() { echo -e "${SCM_PREFIX}${SCM_BRANCH}:${SCM_CHANGE}${SCM_STATE}${SCM_SUFFIX}" } -function svn_prompt_info { +function svn_prompt_info() { svn_prompt_vars echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" } @@ -544,39 +545,32 @@ function hg_prompt_info() { echo -e "${SCM_PREFIX}${SCM_BRANCH}:${SCM_CHANGE#*:}${SCM_STATE}${SCM_SUFFIX}" } -function scm_char { +function scm_char() { scm_prompt_char echo -e "${SCM_THEME_CHAR_PREFIX}${SCM_CHAR}${SCM_THEME_CHAR_SUFFIX}" } -function prompt_char { +function prompt_char() { scm_char } -function battery_char { - if [[ "${THEME_BATTERY_PERCENTAGE_CHECK}" = true ]]; then - echo -e "${bold_red:-}$(battery_percentage)%" +function battery_char() { + # The battery_char function depends on the presence of the battery_percentage function. + if [[ "${THEME_BATTERY_PERCENTAGE_CHECK}" == true ]] && _command_exists battery_percentage; then + echo -e "${bold_red?}$(battery_percentage)%" + else + false fi } if ! _command_exists battery_charge; then # if user has installed battery plugin, skip this... function battery_charge() { - # no op - echo -n + : # no op } fi -# The battery_char function depends on the presence of the battery_percentage function. -# If battery_percentage is not defined, then define battery_char as a no-op. -if ! _command_exists battery_percentage; then - function battery_char() { - # no op - echo -n - } -fi - -function aws_profile { +function aws_profile() { if [[ -n "${AWS_DEFAULT_PROFILE:-}" ]]; then echo -e "${AWS_DEFAULT_PROFILE}" else From ac0d91b682402d916ff0055ecf59e7593e8fb303 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 19 Jan 2022 12:10:54 -0800 Subject: [PATCH 05/11] lib/theme.githelpers: remove dead code Five years deprecation is plenty warning. --- themes/githelpers.theme.bash | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/themes/githelpers.theme.bash b/themes/githelpers.theme.bash index 2fbb7e8a..719effec 100644 --- a/themes/githelpers.theme.bash +++ b/themes/githelpers.theme.bash @@ -171,35 +171,3 @@ function _git-remote-info() { fi fi } - -# Unused by bash-it, present for API compatibility -function git_status_summary() { - awk ' - BEGIN { - untracked=0; - unstaged=0; - staged=0; - } - { - if (!after_first && $0 ~ /^##.+/) { - print $0 - seen_header = 1 - } else if ($0 ~ /^\?\? .+/) { - untracked += 1 - } else { - if ($0 ~ /^.[^ ] .+/) { - unstaged += 1 - } - if ($0 ~ /^[^ ]. .+/) { - staged += 1 - } - } - after_first = 1 - } - END { - if (!seen_header) { - print - } - print untracked "\t" unstaged "\t" staged - }' -} From 2b3af0d8c99b068b41225a02db1de70a8e05972d Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 26 Jan 2022 11:02:11 -0800 Subject: [PATCH 06/11] lib/theme: eliminate a lot of subshells A lot of useless `echo`s in here. --- themes/base.theme.bash | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 1706eba4..61e9cb7a 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -206,14 +206,14 @@ function scm_prompt_info_common() { function terraform_workspace_prompt() { if _command_exists terraform; then if [[ -d .terraform ]]; then - echo -e "$(terraform workspace show 2> /dev/null)" + terraform workspace show 2> /dev/null fi fi } function active_gcloud_account_prompt() { if _command_exists gcloud; then - echo -e "$(gcloud config list account --format "value(core.account)" 2> /dev/null)" + gcloud config list account --format "value(core.account)" 2> /dev/null fi } @@ -291,7 +291,7 @@ function git_prompt_vars() { unstaged_count="${VCS_STATUS_NUM_UNSTAGED?}" staged_count="${VCS_STATUS_NUM_STAGED?}" else - IFS=$'\t' read -r untracked_count unstaged_count staged_count <<< "$(_git-status-counts)" + IFS=$'\t' read -r untracked_count unstaged_count staged_count < <(_git-status-counts) fi if [[ "${untracked_count}" -gt 0 || "${unstaged_count}" -gt 0 || "${staged_count}" -gt 0 ]]; then SCM_DIRTY=1 @@ -310,14 +310,17 @@ function git_prompt_vars() { SCM_PREFIX="${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" SCM_SUFFIX="${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" - SCM_CHANGE=$(_git-short-sha 2> /dev/null || echo "") + SCM_CHANGE=$(_git-short-sha 2> /dev/null || true) } function p4_prompt_vars() { + local opened_count non_default_changes default_count \ + add_file_count edit_file_count delete_file_count + IFS=$'\t' read -r \ opened_count non_default_changes default_count \ add_file_count edit_file_count delete_file_count \ - <<< "$(_p4-opened-counts)" + < <(_p4-opened-counts) if [[ "${opened_count}" -gt 0 ]]; then SCM_DIRTY=1 SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" @@ -334,7 +337,7 @@ function p4_prompt_vars() { } function svn_prompt_vars() { - if [[ -n $(svn status | head -c1 2> /dev/null) ]]; then + if [[ -n "$(svn status | head -c1 2> /dev/null)" ]]; then SCM_DIRTY=1 SCM_STATE="${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" else @@ -407,7 +410,7 @@ function nvm_version_prompt() { } function node_version_prompt() { - echo -e "$(nvm_version_prompt)" + nvm_version_prompt } function rvm_version_prompt() { @@ -457,14 +460,15 @@ function ruby_version_prompt() { } function k8s_context_prompt() { - echo -e "$(kubectl config current-context 2> /dev/null)" + kubectl config current-context 2> /dev/null } function k8s_namespace_prompt() { - echo -e "$(kubectl config view --minify --output 'jsonpath={..namespace}' 2> /dev/null)" + kubectl config view --minify --output 'jsonpath={..namespace}' 2> /dev/null } function virtualenv_prompt() { + local virtualenv if [[ -n "${VIRTUAL_ENV:-}" ]]; then virtualenv="${VIRTUAL_ENV##*/}" echo -e "$VIRTUALENV_THEME_PROMPT_PREFIX$virtualenv$VIRTUALENV_THEME_PROMPT_SUFFIX" @@ -507,13 +511,13 @@ function clock_char() { } function clock_prompt() { - local CLOCK_COLOR="${THEME_CLOCK_COLOR:-${normal?}}" - local CLOCK_FORMAT="${THEME_CLOCK_FORMAT:-"%H:%M:%S"}" - local SHOW_CLOCK="${THEME_SHOW_CLOCK:-${THEME_CLOCK_CHECK:-true}}" - local CLOCK_STRING="\D{${CLOCK_FORMAT}}" + local clock_color="${THEME_CLOCK_COLOR:-${normal?}}" + local clock_format="${THEME_CLOCK_FORMAT:-"%H:%M:%S"}" + local show_clock="${THEME_SHOW_CLOCK:-${THEME_CLOCK_CHECK:-true}}" + local clock_string="\D{${clock_format}}" - if [[ "${SHOW_CLOCK}" == "true" ]]; then - echo -e "${CLOCK_COLOR}${CLOCK_THEME_PROMPT_PREFIX}${CLOCK_STRING}${CLOCK_THEME_PROMPT_SUFFIX}" + if [[ "${show_clock}" == "true" ]]; then + echo -e "${clock_color}${CLOCK_THEME_PROMPT_PREFIX}${clock_string}${CLOCK_THEME_PROMPT_SUFFIX}" fi } From c6ac9109d71e833730149cb723442b17c8458531 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 26 Jan 2022 10:59:49 -0800 Subject: [PATCH 07/11] lib/theme: parameter cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improve handling of parameters by adding defaults (often blank). Alsö, eliminate newlines from `echo` in many places. --- themes/base.theme.bash | 265 ++++++++++++++++++++--------------------- 1 file changed, 131 insertions(+), 134 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 61e9cb7a..3455812f 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -88,21 +88,21 @@ RBENV_THEME_PROMPT_SUFFIX='|' RBFU_THEME_PROMPT_PREFIX=' |' RBFU_THEME_PROMPT_SUFFIX='|' -: "${GIT_EXE:=$SCM_GIT}" -: "${P4_EXE:=$SCM_P4}" -: "${HG_EXE:=$SCM_HG}" -: "${SVN_EXE:=$SCM_SVN}" +: "${GIT_EXE:=${SCM_GIT?}}" +: "${HG_EXE:=${SCM_HG?}}" +: "${SVN_EXE:=${SCM_SVN?}}" +: "${P4_EXE:=${SCM_P4?}}" function _bash_it_appearance_scm_init() { - GIT_EXE="$(type -P "${SCM_GIT}" || true)" - P4_EXE="$(type -P "${SCM_P4}" || true)" - HG_EXE="$(type -P "${SCM_HG}" || true)" - SVN_EXE="$(type -P "${SCM_SVN}" || true)" + GIT_EXE="$(type -P "${SCM_GIT:-git}" || true)" + HG_EXE="$(type -P "${SCM_HG:-hg}" || true)" + SVN_EXE="$(type -P "${SCM_SVN:-svn}" || true)" + P4_EXE="$(type -P "${SCM_P4:-p4}" || true)" # Check for broken SVN exe that is caused by some versions of Xcode. # See https://github.com/Bash-it/bash-it/issues/1612 for more details. - if [[ -x "$SVN_EXE" && -x "${SVN_EXE%/*}/xcrun" ]]; then - if ! "$SVN_EXE" --version > /dev/null 2>&1; then + if [[ -x "${SVN_EXE-}" && -x "${SVN_EXE%/svn}/xcrun" ]]; then + if ! "${SVN_EXE}" --version > /dev/null 2>&1; then # Unset the SVN exe variable so that SVN commands are avoided. SVN_EXE="" fi @@ -112,51 +112,61 @@ function _bash_it_appearance_scm_init() { _bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init') function scm() { - if [[ "$SCM_CHECK" == false ]]; then - SCM="$SCM_NONE" - elif [[ -f .git/HEAD ]] && [[ -x "$GIT_EXE" ]]; then - SCM="$SCM_GIT" - elif [[ -d .hg ]] && [[ -x "$HG_EXE" ]]; then - SCM="$SCM_HG" - elif [[ -d .svn ]] && [[ -x "$SVN_EXE" ]]; then - SCM="$SCM_SVN" - elif [[ -x "$GIT_EXE" ]] && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then - SCM="$SCM_GIT" - elif [[ -x "$HG_EXE" ]] && [[ -n "$(hg root 2> /dev/null)" ]]; then - SCM="$SCM_HG" - elif [[ -x "$SVN_EXE" ]] && [[ -n "$(svn info --show-item wc-root 2> /dev/null)" ]]; then - SCM="$SCM_SVN" - elif [[ -x "$P4_EXE" ]] && [[ -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then - SCM="$SCM_P4" + if [[ "${SCM_CHECK:-true}" == "false" ]]; then + SCM="${SCM_NONE-NONE}" + elif [[ -f .git/HEAD ]] && [[ -x "${GIT_EXE-}" ]]; then + SCM="${SCM_GIT?}" + elif [[ -d .hg ]] && [[ -x "${HG_EXE-}" ]]; then + SCM="${SCM_HG?}" + elif [[ -d .svn ]] && [[ -x "${SVN_EXE-}" ]]; then + SCM="${SCM_SVN?}" + elif [[ -x "${GIT_EXE-}" ]] && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then + SCM="${SCM_GIT?}" + elif [[ -x "${HG_EXE-}" ]] && [[ -n "$(hg root 2> /dev/null)" ]]; then + SCM="${SCM_HG?}" + elif [[ -x "${SVN_EXE-}" ]] && [[ -n "$(svn info --show-item wc-root 2> /dev/null)" ]]; then + SCM="${SCM_SVN?}" + elif [[ -x "${P4_EXE-}" ]] && [[ -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then + SCM="${SCM_P4?}" else - SCM="$SCM_NONE" + SCM="${SCM_NONE-NONE}" fi } function scm_prompt() { - local CHAR - CHAR="$(scm_char)" - local format="${SCM_PROMPT_FORMAT:-'[%s%s]'}" + local format="${SCM_PROMPT_FORMAT-"[%s%s]"}" + local scm_char scm_prompt_info + scm_char="$(scm_char)" + scm_prompt_info="$(scm_prompt_info)" - if [[ "${CHAR}" != "$SCM_NONE_CHAR" ]]; then + if [[ "${scm_char}" != "${SCM_NONE_CHAR:-}" ]]; then # shellcheck disable=2059 - printf "$format\n" "$CHAR" "$(scm_prompt_info)" + printf "${format}" "${scm_char}" "${scm_prompt_info}" fi } function scm_prompt_char() { - if [[ -z "$SCM" ]]; then scm; fi - if [[ $SCM == "$SCM_GIT" ]]; then - SCM_CHAR="$SCM_GIT_CHAR" - elif [[ $SCM == "$SCM_P4" ]]; then - SCM_CHAR="$SCM_P4_CHAR" - elif [[ $SCM == "$SCM_HG" ]]; then - SCM_CHAR="$SCM_HG_CHAR" - elif [[ $SCM == "$SCM_SVN" ]]; then - SCM_CHAR="$SCM_SVN_CHAR" - else - SCM_CHAR="$SCM_NONE_CHAR" + if [[ -z "${SCM:-}" ]]; then + scm fi + + case ${SCM?} in + "${SCM_GIT?}") + SCM_CHAR="${SCM_GIT_CHAR?}" + ;; + "${SCM_HG?}") + SCM_CHAR="${SCM_HG_CHAR?}" + ;; + "${SCM_SVN?}") + SCM_CHAR="${SCM_SVN_CHAR?}" + ;; + "${SCM_P4?}") + SCM_CHAR="${SCM_P4_CHAR?}" + ;; + *) + SCM_CHAR="${SCM_NONE_CHAR:-}" + ;; + esac } function scm_prompt_vars() { @@ -164,10 +174,9 @@ function scm_prompt_vars() { scm_prompt_char SCM_DIRTY=0 SCM_STATE='' - [[ $SCM == "$SCM_GIT" ]] && git_prompt_vars && return - [[ $SCM == "$SCM_P4" ]] && p4_prompt_vars && return - [[ $SCM == "$SCM_HG" ]] && hg_prompt_vars && return - [[ $SCM == "$SCM_SVN" ]] && svn_prompt_vars && return + + local prompt_vars="${SCM}_prompt_vars" + _is_function "${prompt_vars}" && "${prompt_vars}" } function scm_prompt_info() { @@ -178,29 +187,31 @@ function scm_prompt_info() { function scm_prompt_char_info() { scm_prompt_char - echo -ne "${SCM_THEME_CHAR_PREFIX}${SCM_CHAR}${SCM_THEME_CHAR_SUFFIX}" + echo -ne "${SCM_THEME_CHAR_PREFIX-}${SCM_CHAR?}${SCM_THEME_CHAR_SUFFIX-}" scm_prompt_info_common } function scm_prompt_info_common() { + local prompt_info SCM_DIRTY=0 SCM_STATE='' - if [[ ${SCM} == "${SCM_GIT}" ]]; then - if [[ ${SCM_GIT_SHOW_MINIMAL_INFO} == true ]]; then - # user requests minimal git status information - git_prompt_minimal_info - else - # more detailed git status - git_prompt_info - fi - return - fi - - # TODO: consider adding minimal status information for hg and svn - { [[ ${SCM} == "${SCM_P4}" ]] && p4_prompt_info && return; } || true - { [[ ${SCM} == "${SCM_HG}" ]] && hg_prompt_info && return; } || true - { [[ ${SCM} == "${SCM_SVN}" ]] && svn_prompt_info && return; } || true + case ${SCM?} in + "${SCM_GIT?}") + if [[ ${SCM_GIT_SHOW_MINIMAL_INFO:-false} == "true" ]]; then + # user requests minimal git status information + prompt_info="${SCM}_prompt_minimal_info" + else + # more detailed git status + prompt_info="${SCM}_prompt_info" + fi + ;; + *) + # TODO: consider adding minimal status information for hg and svn + prompt_info="${SCM}_prompt_info" + ;; + esac + _is_function "${prompt_info}" && "${prompt_info}" } function terraform_workspace_prompt() { @@ -218,25 +229,25 @@ function active_gcloud_account_prompt() { } function git_prompt_minimal_info() { - SCM_STATE="${SCM_THEME_PROMPT_CLEAN}" + SCM_STATE="${SCM_THEME_PROMPT_CLEAN?}" _git-hide-status && return - SCM_BRANCH="${SCM_THEME_BRANCH_PREFIX}\$(_git-friendly-ref)" + SCM_BRANCH="${SCM_THEME_BRANCH_PREFIX-}\$(_git-friendly-ref)" if [[ -n "$(_git-status | tail -n1)" ]]; then SCM_DIRTY=1 - SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" + SCM_STATE="${SCM_THEME_PROMPT_DIRTY?}" fi # Output the git prompt - SCM_PREFIX="${SCM_THEME_PROMPT_PREFIX}" - SCM_SUFFIX="${SCM_THEME_PROMPT_SUFFIX}" - echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" + SCM_PREFIX="${SCM_THEME_PROMPT_PREFIX-}" + SCM_SUFFIX="${SCM_THEME_PROMPT_SUFFIX-}" + echo -ne "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" } function git_prompt_vars() { - if "${SCM_GIT_USE_GITSTATUS:-false}" && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT:-}" == "ok-sync" ]]; then + if [[ "${SCM_GIT_USE_GITSTATUS:-false}" != "false" ]] && _command_exists gitstatus_query && gitstatus_query && [[ "${VCS_STATUS_RESULT:-}" == "ok-sync" ]]; then # we can use faster gitstatus # use this variable in githelpers and below to choose gitstatus output SCM_GIT_GITSTATUS_RAN=true @@ -300,15 +311,15 @@ function git_prompt_vars() { [[ "${unstaged_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_UNSTAGED_CHAR}${unstaged_count}" && SCM_DIRTY=2 [[ "${untracked_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_GIT_UNTRACKED_CHAR}${untracked_count}" && SCM_DIRTY=1 fi - SCM_STATE="${GIT_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" + SCM_STATE="${GIT_THEME_PROMPT_DIRTY:-${SCM_THEME_PROMPT_DIRTY?}}" fi fi # no if for gitstatus here, user extraction is not supported by it [[ "${SCM_GIT_SHOW_CURRENT_USER}" == "true" ]] && SCM_BRANCH+="$(git_user_info)" - SCM_PREFIX="${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" - SCM_SUFFIX="${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" + SCM_PREFIX="${GIT_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" + SCM_SUFFIX="${GIT_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" SCM_CHANGE=$(_git-short-sha 2> /dev/null || true) } @@ -323,65 +334,45 @@ function p4_prompt_vars() { < <(_p4-opened-counts) if [[ "${opened_count}" -gt 0 ]]; then SCM_DIRTY=1 - SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" - [[ "${opened_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_OPENED_CHAR}${opened_count}" - [[ "${non_default_changes}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_CHANGES_CHAR}${non_default_changes}" - [[ "${default_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_DEFAULT_CHAR}${default_count}" + SCM_STATE="${SCM_THEME_PROMPT_DIRTY?}" + [[ "${opened_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_OPENED_CHAR?}${opened_count}" + [[ "${non_default_changes}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_CHANGES_CHAR?}${non_default_changes}" + [[ "${default_count}" -gt 0 ]] && SCM_BRANCH+=" ${SCM_P4_DEFAULT_CHAR?}${default_count}" else SCM_DIRTY=0 - SCM_STATE="${SCM_THEME_PROMPT_DIRTY}" + SCM_STATE="${SCM_THEME_PROMPT_CLEAN?}" fi - SCM_PREFIX="${P4_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" - SCM_SUFFIX="${P4_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" + SCM_PREFIX="${P4_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" + SCM_SUFFIX="${P4_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" } function svn_prompt_vars() { if [[ -n "$(svn status | head -c1 2> /dev/null)" ]]; then SCM_DIRTY=1 - SCM_STATE="${SVN_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" + SCM_STATE="${SVN_THEME_PROMPT_DIRTY:-${SCM_THEME_PROMPT_DIRTY?}}" else SCM_DIRTY=0 - SCM_STATE="${SVN_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}" + SCM_STATE="${SVN_THEME_PROMPT_CLEAN:-${SCM_THEME_PROMPT_CLEAN?}}" fi - SCM_PREFIX="${SVN_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" - SCM_SUFFIX="${SVN_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" + SCM_PREFIX="${SVN_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" + SCM_SUFFIX="${SVN_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" SCM_BRANCH="$(svn info --show-item=url 2> /dev/null | awk -F/ '{ for (i=0; i<=NF; i++) { if ($i == "branches" || $i == "tags" ) { print $(i+1); break }; if ($i == "trunk") { print $i; break } } }')" || return SCM_CHANGE="$(svn info --show-item=revision 2> /dev/null)" } -# this functions returns absolute location of .hg directory if one exists -# It starts in the current directory and moves its way up until it hits /. -# If we get to / then no Mercurial repository was found. -# Example: -# - lets say we cd into ~/Projects/Foo/Bar -# - .hg is located in ~/Projects/Foo/.hg -# - get_hg_root starts at ~/Projects/Foo/Bar and sees that there is no .hg directory, so then it goes into ~/Projects/Foo -function get_hg_root() { - local CURRENT_DIR="${PWD}" - - while [[ "${CURRENT_DIR:-/}" != "/" ]]; do - if [[ -d "$CURRENT_DIR/.hg" ]]; then - echo "$CURRENT_DIR/.hg" - return - fi - - CURRENT_DIR="${CURRENT_DIR%/*}" - done -} - function hg_prompt_vars() { if [[ -n $(hg status 2> /dev/null) ]]; then SCM_DIRTY=1 - SCM_STATE="${HG_THEME_PROMPT_DIRTY:-$SCM_THEME_PROMPT_DIRTY}" + SCM_STATE="${HG_THEME_PROMPT_DIRTY:-${SCM_THEME_PROMPT_DIRTY?}}" else SCM_DIRTY=0 - SCM_STATE="${HG_THEME_PROMPT_CLEAN:-$SCM_THEME_PROMPT_CLEAN}" + SCM_STATE="${HG_THEME_PROMPT_CLEAN:-${SCM_THEME_PROMPT_CLEAN?}}" fi - SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX}" - SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX}" + SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" + SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" - HG_ROOT=$(get_hg_root) + HG_ROOT=$(_bash-it-find-in-ancestor ".hg") if [[ -f "$HG_ROOT/branch" ]]; then # Mercurial holds it's current branch in .hg/branch file @@ -405,7 +396,7 @@ function nvm_version_prompt() { if _is_function nvm; then node=$(nvm current 2> /dev/null) [[ "${node}" == "system" ]] && return - echo -e "${NVM_THEME_PROMPT_PREFIX}${node}${NVM_THEME_PROMPT_SUFFIX}" + echo -ne "${NVM_THEME_PROMPT_PREFIX-}${node}${NVM_THEME_PROMPT_SUFFIX-}" fi } @@ -417,7 +408,7 @@ function rvm_version_prompt() { if _command_exists rvm; then rvm="$(rvm-prompt)" || return if [[ -n "$rvm" ]]; then - echo -e "$RVM_THEME_PROMPT_PREFIX$rvm$RVM_THEME_PROMPT_SUFFIX" + echo -ne "${RVM_THEME_PROMPT_PREFIX-}${rvm}${RVM_THEME_PROMPT_SUFFIX-}" fi fi } @@ -427,14 +418,14 @@ function rbenv_version_prompt() { rbenv=$(rbenv version-name) || return rbenv commands | grep -q gemset && gemset=$(rbenv gemset active 2> /dev/null) && rbenv="$rbenv@${gemset%% *}" if [[ "$rbenv" != "system" ]]; then - echo -e "$RBENV_THEME_PROMPT_PREFIX$rbenv$RBENV_THEME_PROMPT_SUFFIX" + echo -ne "${RBENV_THEME_PROMPT_PREFIX-}${rbenv}${RBENV_THEME_PROMPT_SUFFIX-}" fi fi } function rbfu_version_prompt() { if [[ -n "${RBFU_RUBY_VERSION:-}" ]]; then - echo -e "${RBFU_THEME_PROMPT_PREFIX}${RBFU_RUBY_VERSION}${RBFU_THEME_PROMPT_SUFFIX}" + echo -ne "${RBFU_THEME_PROMPT_PREFIX-}${RBFU_RUBY_VERSION}${RBFU_THEME_PROMPT_SUFFIX-}" fi } @@ -449,13 +440,16 @@ function chruby_version_prompt() { if ! chruby | grep -q '\*'; then ruby_version="${ruby_version} (system)" fi - echo -e "${CHRUBY_THEME_PROMPT_PREFIX:-}${ruby_version}${CHRUBY_THEME_PROMPT_SUFFIX:-}" + echo -ne "${CHRUBY_THEME_PROMPT_PREFIX-}${ruby_version}${CHRUBY_THEME_PROMPT_SUFFIX-}" fi } function ruby_version_prompt() { if [[ "${THEME_SHOW_RUBY_PROMPT:-}" == "true" ]]; then - echo -e "$(rbfu_version_prompt)$(rbenv_version_prompt)$(rvm_version_prompt)$(chruby_version_prompt)" + rbfu_version_prompt + rbenv_version_prompt + rvm_version_prompt + chruby_version_prompt fi } @@ -471,32 +465,35 @@ function virtualenv_prompt() { local virtualenv if [[ -n "${VIRTUAL_ENV:-}" ]]; then virtualenv="${VIRTUAL_ENV##*/}" - echo -e "$VIRTUALENV_THEME_PROMPT_PREFIX$virtualenv$VIRTUALENV_THEME_PROMPT_SUFFIX" + echo -ne "${VIRTUALENV_THEME_PROMPT_PREFIX-}${virtualenv}${VIRTUALENV_THEME_PROMPT_SUFFIX-}" fi } function condaenv_prompt() { if [[ -n "${CONDA_DEFAULT_ENV:-}" ]]; then - echo -e "${CONDAENV_THEME_PROMPT_PREFIX:-}${CONDA_DEFAULT_ENV}${CONDAENV_THEME_PROMPT_SUFFIX:-}" + echo -ne "${CONDAENV_THEME_PROMPT_PREFIX-}${CONDA_DEFAULT_ENV}${CONDAENV_THEME_PROMPT_SUFFIX-}" fi } function py_interp_prompt() { local py_version py_version="$(python --version 2>&1 | awk 'NR==1{print "py-"$2;}')" || return - echo -e "${PYTHON_THEME_PROMPT_PREFIX:-}${py_version}${PYTHON_THEME_PROMPT_SUFFIX:-}" + echo -ne "${PYTHON_THEME_PROMPT_PREFIX-}${py_version}${PYTHON_THEME_PROMPT_SUFFIX-}" } function python_version_prompt() { - echo -e "$(virtualenv_prompt)$(condaenv_prompt)$(py_interp_prompt)" + virtualenv_prompt + condaenv_prompt + py_interp_prompt } function git_user_info() { + local current_user # support two or more initials, set by 'git pair' plugin - SCM_CURRENT_USER="$(git config user.initials | sed 's% %+%')" + current_user="$(git config user.initials | sed 's% %+%')" # if `user.initials` weren't set, attempt to extract initials from `user.name` - [[ -z "${SCM_CURRENT_USER}" ]] && SCM_CURRENT_USER=$(printf "%s" "$(for word in $(git config user.name | PERLIO=:utf8 perl -pe '$_=lc'); do printf "%s" "${word:0:1}"; done)") - [[ -n "${SCM_CURRENT_USER}" ]] && printf "%s" "$SCM_THEME_CURRENT_USER_PREFFIX$SCM_CURRENT_USER$SCM_THEME_CURRENT_USER_SUFFIX" + [[ -z "${current_user}" ]] && current_user=$(printf "%s" "$(for word in $(git config user.name | PERLIO=:utf8 perl -pe '$_=lc'); do printf "%s" "${word:0:1}"; done)") + [[ -n "${current_user}" ]] && printf "%s" "${SCM_THEME_CURRENT_USER_PREFFIX-}${current_user}${SCM_THEME_CURRENT_USER_SUFFIX-}" } function clock_char() { @@ -506,7 +503,7 @@ function clock_char() { show_clock_char="${THEME_SHOW_CLOCK_CHAR:-"true"}" if [[ "${show_clock_char}" == "true" ]]; then - echo -e "${clock_char_color}${CLOCK_CHAR_THEME_PROMPT_PREFIX}${clock_char}${CLOCK_CHAR_THEME_PROMPT_SUFFIX}" + echo -ne "${clock_char_color}${CLOCK_CHAR_THEME_PROMPT_PREFIX-}${clock_char}${CLOCK_CHAR_THEME_PROMPT_SUFFIX-}" fi } @@ -517,13 +514,13 @@ function clock_prompt() { local clock_string="\D{${clock_format}}" if [[ "${show_clock}" == "true" ]]; then - echo -e "${clock_color}${CLOCK_THEME_PROMPT_PREFIX}${clock_string}${CLOCK_THEME_PROMPT_SUFFIX}" + echo -ne "${clock_color}${CLOCK_THEME_PROMPT_PREFIX-}${clock_string}${CLOCK_THEME_PROMPT_SUFFIX-}" fi } function user_host_prompt() { - if [[ "${THEME_SHOW_USER_HOST}" == "true" ]]; then - echo -e "${USER_HOST_THEME_PROMPT_PREFIX}\u@\h${USER_HOST_THEME_PROMPT_SUFFIX}" + if [[ "${THEME_SHOW_USER_HOST:-false}" == "true" ]]; then + echo -ne "${USER_HOST_THEME_PROMPT_PREFIX-}\u@${THEME_PROMPT_HOST:-\h}${USER_HOST_THEME_PROMPT_SUFFIX-}" fi } @@ -531,27 +528,27 @@ function user_host_prompt() { function git_prompt_info() { _git-hide-status && return git_prompt_vars - echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" + echo -ne "${SCM_PREFIX?}${SCM_BRANCH?}${SCM_STATE?}${SCM_SUFFIX?}" } function p4_prompt_info() { p4_prompt_vars - echo -e "${SCM_PREFIX}${SCM_BRANCH}:${SCM_CHANGE}${SCM_STATE}${SCM_SUFFIX}" + echo -ne "${SCM_PREFIX?}${SCM_BRANCH?}:${SCM_CHANGE?}${SCM_STATE?}${SCM_SUFFIX?}" } function svn_prompt_info() { svn_prompt_vars - echo -e "${SCM_PREFIX}${SCM_BRANCH}${SCM_STATE}${SCM_SUFFIX}" + echo -ne "${SCM_PREFIX?}${SCM_BRANCH?}${SCM_STATE?}${SCM_SUFFIX?}" } function hg_prompt_info() { hg_prompt_vars - echo -e "${SCM_PREFIX}${SCM_BRANCH}:${SCM_CHANGE#*:}${SCM_STATE}${SCM_SUFFIX}" + echo -ne "${SCM_PREFIX?}${SCM_BRANCH?}:${SCM_CHANGE#*:}${SCM_STATE?}${SCM_SUFFIX?}" } function scm_char() { scm_prompt_char - echo -e "${SCM_THEME_CHAR_PREFIX}${SCM_CHAR}${SCM_THEME_CHAR_SUFFIX}" + echo -ne "${SCM_THEME_CHAR_PREFIX?}${SCM_CHAR?}${SCM_THEME_CHAR_SUFFIX?}" } function prompt_char() { @@ -561,7 +558,7 @@ function prompt_char() { function battery_char() { # The battery_char function depends on the presence of the battery_percentage function. if [[ "${THEME_BATTERY_PERCENTAGE_CHECK}" == true ]] && _command_exists battery_percentage; then - echo -e "${bold_red?}$(battery_percentage)%" + echo -ne "${bold_red?}$(battery_percentage)%" else false fi @@ -576,9 +573,9 @@ fi function aws_profile() { if [[ -n "${AWS_DEFAULT_PROFILE:-}" ]]; then - echo -e "${AWS_DEFAULT_PROFILE}" + echo -ne "${AWS_DEFAULT_PROFILE}" else - echo -e "default" + echo -ne "default" fi } From d86a182b6eb9d0f34c4a8f54196c38431f45eb4b Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 6 Feb 2022 16:55:32 -0800 Subject: [PATCH 08/11] lib/theme: export `$LS_COLORS` et al --- themes/base.theme.bash | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 3455812f..593bcc51 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -1,6 +1,10 @@ # shellcheck shell=bash # shellcheck disable=SC2034 # Expected behavior for themes. +# Colors for listing files, using default color scheme. +# To customize color scheme by theme, check out https://geoff.greer.fm/lscolors/ +export CLICOLOR LSCOLORS LS_COLORS + CLOCK_CHAR_THEME_PROMPT_PREFIX='' CLOCK_CHAR_THEME_PROMPT_SUFFIX='' CLOCK_THEME_PROMPT_PREFIX='' From c9efc161ff9a05d8512dc6e4bffeb24bb1df91c3 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 12 Feb 2022 21:08:29 -0800 Subject: [PATCH 09/11] lib/theme: improve performance of `scm()` - Don't invoke the source control utility when all we want to know is if we're somewhere inside the repository; use `_bash-it-find-in-ancestor()`. --- themes/base.theme.bash | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 593bcc51..440a817c 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -118,19 +118,13 @@ _bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init') function scm() { if [[ "${SCM_CHECK:-true}" == "false" ]]; then SCM="${SCM_NONE-NONE}" - elif [[ -f .git/HEAD ]] && [[ -x "${GIT_EXE-}" ]]; then + elif [[ -x "${GIT_EXE-}" ]] && _bash-it-find-in-ancestor '.git' > /dev/null; then SCM="${SCM_GIT?}" - elif [[ -d .hg ]] && [[ -x "${HG_EXE-}" ]]; then + elif [[ -x "${HG_EXE-}" ]] && _bash-it-find-in-ancestor '.hg' > /dev/null; then SCM="${SCM_HG?}" - elif [[ -d .svn ]] && [[ -x "${SVN_EXE-}" ]]; then + elif [[ -x "${SVN_EXE-}" ]] && _bash-it-find-in-ancestor '.svn' > /dev/null; then SCM="${SCM_SVN?}" - elif [[ -x "${GIT_EXE-}" ]] && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then - SCM="${SCM_GIT?}" - elif [[ -x "${HG_EXE-}" ]] && [[ -n "$(hg root 2> /dev/null)" ]]; then - SCM="${SCM_HG?}" - elif [[ -x "${SVN_EXE-}" ]] && [[ -n "$(svn info --show-item wc-root 2> /dev/null)" ]]; then - SCM="${SCM_SVN?}" - elif [[ -x "${P4_EXE-}" ]] && [[ -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then + elif [[ -x "${P4_EXE-}" && -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then SCM="${SCM_P4?}" else SCM="${SCM_NONE-NONE}" @@ -376,7 +370,7 @@ function hg_prompt_vars() { SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" - HG_ROOT=$(_bash-it-find-in-ancestor ".hg") + HG_ROOT="$(_bash-it-find-in-ancestor ".hg")/.hg" if [[ -f "$HG_ROOT/branch" ]]; then # Mercurial holds it's current branch in .hg/branch file From 7762aa687a6c60e4c7b49c3c741577e5c9b62149 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 23 Feb 2022 16:38:58 -0800 Subject: [PATCH 10/11] lib/theme: `local hg_root` in `hg_prompt_vars()` --- themes/base.theme.bash | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 440a817c..02602bc8 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -360,6 +360,7 @@ function svn_prompt_vars() { } function hg_prompt_vars() { + local hg_root bookmark if [[ -n $(hg status 2> /dev/null) ]]; then SCM_DIRTY=1 SCM_STATE="${HG_THEME_PROMPT_DIRTY:-${SCM_THEME_PROMPT_DIRTY?}}" @@ -370,20 +371,20 @@ function hg_prompt_vars() { SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" - HG_ROOT="$(_bash-it-find-in-ancestor ".hg")/.hg" + hg_root="$(_bash-it-find-in-ancestor ".hg")/.hg" - if [[ -f "$HG_ROOT/branch" ]]; then + if [[ -f "$hg_root/branch" ]]; then # Mercurial holds it's current branch in .hg/branch file - SCM_BRANCH=$(< "${HG_ROOT}/branch") - local bookmark="${HG_ROOT}/bookmarks.current" + SCM_BRANCH=$(< "${hg_root}/branch") + bookmark="${hg_root}/bookmarks.current" [[ -f "${bookmark}" ]] && SCM_BRANCH+=:$(< "${bookmark}") else SCM_BRANCH=$(hg summary 2> /dev/null | grep branch: | awk '{print $2}') fi - if [[ -f "$HG_ROOT/dirstate" ]]; then + if [[ -f "$hg_root/dirstate" ]]; then # Mercurial holds various information about the working directory in .hg/dirstate file. More on http://mercurial.selenic.com/wiki/DirState - SCM_CHANGE=$(hexdump -vn 10 -e '1/1 "%02x"' "$HG_ROOT/dirstate" | cut -c-12) + SCM_CHANGE=$(hexdump -vn 10 -e '1/1 "%02x"' "$hg_root/dirstate" | cut -c-12) else SCM_CHANGE=$(hg summary 2> /dev/null | grep parent: | awk '{print $2}') fi From df87b41635b5f01eeaa4b51ca6fbc4df5b5164c3 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 23 Feb 2022 16:54:47 -0800 Subject: [PATCH 11/11] lib/theme: use `_command_exists()` in `rbenv_version_prompt()` --- themes/base.theme.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 02602bc8..92a56e5e 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -413,7 +413,7 @@ function rvm_version_prompt() { } function rbenv_version_prompt() { - if which rbenv &> /dev/null; then + if _command_exists rbenv; then rbenv=$(rbenv version-name) || return rbenv commands | grep -q gemset && gemset=$(rbenv gemset active 2> /dev/null) && rbenv="$rbenv@${gemset%% *}" if [[ "$rbenv" != "system" ]]; then