zsh/.zshrc

198 lines
5 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.

# ─< Helper functions >─────────────────────────────────────────────────────────────────
# ANSI color codes
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
proxy() {
local ip="172.22.11.69"
local port="8080"
# Check if the IP and port are reachable
if nc -z -w5 $ip $port; then
# If reachable, set the proxy environment variables
export http_proxy="http://$ip:$port"
export https_proxy="http://$ip:$port"
export HTTP_PROXY="http://$ip:$port"
export HTTPS_PROXY="http://$ip:$port"
echo_info "Proxy set to: http://$ip:$port"
else
echo_error "IP $ip with port $port is not reachable."
fi
alias proxy='echo "http: $HTTP_PROXY"; echo "https: $HTTPS_PROXY"'
}
noproxy() {
unset {HTTP_PROXY,HTTPS_PROXY,http_proxy,https_proxy}
}
# Message storage
typeset -A _MESSAGES
_MESSAGES=(
[error]=""
[warn]=""
[info]=""
)
# Logging functions with emojis
echo_error() {
local msg="${RED}$1${NC}\n"
printf "$msg" >&2
_MESSAGES[error]+="$msg"
}
echo_warning() {
local msg="${YELLOW}⚠️ $1${NC}\n"
printf "$msg"
_MESSAGES[warn]+="$msg"
}
echo_info() {
local msg="${CYAN} $1${NC}\n"
printf "$msg"
_MESSAGES[info]+="$msg"
}
# Display stored messages
error_log() {
[[ -z "${_MESSAGES[error]}${_MESSAGES[warn]}${_MESSAGES[info]}" ]] && return 0
typeset -A headers colors
headers=(
error "❌ Errors"
warn "⚠️ Warnings"
info " Info"
)
colors=(
error "$RED"
warn "$YELLOW"
info "$CYAN"
)
for type in error warn info; do
[[ -n "${_MESSAGES[$type]}" ]] && {
printf "\n${BOLD}${colors[$type]}=== ${headers[$type]} ===${NC}\n"
printf "${_MESSAGES[$type]}"
}
done
}
# ─< 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
}
_init() {
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/sim-web.toml')"
eval "$(oh-my-posh init zsh --config '~/.zsh/themes/amro.toml')"
# eval "$(oh-my-posh init zsh --config '~/.zsh/themes/atomicBit.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
}
_sources() {
if [ -e "$HOME/.bash_aliases" ]; then
. "$HOME/.bash_aliases"
_alias=""
else
unset $_alias
fi
local sourceDir="$HOME/.zsh"
local sourceOptions=(
"$_alias"
"defaults"
"plugins"
"installs"
)
for _s in "${sourceOptions[@]}"; do
local _source_="${sourceDir}/.${_s}.zsh"
if [ -e "$_source_" ]; then
. $_source_
fi
local _source_""
done
}
_environment() {
if command_exists nvim; then
export EDITOR="$(which nvim)"
fi
# ─< paths >──────────────────────────────────────────────────────────────────────────────
if [ -d "$HOME/.local/bin" ]; then
export PATH="$HOME/.local/bin:$PATH"
if [ -e "$HOME/.local/bin/lazydocker" ]; then
alias ld="$HOME/.local/bin/lazydocker"
fi
fi
[ -d "$HOME/go/bin" ] && export PATH="$HOME/go/bin:$PATH"
[ -d "$HOME/.cargo/bin" ] && export PATH="$HOME/.cargo/bin:$PATH"
[ -e "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
# bun completions
[ -s "$HOME/.bun/_bun" ] && . "$HOME/.bun/_bun"
[ -s "$HOME/.bun/_bun" ] && export BUN_INSTALL="$HOME/.bun" && export PATH="$BUN_INSTALL/bin:$PATH"
[ -d "$HOME/.zsh/plugins/fzf-zsh-plugin/bin" ] && export PATH="$HOME/.zsh/plugins/fzf-zsh-plugin/bin:$PATH"
}
_end() {
if command_exists fastfetch; then
clear &&
fastfetch
fi
if command_exists cowsay; then
alias clear='clear && cowsay -f tux "$(uptime --pretty)"'
cowsay -f tux "$(uptime --pretty)"
fi
error_log
}
main() {
_environment
_init
_sources
_end
}
if check_root; then
main
fi