zsh/.zshrc
2025-05-17 14:48:53 +02:00

278 lines
7 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
#
# Check if the user is root and set sudo variable if necessary
check_root() {
if [[ "${EUID}" -ne 0 ]]; then
if command_exists sudo; then
echo_info "User <$(whoami)> is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
# ───────────────────────────────────< Message storage >─────────────────────────────────
typeset -A _MESSAGES
_MESSAGES=(
[missing]=""
[error]=""
[warn]=""
[info]=""
)
# Define color variables
RED='\033[0;31m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color
BOLD='\033[1m'
# Functions to store messages
echo_error() {
_MESSAGES[error]+="${RED}$1${NC}\n"
}
echo_missing() {
_MESSAGES[missing]+="${YELLOW} 󱥸 $1${NC}\n"
}
echo_warning() {
_MESSAGES[warn]+="${YELLOW}⚠️ $1${NC}\n"
}
echo_info() {
_MESSAGES[info]+="${CYAN} $1${NC}\n"
}
# Display stored messages
error_log() {
[[ -z "${_MESSAGES[error]}${_MESSAGES[warn]}${_MESSAGES[info]}${_MESSAGES[missing]}" ]] && return 0
typeset -A headers colors
headers=(
missing "⚠️ MISSING ESSENTIALS ⚠️"
error "❌ Errors"
warn "⚠️ Warnings"
info " Info"
)
colors=(
missing "$YELLOW"
error "$RED"
warn "$YELLOW"
info "$CYAN"
)
for type in error warn info missing; do
[[ -n "${_MESSAGES[$type]}" ]] && {
printf "\n${BOLD}${colors[$type]}=== ${headers[$type]} ===${NC}\n"
printf "${_MESSAGES[$type]}"
}
done
}
__shell_qol__() {
if command_exists oh-my-posh; then
# eval "$(oh-my-posh init zsh --config 'https://git.k4li.de/dotfiles/oh-my-posh/raw/branch/main/amro.toml')"
eval "$(oh-my-posh init zsh --config '~/.zsh/themes/power10k_edit.toml')"
else
curl -s https://ohmyposh.dev/install.sh | $_sudo bash --norc -s -- -d /usr/bin/
fi
# ─< init fzf for zsh >───────────────────────────────────────────────────────────────────
if command_exists fzf; then
source <(fzf --zsh)
fi
if command_exists zoxide; then
eval "$(zoxide init zsh)"
eval "$(zoxide init zsh --cmd cd)"
fi
}
__get_Packager__() {
# Load OS release information
[ -f /etc/os-release ] && . /etc/os-release || return 1
DISTRO="${ID}:${ID_LIKE}"
case "$DISTRO" in
*debian*)
if command_exists nala; then
alias search="nala search"
alias install="$_sudo nala install --assume-yes"
alias update="$_sudo nala update && $_sudo nala upgrade --full"
alias remove="$_sudo nala purge"
else
alias search="apt-cache search"
alias install="$_sudo apt install --yes"
alias update="$_sudo apt update && $_sudo apt upgrade"
alias remove="$_sudo apt purge"
fi
alias unbreak="$_sudo dpkg --configure -a"
;;
*arch*)
if command_exists paru; then
alias search="paru -Ss --color always"
alias install="paru -S --color always --noconfirm"
alias update="paru -Syu --color always"
alias remove="paru -R --color always"
elif command_exists yay; then
alias search="yay -Ss --color always"
alias install="yay -S --noconfirm --color always"
alias update="yay -Syu --color always"
alias remove="yay -R --color always"
else
alias search="$_sudo pacman -Ss --color always"
alias install="$_sudo pacman -S --noconfirm --color always"
alias update="$_sudo pacman -Syu --color always"
alias remove="$_sudo pacman -R --color always"
fi
;;
*rhel* | *fedora*)
alias search="dnf search"
alias install="$_sudo dnf install -y --skip-missing"
alias update="$_sudo dnf update -y"
alias remove="$_sudo dnf remove -y"
;;
*suse*)
alias search="zypper search"
alias install="$_sudo zypper install --no-confirm"
alias update="$_sudo zypper update"
alias remove="$_sudo zypper remove"
;;
*alpine*)
alias install="$_sudo apk add"
alias update="$_sudo apk update && $_sudo apk upgrade"
alias remove="$_sudo apk del"
;;
*nixos*)
echo_info "Using NIX!!"
alias update="$_sudo nixos-rebuild switch"
# alias install="$_sudo nix-env -iA nixos."
install() {
"$_sudo nix-end -iA nixos.$@"
}
alias edit="$_sudo -E $EDITOR /etc/nixos/configuration.nix"
;;
*)
echo_error "Unsupported distro: $ID"
;;
esac
}
__sources__() {
local sourceDir="$HOME/.zsh"
local sourceOptions=(
# "defaults"
"plugins"
"installs"
"aliases"
)
for i in "${sourceOptions[@]}"; do
if [ -e "${sourceDir}/.$i.zsh" ]; then
. "${sourceDir}/.$i.zsh"
fi
done
}
__defaults__() {
# Load completions
autoload -Uz compinit && compinit
bindkey -e
# # Define custom word style that treats special characters as word boundaries
autoload -Uz select-word-style
select-word-style bash
setopt appendhistory
setopt sharehistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups
# Huge history. Doesn't appear to slow things down, so why not?
HISTSIZE=500000
HISTFILESIZE=100000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
# Completion styling
zstyle :compinstall filename "$HOME/.zshrc"
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
# zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath'
setopt autocd notify
# End of lines configured by zsh-newuser-install
#
}
__keychain_setup__() {
setupKeys() {
local keys=(
"homelab-id_rsa"
"hetzner_id_rsa"
)
for i in "${keys[@]}"; do
if [ -n $i ]; then
if [ -e "$i" ]; then
eval "$(keychain --eval --noask --agents ssh ~/.ssh/$i)"
fi
else
return 69
fi
done
}
if ! setupKeys; then
eval "$(keychain --eval --noask --agents ssh ~/.ssh/{homelab-id_rsa,hetzner_id_rsa})"
fi
}
__end__() {
if command_exists fastfetch; then
clear &&
fastfetch
else
echo_warning "fastfetch is not installed.."
fi
if command_exists cowsay; then
alias clear='clear && cowsay -f tux "$(uptime --pretty)"'
cowsay -f tux "$(uptime --pretty)"
else
echo_warning "cowasy is not installed.."
fi
__shell_qol__
error_log
}
main() {
__defaults__
__alias__
__sources__
__end__
__keychain_setup__
}
if check_root; then
main
fi