bash/.bashrc
2024-08-18 14:05:46 +02:00

477 lines
18 KiB
Bash

#!/bin/bash
# ─< Helper functions >─────────────────────────────────────────────────────────────────
function echo_error() { echo -e "\033[0;1;31merror:\033[0;31m\t${*}\033[0m"; }
function echo_binfo() { echo -e "\033[0;1;34mInfo:\033[0;34m\t${*}\033[0m"; }
function echo_info() { echo -e "\033[0;35m${*}\033[0m"; }
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
# case $- in
# *i*) ;;
# *) return ;;
# esac
_defaults_() {
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
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
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
}
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm* | rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*) ;;
esac
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
}
# ─< check if command exists >────────────────────────────────────────────────────────────
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# ─< Silent execution >─────────────────────────────────────────────────────────────────
silentexec() {
"$@" >/dev/null 2>&1
}
check_root() {
if [[ "${EUID}" -ne 0 ]]; then
if command_exists sudo; then
_sudo="sudo"
else
_sudo=""
echo_info "-- sudo was not found on this system ! --"
fi
else
_sudo=""
fi
}
_zoxide_() {
if command_exists zoxide; then
eval "$(zoxide init bash)"
fi
}
_fancy_ls_() {
# ─< colorized ls >─────────────────────────────────────────────────────────────────────────
if command_exists exa; then
alias ls="exa --icons --long --git"
alias l="exa --icons -l"
alias ll="exa --icons -laa"
alias tree="exa --icons -l --tree"
elif command_exists lsd; then
alias ls="lsd -l -1 -h1 --almost-all --git"
alias l="lsd -1"
alias ll="lsd -1 --almost-all"
alias clearl="command clear && l"
alias tree="lsd --tree"
elif command_exists eza; then
alias ls="eza --icons --long --git"
alias l="eza --icons -l"
alias ll="eza --icons -laa"
alias tree="eza --icons -l --tree"
else
alias ls="ls --color=always -lAph"
alias l="ls --color=always -lph -w1"
alias ll="ls --color=always -lph"
fi
}
_tmux_() {
# ─< t stands for tmux >────────────────────────────────────────────────────────────────────
if command_exists tmux; then
tmux_y="-- tmux-session active! | connecting to active session --"
tmux_n="-- no tmux-session found! | creating one --"
ta() {
command tmux list-sessions >/dev/null 2>&1
if [ $? -eq 0 ]; then
if command_exists notify-send; then
notify-send "$tmux_y"
sleep 0.5
tmux attach
else
echo_info "$tmux_y"
sleep 0.5
tmux attach
fi
else
if command_exists notify-send; then
notify-send "$tmux_n"
sleep 0.5
tmux
else
echo_info "$tmux_n"
sleep 0.5
tmux
fi
fi
}
alias ts="tmux source $HOME/.tmux.conf"
fi
}
_cli_qol_() {
# ─< colored everything >───────────────────────────────────────────────────────────────────
alias ip="ip --color=always"
# ─< easier dir up >────────────────────────────────────────────────────────────────────────
alias ..="cd .."
# ─< weather >──────────────────────────────────────────────────────────────────────────────
alias www="curl wttr.in/Ulm"
# ─< check for rg >─────────────────────────────────────────────────────────────────────────
if command_exists rg; then
alias grep="rg --color=always"
else
alias grep="grep --color=always"
fi
# ─< linutil >────────────────────────────────────────────────────────────────────────────
alias linutil="curl -fsSL https://christitus.com/linux | sh"
# ─< telnet (starwars) >────────────────────────────────────────────────────────────────────
if command_exists telnet; then
alias starwars="telnet -a telehack.com"
fi
# ─< oh-my-posh >───────────────────────────────────────────────────────────────────────────
# ─< oh-my-posh initialization >────────────────────────────────────────────────────────────
if command_exists oh-my-posh; then
# eval "$(oh-my-posh init bash --config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/amro.omp.json')"
eval "$(oh-my-posh init bash --config 'https://git.k4li.de/dotfiles/oh-my-posh/raw/branch/main/amro.toml')"
# eval "$(oh-my-posh init bash --config '~/gitea/dotfiles/oh-my-posh/amro.toml')"
else
curl -s https://ohmyposh.dev/install.sh | sudo bash -s -- -d /usr/bin/
fi
[[ -f $HOME/.local/share/blesh/ble.sh ]] && . $HOME/.local/share/blesh/ble.sh
}
# ─< t stands for trash(-cli) >───────────────────────────────────────────────────────────────
_trash() {
if command_exists trash; then
alias rm="trash"
alias t="trash"
elif command_exists trash-cli; then
alias rm="trash-cli"
alias t="trash-cli"
else
echo_error "-- You do not have trash or trash-cli installed! Your 'rm' will be permanent! --"
fi
}
# ─< rsync >────────────────────────────────────────────────────────────────────────────────
_rsync_() {
_rrsync() {
if command_exists find; then
numbers=0
for source in "${@:1:$#-1}"; do
numbers=$(($numbers + $(find "$source" -type f | wc -l)))
done
destination="${@: -1}"
rsync -avP --info=progress2 "${@:1:$#-1}" "$destination" | pv -lpes $numbers
else
echo_error "-- We could not find 'find'. Please install 'find' to enable the progress bar. (hit 'CTRL + C' to exit now) --"
sleep 5
rsync -avP --info=progress2 "$@"
fi
}
if command_exists rsync; then
alias cp="_rrsync"
alias scp="rsync -avP"
fi
}
_cat_() {
# ─< bat alias >────────────────────────────────────────────────────────────────────────────
if command_exists batcat; then
alias cat="batcat --color=always -p --paging=never"
alias less="batcat --paging always --color=always"
alias gd="batcat --diff"
elif command_exists bat; then
alias cat="bat --color=always -p"
alias less="bat --paging always --color=always"
alias gd="bat --diff"
fi
}
_fetches_() {
# ─< 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
}
_nmap_() {
# ─< set nmap-alias >───────────────────────────────────────────────────────────────────────
if command_exists nmap; then
alias scanvuln="sudo nmap --script vuln -vvv"
alias sv="scanvuln"
alias portscan="sudo nmap -sT"
alias ps="portscan"
fi
}
_docker_() {
# ─< d stands for docker >──────────────────────────────────────────────────────────────────
if command_exists docker; then
alias up="docker compose up"
alias down="docker compose down"
alias pull="docker compose pull"
alias d="docker"
alias dr="docker run --rm -it"
alias drs="docker compose down && docker compose up -d --remove-orphans --force-recreate"
alias ds="docker ps -a"
alias dc="docker compose"
alias appupdate="docker compose pull && docker compose up -d --force-recreate"
fi
}
_git_() {
# ─< g stands for GIT >─────────────────────────────────────────────────────────────────────
if command_exists git; then
alias g="git"
alias gs="git status"
alias gm='git checkout main && git merge'
alias gc="git clone --recurse-submodule"
alias ga="git add"
alias gp="git pull --recurse-submodule"
alias gms='git maintenance start'
alias gsu="git submodule foreach git pull && git add . && git commit -m ' updated 📌submodules' && echo '-- Committed changes, pushing now..' && sleep 1 && git push"
alias gcm="git commit -m"
alias gpu="git push --recurse-submodule=on-demand"
if command_exists lazygit; then
alias lg="lazygit"
fi
fi
}
# ╭────────╮
# │ CODING │
# ╰────────╯
_coding_() {
# ─< h stands for HUGO >──────────────────────────────────────────────────────────────────
if command_exists hugo; then
alias h='hugo'
alias hs='hugo server -D --noHTTPCache --disableFastRender'
fi
# ─< c stands for bin/cake >──────────────────────────────────────────────────────────────
alias cake='bin/cake'
alias c='cake'
alias cs='c server -p 1313'
# ─< VSCodium >─────────────────────────────────────────────────────────────────────────────
if command_exists codium; then
alias code="codium"
export EDITOR="codium"
fi
# ─< neovide, the best frontend for any neovim-config >───────────────────────────────────
if [ -d "$HOME/.local/share/neovide/" ]; then
if command_exists neovide; then
alias nvim='neovide --fork'
else
neovide_path=$(find "$HOME/" -name "*neovide*.appimage" 2>/dev/null)
if [ -n "$neovide_path" ]; then
alias nvim="$neovide_path --fork"
fi
fi
fi
# Function to get the IP address
get_ip() {
ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1
}
# Check if php is available, then create the alias
if command -v php >/dev/null 2>&1; then
alias phprun="php artisan serve --host=$(get_ip) --port=8000"
fi
# Check if npm is available, then create the alias
if command -v npm >/dev/null 2>&1; then
alias npmrun="npm run dev -- --host=$(get_ip) --port=8001"
fi
}
get_packager() {
PKG=""
install=""
update=""
refresh=""
remove=""
source /etc/os-release
case "$ID" in
ubuntu | debian)
if command_exists nala; then
PKG="nala"
install="nala install --assume-yes"
upgrade="nala upgrade"
refresh="nala update"
remove="nala remove"
purge="nala purge"
search="nala search"
alias update="$_sudo $refresh && $_sudo $upgrade"
alias install="$_sudo $refresh && $_sudo $install"
alias remove="$_sudo $remove && $_sudo $purge && $_sudo $clean"
alias search="$search"
elif command_exists apt-get; then
PKG="apt-get"
install="apt-get install --yes"
upgrade="apt-get upgrade"
refresh="apt-get update"
clean="apt-get autoremove"
remove="apt-get remove"
purge="apt-get purge"
search="apt-cache search"
alias update="$_sudo $refresh && $_sudo $upgrade"
alias install="$_sudo $refresh && $_sudo $install"
alias remove="$_sudo $remove && $_sudo $purge && $_sudo $clean"
alias search="$search"
fi
;;
arch | manjaro | endevouros)
if command_exists yay; then
PKG="yay"
alias install="yay -S --noconfirm"
alias update="yay -Syu"
alias remove="yay -R"
alias search="yay -Ss"
elif command_exists paru; then
PKG="paru"
alias install="paru -S --noconfirm"
alias update="paru -Syu"
alias remove="paru -R"
alias search="paru -Ss"
elif command_exists pacman; then
PKG="pacman"
alias install="$_sudo pacman -S --noconfirm"
alias update="$_sudo pacman -Syu"
alias remove="$_sudo pacman -R"
alias search="$_sudo pacman -Ss"
fi
;;
fedora | centos)
PKG="dnf"
alias install="dnf install --yes"
alias update="dnf update"
alias remove="dnf remove"
alias search="dnf search"
;;
alpine)
PKG="apk"
install="apk add"
update="apk update"
upgrade="apk upgrade"
remove="apk del"
alias install="$_sudo $install"
alias update="$_sudo $update && $_sudo $upgrade"
alias remove="$_sudo $remove"
;;
esac
}
get_alias() {
_defaults_
_color_prompt_
_cli_qol_
_cat_
_trash
_nmap_
_tmux_
_docker_
_git_
_rsync_
_zoxide_
_fancy_ls_
_coding_
_fetches_
}
main() {
check_root
get_packager
get_alias
}
main
[[ ${BLE_VERSION-} ]] && ble-attach