1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
common-configs/bash/bash_aliases.sh

117 lines
3.9 KiB

#!/bin/bash
# Disable the gosh darn XON/XOFF function that serves no purpose but to confuse me when I accidentally press Ctrl+S
stty -ixon
# Find the path of the git repository relative to this file.
export __COMMON_CONFIGS=$(readlink -e "$( dirname "$(realpath -e "${BASH_SOURCE[0]}")" )/..")
# Set the SSH auth sock to the central location
if [[ -n $SSH_AUTH_SOCK ]] && [[ $SSH_AUTH_SOCK != $HOME/.ssh/ssh_auth_sock ]]; then
ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/ssh_auth_sock"
fi
export SSH_AUTH_SOCK="$HOME/.ssh/ssh_auth_sock"
# Set TZ if it's not set
LOCALZONE=$(readlink -e /etc/localtime); export TZ=${TZ:-${LOCALZONE#/usr/share/zoneinfo/}}
# Install EDITOR
export EDITOR='nvim'
# Install PAGER
export PAGER='less --mouse'
# Install BROWSER
export BROWSER='firefox -P Mari --class Firefox.Main'
# Set up the vim config path
export VIM_CONFIG_DIR=$__COMMON_CONFIGS/vim
# Set potentially-destructive commands to use interactive modes
alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"
# Use a larger number of KDF rounds and the secure ed25519 key type by default.
alias ssh-keygen="ssh-keygen -o -a 100 -b 4096 -t ed25519"
# Reload this file's (and other customizations') aliases.
function reload_bashrc()
{
. "$HOME"/.bashrc
}
# Check if the git repository is fully synced.
function __common_configs_in_sync()
{
(
# Failure modes, in order:
# exit 9: Problem cd'ing or getting commit hashes - things that should never fail
cd "$__COMMON_CONFIGS" || exit 9
localCommit=$(git show-ref --verify --hash refs/heads/main) || exit 9
if [[ $1 == --force-fetch ]] || [[ ! -f .last-fetch ]] || [[ $(date --reference=.last-fetch +%s) -lt $(date --date='5 minutes ago' +%s) ]]; then
git fetch --quiet origin && touch .last-fetch
fi
remoteCommit=$(git show-ref --verify --hash refs/remotes/origin/main) || exit 9
# exit 1: master is not the commit which is checked out.
[[ $(git symbolic-ref HEAD) == "refs/heads/main" ]] || exit 1
# exit 2: master and origin/master are different.
[[ $localCommit == $remoteCommit ]] || exit 2
# exit 3: There are staged changes.
git diff-index --cached --exit-code --quiet HEAD || exit 3
# exit 4: There are unstaged changes.
git diff-files --exit-code --quiet || exit 4
)
}
function help_prompt() {
echo '\ Help\ page\ '"$@"'\ ?ltline\ %lt?L/%L.:byte\ %bB?s/%s..?\ (END):?pB\ %pB\\%..(press h for help or q to quit)'
}
function man ()
{
if command man "$@" >&/dev/null; then
command man "$@"
elif builtin help -m "$@" >&/dev/null; then
builtin help -m "$@" | command man -l -r "$(help_prompt "$@")" -
else
command man "$@"
fi
}
function help ()
{
if builtin help -m "$@" >&/dev/null; then
builtin help -m "$@" | command man -l -r "$(help_prompt "$@")" -
elif command man "$@" >&/dev/null; then
command man "$@"
else
builtin help -m "$@"
fi
}
source "$__COMMON_CONFIGS"/bash/bash_tmux.sh
PROMPT_COMMAND=""
export GITAWAREPROMPT="$__COMMON_CONFIGS"/bash/git-aware-prompt
source "$GITAWAREPROMPT"/main.sh
function __prettylastexit() {
local exitcode=$?
if [[ -z $exitcode ]]; then
echo "[---]$txtrst "
elif [[ $exitcode == 0 ]]; then
echo "$txtgrn[ OK]$txtrst "
else
printf "$txtred[%3.3s]$txtrst " "$exitcode"
fi
}
function __prettyconfigstate() {
__common_configs_in_sync
local exitcode=$?
case $exitcode in
0) ;;
2) echo; echo "${txtgrn}<config update ready>${txtrst}"; ;;
3) echo; echo "${txtylw}<config change in progress>${txtrst}"; ;;
4) echo; echo "${txtylw}<config change in progress>${txtrst}"; ;;
*) echo; echo "${txtred}<config error>$txtrst" ;;
esac
}
PS1='$(__prettylastexit)\[$bldred\]${debian_chroot:+($debian_chroot)}\[$txtrst\]\[$txtpur\]\u\[$txtrst\]\[$txtylw\]@\[$txtrst\]\[$txtblu\]\h\[$txtrst\]:\[$bldgrn\]\w\[$txtrst\] \[$txtcyn\]$git_branch\[$txtrst\]\[$bldred\]${git_dirty}\[$txtrst\]$(__prettyconfigstate)\n\$ '