profile/home/dotfiles/zshrc

267 lines
6.5 KiB
Bash
Raw Normal View History

2018-02-17 01:29:19 +00:00
#!/bin/zsh
2019-11-22 09:22:54 +00:00
when_binary_available() {
if has_binary "$@"; then
printf '1'
return
fi
printf '0'
return
}
has_binary() {
command -v "$@" >/dev/null 2>&1
}
2018-02-17 01:29:19 +00:00
ZSH_DEBUG="${ZSH_DEBUG:-0}"
ZSH_ALIAS=${ZSH_ALIAS:-1}
ZSH_COMPAT_BASH_COMPLETION=${ZSH_COMPAT_BASH_COMPLETION:-1}
2018-07-17 22:12:00 +00:00
ZSH_CUSTOM=${ZSH_CUSTOM:-1}
2018-02-17 01:29:19 +00:00
ZSH_DEFAULT_TERM=${ZSH_DEFAULT_TERM:-1}
ZSH_GPG_PINENTRY_FIX=${ZSH_GPG_PINENTRY_FIX:-1}
2019-11-22 09:22:54 +00:00
ZSH_PIPX=${ZSH_PIPX:-$(when_binary_available pipx)}
2018-02-17 01:29:19 +00:00
ZSH_POWERLINE="${ZSH_POWERLINE:-1}"
ZSH_PRINT_REBOOT_REQUIRED=${ZSH_PRINT_REBOOT_REQUIRED:-0} # hacky, see below
ZSH_SYNTAX_HIGHLIGHTING="${ZSH_SYNTAX_HIGHLIGHTING:-1}"
ZSH_THEFUCK=${ZSH_THEFUCK:-1}
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"
2019-11-22 09:22:54 +00:00
if [ $ZSH_COMPAT_BASH_COMPLETION -gt 0 ] || [ $ZSH_PIPX -gt 0 ]; then
log "Enabling compatibility for bash completion scripts..."
2019-11-22 09:22:54 +00:00
autoload -U bashcompinit
bashcompinit
fi
section_end
2018-10-11 08:13:45 +00:00
# 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
2018-07-17 22:17:40 +00:00
# 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
2018-07-23 09:07:07 +00:00
section_end
2019-11-22 09:22:54 +00:00
section "pipx bin"
if [ $ZSH_PIPX -gt 0 ]; then
autoload -U bashcompinit
bashcompinit
eval "$(register-python-argcomplete pipx)"
fi
section_end
2018-07-23 09:07:07 +00:00
# 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
2018-07-17 22:17:40 +00:00
section_end
2018-02-17 01:29:19 +00:00
# Powerline
section "powerline"
if [ $ZSH_POWERLINE -gt 0 ]; then
log "Running powerline daemon..."
log "$(powerline-daemon 2>&1)"
log "Finding powerline..."
POWERLINE_DIR="$(pip3 show powerline-status --isolated | grep '^Location:' | awk '{print $2}')"
2018-02-17 01:29:19 +00:00
log "Powerline repository root: $POWERLINE_DIR"
log "Preparing zstyle..."
autoload -Uz promptinit
promptinit
prompt off
2018-02-17 01:29:19 +00:00
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
2018-02-17 01:29:19 +00:00
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
2018-02-17 01:29:19 +00:00
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
# sugar to easily connect to remote docker hosts through SSH
section "ssh-based remote docker access"
ssh_docker() {
args=("$@")
control_path=$(sha1sum - <<< "${args[-1]}" | awk '{print $1}')
base_path=/var/tmp/ssh-docker/$(sha1sum - <<< "$1" | awk '{print $1}')
control_path="${base_path}.control"
sock_path="${base_path}.sock"
if [ -f "${sock_path}" ]
then
echo "Socket path ${sock_path} already exists." >&2
return 1
fi
if [ -f "${control_path}" ]
then
echo "Control path ${control_path} already exists." >&2
return 1
fi
mkdir -p /var/tmp/ssh-docker
ssh -o ControlMaster=auto -o ControlPersist=yes -o ControlPath="${control_path}" -nfNT -L "${sock_path}:/var/run/docker.sock" "$@"
echo "export DOCKER_TLS_VERIFY="
echo "export DOCKER_HOST=unix://${sock_path}"
echo "export DOCKER_CERT_PATH="
echo "export DOCKER_SSH_CONTROL_PATH=${control_path}"
echo "export DOCKER_SSH_SOCKET_PATH=${sock_path}"
}
ssh_docker_exit() {
args=("$@")
if [ "${#args}" -gt 0 ]
then
base_path=/var/tmp/ssh-docker/$(sha1sum - <<< "${args[-1]}" | awk '{print $1}')
control_path="${base_path}.control"
sock_path="${base_path}.sock"
else
control_path="${DOCKER_SSH_CONTROL_PATH}"
sock_path="${DOCKER_SSH_SOCKET_PATH}"
fi
if [ -z "${control_path}" ]
then
echo "No active Docker SSH forwarding found in environment." >&2
return 1
fi
ssh -o ControlPath="${control_path}" -O exit "$@" dummy
rm -f "${sock_path}" "${control_path}"
}
ssh_docker_cleanup() {
for socket in /var/tmp/ssh-docker/*.sock
do
if [ ! -e "$socket" ]
then
continue
fi
control="$(basename "$socket" .control).sock"
DOCKER_SSH_CONTROL_PATH="$control" \
DOCKER_SSH_SOCKET_PATH="$socket" \
ssh_docker_exit
done
}
export ssh_docker
export ssh_docker_exit
export ssh_docker_cleanup
section_end
2018-02-17 01:29:19 +00:00
# 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
# print env
section "Environment variables"
log "$(export)"
section_end