303 lines
7.7 KiB
Bash
303 lines
7.7 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_COMPAT_BASH_COMPLETION=${ZSH_COMPAT_BASH_COMPLETION:-1}
|
|
ZSH_CUSTOM=${ZSH_CUSTOM:-1}
|
|
ZSH_DEFAULT_TERM=${ZSH_DEFAULT_TERM:-1}
|
|
ZSH_GPG_PINENTRY_FIX=${ZSH_GPG_PINENTRY_FIX:-1}
|
|
ZSH_LOGO_LS=${ZSH_LOGL_LS:-$(when_binary_available logo-ls)}
|
|
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:-1}"
|
|
ZSH_PRINT_REBOOT_REQUIRED=${ZSH_PRINT_REBOOT_REQUIRED:-0} # hacky, see below
|
|
ZSH_SYMFONY_CLI="${ZSH_SYMFONY_CLI:-$(when_binary_available symfony)}"
|
|
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"
|
|
if [ $ZSH_COMPAT_BASH_COMPLETION -gt 0 ] || [ $ZSH_PIPX -gt 0 ]; then
|
|
log "Enabling compatibility for bash completion scripts..."
|
|
autoload -U bashcompinit
|
|
bashcompinit
|
|
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
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|