bash/.bashrc
2025-03-01 22:01:53 +01:00

233 lines
7.5 KiB
Bash
Raw Permalink 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.

#!/bin/bash
# ─< Helper functions >─────────────────────────────────────────────────────────────────
declare -a echo_messages
function echo_error() {
local message="\033[0;1;31m❌ ERROR:\033[0;31m\t${*}\033[0m"
echo -e "$message"
echo_messages+=("$message")
}
function echo_warning() {
local message="\033[0;1;33m⚠ WARNING:\033[0;33m\t${*}\033[0m"
echo -e "$message"
echo_messages+=("$message")
}
function echo_info() {
local message="\033[0;1;34m INFO:\033[0;34m\t${*}\033[0m"
echo -e "$message"
echo_messages+=("$message")
}
function echo_plugin() {
local message="\033[0;1;35m🔥 Pluginmgmt:\033[0;35m\t${*} loaded\033[0m"
echo -e "$message"
echo_messages+=("$message")
}
# Function to print all stored messages
function print_echo_messages() {
echo -e "\033[38;5;196mL\033[38;5;202mo\033[38;5;208mg\033[38;5;214m \033[38;5;220mo\033[38;5;226mu\033[38;5;118mt\033[38;5;46mp\033[38;5;48mu\033[38;5;51mt\033[38;5;45m:"
for msg in "${echo_messages[@]}"; do
echo -e "$msg"
done
}
# ─< Silent execution >─────────────────────────────────────────────────────────────────
silentexec() {
"$@" >/dev/null 2>&1
}
# ─< 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 [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
echo_warning "User 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."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
_sources() {
local sourceDir="$HOME/.bash"
local sourceOptions=(
# "aliases"
# "defaults"
"installs"
)
[[ -e "$HOME/.bash_aliases" ]] &&
. "$HOME/.bash_aliases"
for _s in "${sourceOptions[@]}"; do
local _sourceFile="${sourceDir}/.${_s}.sh"
if [ -e "$_sourceFile" ]; then
. "$_sourceFile"
fi
local _source_""
done
}
_init() {
# ─< zoxide >─────────────────────────────────────────────────────────────────────────────
if command_exists zoxide; then
eval "$(zoxide init bash)"
fi
# ─< fzf >────────────────────────────────────────────────────────────────────────────────
if command_exists fzf; then
eval "$(fzf --bash)"
fi
# ─< oh-my-posh initialization >────────────────────────────────────────────────────────────
if command_exists oh-my-posh; then
eval "$(oh-my-posh init bash --config "$HOME/.bash/themes/zen.toml")"
else
if command_exists curl; then
curl -s https://ohmyposh.dev/install.sh | $_sudo bash -s -- -d /usr/bin/
binDirs=(
"$HOME/.local/bin"
"/usr/local/bin"
"/usr/bin"
)
while ! command_exists oh-my-posh; do
for binDir in "${binDirs[@]}"; do
if [ -d "$binDir" ]; then
case "$binDir" in
"$HOME/.local/bin")
echo_info "Installing oh-my-posh into $binDir"
curl -s https://ohmyposh.dev/install.sh | bash -s -- -d "$binDir"
;;
*)
echo_info "Installing oh-my-posh into $binDir"
curl -s https://ohmyposh.dev/install.sh | $_sudo bash -s -- -d "$binDir"
;;
esac
fi
done
done
fi
fi
}
# ─< ble.sh -- https://github.com/akinomyoga/ble.sh >─────────────────────────────────────
d_blesh() {
for deeps in git make gawk; do
if ! command_exists $deeps; then
echo_error "$deeps was missing from the system, cannot setup shell properly! (blesh setup)"
exit 1
fi
done
}
i_blesh() {
local tmp_dir="$(mktemp -d)"
cd "$tmp_dir" || echo_error "$tmp_dir is not valid!"
git clone --recursive --depth 1 --shallow-submodules https://github.com/akinomyoga/ble.sh.git
make -C ble.sh install PREFIX=~/.local
. "$HOME/.local/share/blesh/ble.sh"
# bash
# cd "$HOME" || return 0
}
_blesh() {
if [ ! -f $HOME/.local/share/blesh/ble.sh ]; then
d_blesh # Dependency check for blesh
echo_info "Installing blesh.. Depending on your internet connection and the performance of the client, this could take a minute or two.."
i_blesh # installing blesh silently
else
# . "$HOME/.local/share/blesh/ble.sh" --attach=none
. "$HOME/.local/share/blesh/ble.sh"
fi
}
_env() {
if command_exists nvim; then
export EDITOR="$(which nvim)"
elif command_exists vim; then
export EDITOR="$(which vim)"
elif command_exists vi; then
export EDITOR="$(which vi)"
fi
}
_color_prompt_() {
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color | *-256color) color_prompt=yes ;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
if [ "$color_prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
PS1="\[$(tput setaf 196)\]\u\[$(tput setaf 247)\]@\[$(tput setaf 203)\]\h \[$(tput setaf 49)\]\w \[$(tput sgr0)\]$ "
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
fi
}
_end() {
# ─< fastfetch >────────────────────────────────────────────────────────────────────────────
if command_exists fastfetch; then
alias ff="fastfetch"
alias clearff="command clear & fastfetch"
alias clearf="command clear & fastfetch"
if fastfetch --config os >/dev/null 2>&1; then
alias f="fastfetch --config os"
else
git clone https://git.k4li.de/mirrors/fastfetch.git "$HOME/.local/share/fastfetch" >/dev/null 2>&1
exec $SHELL
fi
command clear &
fastfetch
fi
if command_exists cowsay; then
alias clear='clear && cowsay -f tux "$(uptime --pretty)"'
cowsay -f tux "$(uptime --pretty)"
fi
print_echo_messages
}
main() {
_env
_sources
# _blesh
_init
_color_prompt_
check_root
_end
}
main