From 63486778767cdfeff00e1df45419626ca817fddd Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Wed, 25 Oct 2023 16:05:55 +0100 Subject: [PATCH] Clean up .bashrc. --- .bashrc | 67 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/.bashrc b/.bashrc index 77ff298..5ccfff0 100644 --- a/.bashrc +++ b/.bashrc @@ -21,20 +21,16 @@ __prompt_git_status() { # GIT_PROMPT_SHOW_STASH=1 Show a $ if there is a stash in this repository (superceeded by above). # Displays: The printf formatted git prompt based upon $1 and the environment vaiables above, for example: # S:branch_name >5 * - # Returns: Exit code of the command line before entering this function. - # shellcheck disable=SC2155 - local RET="$?" BRANCH COUNT GIT_PROMPT GIT_PROMPT_MARKER_SET GIT_REPO_INFO PWD=$(pwd -P) + # Returns: 0 = Produced a prompt successfully. + # 1 = An error occured. + local BRANCH COUNT GIT_PROMPT GIT_PROMPT_MARKER_SET GIT_REPO_INFO IFS= - # Bail out if there's no format argument given. - (( $# != 1 )) && return "$RET" + # Bail out if there's no format argument given, or it doesn't contain %s + (( $# != 1 )) || [[ "$1" != *%s* ]] && return 1 # Get some repository information. # shellcheck disable=SC2207 - IFS=$'\n' GIT_REPO_INFO=( $( git rev-parse --is-bare-repository --is-shallow-repository --is-inside-git-dir --is-inside-work-tree 2>/dev/null) ) - ERR="$?" - - # Do nothing if there's an error. - (( ERR >= 1 )) && return "$RET" + GIT_REPO_INFO=( $( git rev-parse --is-bare-repository --is-shallow-repository --is-inside-git-dir --is-inside-work-tree 2>/dev/null) ) || return 1 # Generate the prompt. if [[ "${GIT_REPO_INFO[2]}" == "true" ]]; then @@ -140,13 +136,11 @@ __prompt_git_status() { # shellcheck disable=SC2059 printf -- "$1" "$GIT_PROMPT" - # Return the original error code. - return "$RET" + return 0 } __prompt_user_colour() { # Determine the colour of the username in the prompt. - local RET="$?" if [[ "$LOGNAME" == "root" ]]; then printf "%s" "1;31m" # Bright Red. @@ -156,29 +150,33 @@ __prompt_user_colour() { printf "%s" "1;36m" # Bright Cyan. fi - return "$RET" + return 0 } __git_prompt_command() { # Perform git actions. # Environment variables: - # GIT_DISABLE_PROMPT_PREFETCH=1 Disable automatic 'prefetch' of upstream refs. - # This can also be disabled on a per repository basis using: - # git config --add --local --bool script.DisablePromptPrefetch true - # Returns: Exit code of the command line before entering this function. - local RET="$?" + # GIT_DISABLE_PROMPT_PREFETCH=1 Disable automatic 'prefetch' of upstream refs. + # This can also be disabled on a per repository basis using: + # git config --add --local --bool script.DisablePromptPrefetch true + # Returns: 0 = Tasks completed successfully. + # 1 = An error occured. # Run prefetch tasks if inside a work directory. [[ -z "$GIT_DISABLE_PROMPT_PREFETCH" ]] && [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]] && \ [[ "$(git config --get --local --bool script.DisablePromptPrefetch 2>/dev/null)" != "true" ]] && { - git maintenance run --task=prefetch 2>/dev/null || printf "\\033[1;31m%s\\033[39m\\n" "Git maintenance 'prefetch' task failed." >&2 + git maintenance run --task=prefetch 2>/dev/null || { + printf "\\033[1;31m%s\\033[39m\\n" "Git maintenance 'prefetch' task failed." >&2 + return 1 + } } - return "$RET" + return 0 } __nanorc_prompt_command() { # Dynamically handle .nanorc file versions. + hash nano >/dev/null 2>&1 && { # shellcheck disable=SC2155 local NANO_VER="$(command nano --version | awk '/version/ { print $4 }' | cut -d. -f1)" @@ -202,12 +200,14 @@ __nanorc_prompt_command() { printf "\\033[1;31m%s\\033[39m\\n" "No .nanorc for version '$NANO_VER'." >&2 fi } + + return 0 } __ssh_agent_prompt_command() { # If necessary, find and activate a new ssh agent socket before each prompt is displayed. - # Returns: 0 = All is good. - # 1 = An error occured. + # Returns: 0 = All is good. + # 1 = An error occured. local ERR if [[ -z "$SSH_AUTH_SOCK" ]]; then @@ -240,13 +240,14 @@ __ssh_agent_prompt_command() { sleep 0.5 fi } + return 0 } __find_ssh_agent_sock() { # Find an *active* ssh agent socket. - # Returns: 0 = Found an active socket. - # 1 = Did not find a viable socket. + # Returns: 0 = Found an active socket. + # 1 = Did not find a viable socket. local I # Search the SSH_AUTH_SOCKS array for a viable socket. @@ -265,8 +266,8 @@ __find_ssh_agent_sock() { __read_ssh_agents() { # Read all the known ssh agent sockets into an array. - # Returns: 0 = Processed and read the agents file without issue. - # 1 = Error processing/reading the agents file. + # Returns: 0 = Processed and read the agents file without issue. + # 1 = Error processing/reading the agents file. local ERR I SOCK [[ ! -e "$HOME/.ssh/agents" ]] && touch "$HOME/.ssh/agents" @@ -330,14 +331,15 @@ __read_ssh_agents() { unset SSH_AUTH_SOCKS SSH_AGENTS_MTIME return 1 } + return 0 } __write_ssh_agents() { # Write all unique ssh agent sockets into the ~/.ssh/agents file. - # Returns: 0 = Processed and wrote the agents file without issue. - # 1 = Error processing/writing the agents file. - # 2 = The SSH_AUTH_SOCKS array may be out of date as the agents file'a mtime has changed. + # Returns: 0 = Processed and wrote the agents file without issue. + # 1 = Error processing/writing the agents file. + # 2 = The SSH_AUTH_SOCKS array may be out of date as the agents file'a mtime has changed. local ERR I J MTIME SOCKS # Add the current agent socket to the sockets array. @@ -411,11 +413,14 @@ __write_ssh_agents() { printf "\\033[1;31m%s\\033[39m\\n" "Failed to write ssh-agent socket list." >&2 return 1 fi + return 0 } imagebin() { - [[ -z "$1" ]] && { + # Throw an image file into an imagebin. + + [[ -z "$1" ]] || [[ ! -e "$1" ]] && { printf "%s: %s\\n" "Usage" "${FUNCNAME[0]} " >&2 return 1 }