profile/home/dotfiles/zshrc

344 lines
8.9 KiB
Bash

#!/bin/zsh
when_binary_available() {
if has_binary "$@"; then
printf '1'
return
fi
printf '0'
return
}
has_binary() {
command -v "$@" >/dev/null 2>&1
}
ZSH_DEBUG="${ZSH_DEBUG:-0}"
ZSH_ALIAS=${ZSH_ALIAS:-1}
ZSH_BAT=${ZSH_BAT:-$(when_binary_available bat)}
ZSH_COMPAT_BASH_COMPLETION=${ZSH_COMPAT_BASH_COMPLETION:-1}
ZSH_CUSTOM=${ZSH_CUSTOM:-1}
ZSH_DEFAULT_TERM=${ZSH_DEFAULT_TERM:-1}
ZSH_GCLOUD_FIXES=${ZSH_GCLOUD_FIXES:=$(when_binary_available gcloud)}
ZSH_GPG_PINENTRY_FIX=${ZSH_GPG_PINENTRY_FIX:-1}
ZSH_GPG_SSH_AGENT=${ZSH_GPG_SSH_AGENT:-$(has_binary gpgconf && [ -z "${SSH_AUTH_SOCK:-}" ] && [ -f $(gpgconf --list-dirs agent-ssh-socket) ] && printf 1 || printf 0)}
ZSH_KEYBOARD=${ZSH_KEYBOARD:-1}
ZSH_LOGO_LS=${ZSH_LOGO_LS:-$(when_binary_available logo-ls)}
ZSH_OPAM=${ZSH_OPAM:-$(when_binary_available opam)}
ZSH_PIPX=${ZSH_PIPX:-$(when_binary_available pipx)}
ZSH_POWERLINE="${ZSH_POWERLINE:-1}"
ZSH_PRESERVE_HISTORY="${ZSH_PRESERVE_HISTORY:-1}"
ZSH_PRESERVE_HISTORY_IMMEDIATELY="${ZSH_PRESERVE_HISTORY_IMMEDIATELY:-0}"
ZSH_PRINT_REBOOT_REQUIRED=${ZSH_PRINT_REBOOT_REQUIRED:-0} # hacky, see below
ZSH_ROOT_ALIAS="${ZSH_ROOT_ALIAS:-1}"
ZSH_SYMFONY_CLI="${ZSH_SYMFONY_CLI:-$(when_binary_available symfony)}"
ZSH_SYNTAX_HIGHLIGHTING="${ZSH_SYNTAX_HIGHLIGHTING:-1}"
ZSH_THEFUCK=${ZSH_THEFUCK:-1}
ZSH_VIDDY=${ZSH_VIDDY:-$(when_binary_available viddy)}
ZSH_DEBUG_SECTION_START_PREFIX="\\ "
ZSH_DEBUG_INDENT="${ZSH_DEBUG_INDENT:- |}"
ZSH_DEBUG_SECTION_END_TEXT="/"
DEBUG_INDENT=0
section() {
if [ $ZSH_DEBUG -gt 0 ]; then
echo "${ZSH_DEBUG_SECTION_START_PREFIX}$(tput smso)$*$(tput rmso)"
fi
((DEBUG_INDENT++))
}
section_end() {
if [ $ZSH_DEBUG -gt 0 ]; then
echo "$ZSH_DEBUG_SECTION_END_TEXT"
fi
((DEBUG_INDENT--))
}
log() {
if [ $ZSH_DEBUG -gt 0 ]; then
indent=""
if [ $DEBUG_INDENT -gt 0 ]; then
for i in $(seq 0 $((DEBUG_INDENT - 1))); do
indent+="$ZSH_DEBUG_INDENT"
done
fi
echo "$*" | while IFS=$'\n' read line; do
echo "$indent" "$line"
done
fi
}
# Compatibility mode for completions
section "compat: bash completion"
if [ $ZSH_COMPAT_BASH_COMPLETION -gt 0 ] || [ $ZSH_PIPX -gt 0 ]; then
log "Enabling compatibility for bash completion scripts..."
autoload -Uz bashcompinit compinit
bashcompinit
compinit
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'
fpath=(/usr/local/share/zsh-completions $fpath)
fi
section_end
# Custom variables
section "custom zshrc"
if [ $ZSH_CUSTOM -gt 0 ]; then
log "Checking if local profile code exists at ~/.local_zshrc..."
if [ -f ~/.local_zshrc ]; then
log "Exists, loading."
# shellcheck source=/dev/null
. ~/.local_zshrc
else
log "Does not exist, skipping."
fi
fi
section_end
# Bin directories
section "python bin"
if [ -d "$HOME/Library/Python/3.7" ]; then
PATH="$HOME/Library/Python/3.7/bin:$PATH"
export PATH
fi
if [ -d "$HOME/Library/Python/2.7" ]; then
PATH="$HOME/Library/Python/2.7/bin:$PATH"
export PATH
fi
section_end
section "pipx bin"
if [ $ZSH_PIPX -gt 0 ]; then
autoload -U bashcompinit
bashcompinit
eval "$(register-python-argcomplete pipx)"
fi
section_end
# Locale fix
section "locale fix"
if [ "$(uname -s)" = "Darwin" ]; then
if [ -z "$LC_ALL" ]; then
export LC_ALL=C
fi
if [ -z "$LANG" ]; then
export LANG=C
fi
fi
section_end
# Powerline
section "powerline"
if [ $ZSH_POWERLINE -gt 0 ]; then
log "Running powerline daemon..."
log "$(powerline-daemon 2>&1)"
log "Finding powerline..."
POWERLINE_DIR="$(pipx runpip powerline-status show powerline-status --isolated | grep '^Location:' | awk '{print $2}')"
log "Powerline repository root: $POWERLINE_DIR"
log "Preparing zstyle..."
autoload -Uz promptinit
promptinit
prompt off
log "Loading powerline zsh plugin..."
source "$POWERLINE_DIR/powerline/bindings/zsh/powerline.zsh"
#prompt -s powerline
fi
section_end
# zsh-syntax-highlighting
section "zsh-syntax-highlighting"
if [ $ZSH_SYNTAX_HIGHLIGHTING -gt 0 ]; then
log "Detecting highlighters directory..."
if [ -d "/usr/share/zsh-syntax-highlighting/highlighters" ]; then
export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR="/usr/share/zsh-syntax-highlighting/highlighters"
elif [ -d "/usr/local/share/zsh-syntax-highlighting/highlighters" ]; then
export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR="/usr/local/share/zsh-syntax-highlighting/highlighters"
fi
log "Loading plugin..."
if [ -f "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]; then
source "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [ -f "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]; then
source "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
else
source "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
fi
fi
section_end
# opam
section "opam"
if [ $ZSH_OPAM -gt 0 ]; then
[[ ! -r ~/.opam/opam-init/init.zsh ]] || source ~/.opam/opam-init/init.zsh >/dev/null 2>/dev/null
fi
section_end
# gcloud fixes
section "gcloud fixes"
if [ $ZSH_GCLOUD_FIXES -gt 0 ]; then
log "Overriding gcloud command"
_local_zshrc_ssh_config_needs_cleanup() {
[ ! -f ~/.ssh/config ] || grep -iq '^\s\+IdentityFile' ~/.ssh/config
}
_local_zshrc_clean_up_ssh_config() {
if [ -f ~/.ssh/config ]; then
# avoid apps such as gcloud referencing their own SSH key, we have our own hardware-backed one, damn it!
sed -i -e 's/^\(\s\+\)IdentityFile/\1# Modified by ~\/.local_zshrc\n\1#IdentityFile/i' ~/.ssh/config
fi
}
gcloud() {
command gcloud "$@"
if _local_zshrc_ssh_config_needs_cleanup; then
_local_zshrc_clean_up_ssh_config
fi
}
if has_binary gke-gcloud-auth-plugin; then
# avoid deprecation warning about gcloud auth by simply using the new plugin
log "Setting USE_GKE_GCLOUD_AUTH_PLUGIN=True to avoid deprecation warning"
USE_GKE_GCLOUD_AUTH_PLUGIN=True
fi
fi
section_end
# symfony cli
section "symfony cli"
if [ $ZSH_SYMFONY_CLI -gt 0 ]; then
if [ -n "$(command -v symfony)" ]; then
complete -C "$(command -v symfony) self:autocomplete" symfony
fi
fi
section_end
section "thefuck"
if [ $ZSH_THEFUCK -gt 0 ]; then
eval "$(thefuck --alias)"
fi
section_end
# Default terminal
section "term"
if [ $ZSH_DEFAULT_TERM -gt 0 ] && [ -z "$TERM" ]; then
export TERM=linux
fi
section_end
# gpg password via console
section "gpg pinentry fix"
if [ $ZSH_GPG_PINENTRY_FIX -gt 0 ]; then
log "Changing GPG_TTY to: $(tty)"
GPG_TTY=$(tty)
export GPG_TTY
if [ ! -z "$SSH_CONNECTION" ]; then
log "Detected SSH connection, making pinentry use curses instead of x11."
export PINENTRY_USER_DATA="USE_CURSES=1"
else
log "All good with pinentry itself it seems."
fi
fi
section_end
# gpg ssh agent
section "gpg ssh agent"
if [ $ZSH_GPG_SSH_AGENT -gt 0 ]; then
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
log "Changed SSH_AUTH_SOCK to $SSH_AUTH_SOCK (GnuPG SSH agent)"
gpgconf --launch gpg-agent
fi
section_end
# logo-ls
section "logo-ls"
if [ $ZSH_LOGO_LS -gt 0 ]; then
log "Aliasing ls to logo-ls"
alias ls=logo-ls
fi
section_end
# bat
section "bat"
if [ $ZSH_BAT -gt 0 ] && command -v bat >/dev/null; then
log "Aliasing cat to bat"
alias cat='bat --paging=never'
fi
section_end
# viddy
section "viddy"
if [ $ZSH_VIDDY -gt 0 ]; then
log "Aliasing watch to viddy"
alias watch=viddy
fi
section_end
# root alias (sudo -EHs)
section "root alias"
if [ $ZSH_ROOT_ALIAS -gt 0 ]; then
log "Aliasing root to sudo -EHs"
alias root="sudo -EHs"
fi
section_end
# keyboard fixes
section "keyboard fixes"
if [ $ZSH_KEYBOARD -gt 0 ]; then
default_keyboard_profile="xterm-256color-ssh"
if [ -z "$SSH_CONNECTION" ]; then
keyboard_profile="$TERM-${${DISPLAY:t}:-$VENDOR-$OSTYPE}"
else
keyboard_profile="$TERM-ssh"
fi
if [ -f ~/.zkbd/"$keyboard_profile" ]
then
log "Loading keyboard profile: $HOME/.zkbd/$keyboard_profile"
. ~/.zkbd/"$keyboard_profile"
else
log "Loading default keyboard profile: $HOME/.zkbd/$default_keyboard_profile"
. ~/.zkbd/"$default_keyboard_profile"
fi
# common assignments
bindkey "${key[Delete]}" delete-char
bindkey "${key[Home]}" beginning-of-line
bindkey "${key[End]}" end-of-line
fi
section_end
# Reboot required? (hacky code)
section "reboot required?"
if [ $ZSH_PRINT_REBOOT_REQUIRED -gt 0 ]; then
if [ "$(pacman -Q linux | cut -d " " -f 2)" -gt "$(uname -r | sed 's,-zen,,' | sed 's,-lts,,')" ]; then
echo "$(tput setaf 1)$(tput bold)Reboot required!"
fi
fi
section_end
# Preserving zsh history
section "preserve zsh history"
if [ $ZSH_PRESERVE_HISTORY -gt 0 ]; then
: "${HISTFILE:=${HOME}/.zsh_history}"
: "${HISTSIZE:=10000}"
: "${SAVEHIST:=1000}"
if [ $ZSH_PRESERVE_HISTORY_IMMEDIATELY -gt 0 ]; then
# import new commands from the history file and also append typed commands to the history file immediately
setopt SHARE_HISTORY
else
# the history entry is written out to the file after the command is finished, so that the time taken by the command is recorded correctly in the history file in EXTENDED_HISTORY format
setopt INC_APPEND_HISTORY_TIME
fi
fi
section_end
# print env
section "Environment variables"
log "$(export)"
section_end