From a170f8725ff4421e02ed70bc473ca6353c0c68f0 Mon Sep 17 00:00:00 2001 From: pika <67532734+pik4li@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:13:27 +0100 Subject: [PATCH] addet message arrays --- .bashrc | 118 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/.bashrc b/.bashrc index a0b5252..e7b8078 100644 --- a/.bashrc +++ b/.bashrc @@ -1,8 +1,31 @@ #!/bin/bash # ─< Helper functions >───────────────────────────────────────────────────────────────── -function echo_error() { echo -e "\033[0;1;31m❌ ERROR:\033[0;31m\t${*}\033[0m"; } -function echo_binfo() { echo -e "\033[0;1;34m⚠️ WARNING:\033[0;34m\t${*}\033[0m"; } -function echo_info() { echo -e "\033[0;35mℹ️ INFO:${*}\033[0m"; } +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;34m⚠️ WARNING:\033[0;34m\t${*}\033[0m" + echo -e "$message" + echo_messages+=("$message") +} + +function echo_info() { + local message="\033[0;35mℹ️ INFO:${*}\033[0m" + echo -e "$message" + echo_messages+=("$message") +} + +# Function to print all stored messages +function print_echo_messages() { + for msg in "${echo_messages[@]}"; do + echo -e "$msg" + done +} # ─< check if command exists >──────────────────────────────────────────────────────────── command_exists() { @@ -14,15 +37,23 @@ 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 [[ "${EUID}" -ne 0 ]]; then + 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 - _sudo="" - echo_info "-- sudo was not found on this system ! --" + 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 } @@ -134,15 +165,7 @@ _sensible.bash_() { # export dropbox="$HOME/Dropbox" } -_defaults_() { - # ─< set keybinding mode >──────────────────────────────────────────────────────────────── - # set -o emacs - # set -o vim - - # If set, the pattern "**" used in a pathname expansion context will - # match all files and zero or more directories and subdirectories. - #shopt -s globstar - +_init() { # make less more friendly for non-text input files, see lesspipe(1) [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" @@ -159,24 +182,42 @@ _defaults_() { *) ;; esac - 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$//'\'')"' - - if [ -f "$HOME/.bash_aliases" ]; then - . "$HOME/.bash_aliases" - fi - + # ─< ssh completions >──────────────────────────────────────────────────────────────────── if [ -f "$HOME/.bash/ssh" ]; then . "$HOME/.bash/ssh" fi + # ─< 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 '~/.bash/themes/zen.toml')" + else + if command_exists curl; then + curl -s https://ohmyposh.dev/install.sh | $_sudo bash -s -- -d /usr/bin/ + fi + fi +} + +_alias() { + # ─< set keybinding mode >──────────────────────────────────────────────────────────────── + # set -o emacs + # set -o vim + + # If set, the pattern "**" used in a pathname expansion context will + # match all files and zero or more directories and subdirectories. + #shopt -s globstar + + 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$//'\'')"' + # ─< colored everything >─────────────────────────────────────────────────────────────────── alias ip="ip --color=always" @@ -201,15 +242,6 @@ _defaults_() { alias starwars="telnet -a telehack.com" fi - # ─< oh-my-posh initialization >──────────────────────────────────────────────────────────── - if command_exists oh-my-posh; then - eval "$(oh-my-posh init bash --config '~/.bash/themes/zen.toml')" - else - if command_exists curl; then - curl -s https://ohmyposh.dev/install.sh | $_sudo bash -s -- -d /usr/bin/ - fi - fi - # ─< neovim >───────────────────────────────────────────────────────────────────────────── if command_exists nvim; then alias cnvim="command nvim" @@ -391,6 +423,15 @@ _blesh() { # . "$HOME/.local/share/blesh/ble.sh" --attach=none . "$HOME/.local/share/blesh/ble.sh" fi + + _games() { + if command_exists curl; then + echo_info "Games available. Try 'alias | grep g'" + alias g2048='bash --norc -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/games/2048.sh)"' + alias gwordle='bash --norc -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/games/wordle.sh)"' + fi + } + } _color_prompt_() { @@ -424,14 +465,6 @@ _color_prompt_() { fi } -_games() { - if command_exists curl; then - echo_info "Games available. Try 'alias | grep g'" - alias g2048='bash --norc -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/games/2048.sh)"' - alias gwordle='bash --norc -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/games/wordle.sh)"' - fi -} - _end() { # ─< fastfetch >──────────────────────────────────────────────────────────────────────────── if command_exists fastfetch; then @@ -441,7 +474,7 @@ _end() { 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 + git clone https://git.k4li.de/mirrors/fastfetch.git "$HOME/.local/share/fastfetch" >/dev/null 2>&1 exec $SHELL fi command clear & @@ -452,6 +485,8 @@ _end() { alias clear='clear && cowsay -f tux "$(uptime --pretty)"' cowsay -f tux "$(uptime --pretty)" fi + + print_echo_messages } # ╭────────╮ @@ -544,15 +579,16 @@ get_packager() { } get_alias() { - _defaults_ + _init _sensible.bash_ - _blesh _color_prompt_ _coding_ + _alias _games } main() { + _blesh check_root get_packager get_alias