Use global PLATFORM. Add dynamic .nanorc support. Fix git prompt.

This commit is contained in:
Darren 'Tadgy' Austin 2023-10-01 20:40:46 +01:00
commit 06d1bab7a6

69
.bashrc
View file

@ -121,6 +121,32 @@ __prompt_user_colour() {
return "$RET"
}
__nanorc_prompt_command() {
hash nano >/dev/null 2>&1 && {
# shellcheck disable=SC2155
local NANO_VER="$(command nano --version | awk '/version/ { print $4 }' | cut -d. -f1)"
# Darwin specifc .nanorc version.
[[ "$PLATFORM" == "Darwin" ]] && NANO_VER="darwin"
if [[ -f "$HOME/.nanorc-$NANO_VER" ]]; then
if (( NANO_VER <= 4 )); then
( cd "$HOME" && ln -sf ".nanorc-$NANO_VER" ".nanorc" )
else
# shellcheck disable=SC2139
alias nano="nano -f \"$HOME/.nanorc-$NANO_VER\""
fi
else
if (( NANO_VER <= 4 )); then
[[ -L "$HOME/.nanorc" ]] && rm -f "$HOME/.nanorc"
else
unalias nano
fi
printf "\\033[1;31;40m%s\\033[0;39m\\n" "No .nanorc for version '$NANO_VER'." >&2
fi
}
}
__find_ssh_agent_sock() {
# Find an *active* ssh agent socket.
# Returns: 0 = Found an active socket.
@ -145,13 +171,10 @@ __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.
local ERR FD I PLATFORM SOCK
local ERR FD I SOCK
[[ ! -e "$HOME/.ssh/agents" ]] && touch "$HOME/.ssh/agents"
# Determine the platform being logged into.
PLATFORM="$(uname -s)"
# Lock the ~/.ssh/agents file.
if [[ "$PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
@ -235,9 +258,6 @@ __write_ssh_agents() {
(( $? <= 1 )) && SOCKS+=("${SSH_AUTH_SOCKS[$I]}")
done
# Determine the platform being logged into.
PLATFORM="$(uname -s)"
# Lock the ~/.ssh/agents file.
if [[ "$PLATFORM" == "Linux" ]]; then
# Make sure SSH_AUTH_SOCKS has the most up to date data.
@ -361,7 +381,7 @@ HISTTIMEFORMAT="%d/%m/%y %H:%M:%S "
history -r
# The commands to execute before the prompt is displayed.
PROMPT_COMMAND="__ssh_agent_prompt_command"
PROMPT_COMMAND="__nanorc_prompt_command; __ssh_agent_prompt_command"
# Git prompt options.
GIT_PROMPT_SHOW_TYPE=1
@ -380,12 +400,12 @@ if (( BASH_VERSINFO[0] >= 4 )); then
# Trim the path in the prompt.
PROMPT_DIRTRIM=2
# Coloured username + host + directory:
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->'
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\]$(__prompt_git_status "\[\\033[0;35;40m\] (%s)\[\\033[0;37;40m\]") ->'
else
# Set the prompts.
# Coloured username + host + directory:
# shellcheck disable=SC2154
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '\''{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}'\'')\[$(tput sgr0)\]$(__prompt_git_status "\[\033[0;35;40m\] (%s)\[\033[0;37;40m\]") ->'
PS1='[\[$(tput bold)$(tput setaf "$(__prompt_user_colour)")\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '\''{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}'\'')\[$(tput sgr0)\]$(__prompt_git_status "\[\\033[0;35;40m\] (%s)\[\\033[0;37;40m\]") ->'
fi
# Set the debugger prompt.
@ -434,22 +454,6 @@ PLATFORM="$(uname -s)"
# Platform specific set up.
if [[ "$PLATFORM" = "Linux" ]]; then
# Linux specific functions.
hash nano >/dev/null 2>&1 && nano() {
# shellcheck disable=SC2155
local NANO_VER="$(command nano --version | awk '/version/ { print $4 }' | cut -d. -f1)"
if [[ -f "$HOME/.nanorc-$NANO_VER" ]]; then
if (( NANO_VER <= 4 )); then
( cd "$HOME" && ln -sf ".nanorc-$NANO_VER" ".nanorc" )
command nano "$@"
rm -f "$HOME/.nanorc"
else
command nano -f "$HOME/.nanorc-$NANO_VER" "$@"
fi
else
printf "%s: %s\\n" "${FUNCNAME[0]}" "no .nanorc for version '$NANO_VER'" >&2
return 1
fi
}
hash ps grep >/dev/null 2>&1 && psgrep() {
if [[ -n "$1" ]]; then
# shellcheck disable=SC2009
@ -473,18 +477,6 @@ if [[ "$PLATFORM" = "Linux" ]]; then
hash pinfo >/dev/null 2>&1 && alias info='pinfo'
hash ping >/dev/null 2>&1 && alias ping='ping -b'
elif [[ "$PLATFORM" = "Darwin" ]]; then
# Darwin specific functions.
hash nano >/dev/null 2>&1 && nano() {
if [[ -f "$HOME/.nanorc-darwin" ]]; then
( cd "$HOME" && ln -sf ".nanorc-darwin" ".nanorc" )
command nano "$@"
rm -f "$HOME/.nanorc"
else
printf "%s: %s\\n" "${FUNCNAME[0]}" "no .nanorc-darwin found" >&2
return 1
fi
}
# Darwin specific aliases.
hash df >/dev/null 2>&1 && alias df='df -P'
hash ls >/dev/null 2>&1 && alias ls='ls -bFG'
@ -492,4 +484,3 @@ elif [[ "$PLATFORM" = "Darwin" ]]; then
else
echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2
fi
unset PLATFORM