feat(scripts): msgr helper script

This commit is contained in:
2023-09-28 17:13:44 +03:00
parent 10b18e8bcc
commit 0aff4784b1
4 changed files with 229 additions and 158 deletions

View File

@@ -10,7 +10,6 @@
: "${VERBOSE:=0}"
: "${DOTFILES:=$HOME/.dotfiles}"
: "${SHARED_SCRIPT:=$DOTFILES/scripts/shared.sh}"
: "${INSTALL_SCRIPT:=$DOTFILES/scripts/install-dotfiles.sh}"
: "${BREWFILE:=$DOTFILES/config/homebrew/Brewfile}"
: "${HOSTFILES:=$DOTFILES/hosts}"
@@ -141,10 +140,7 @@ function section_install
bash "$DOTFILES/scripts/install-ntfy.sh" \
&& msg_yay "ntfy installed!"
;;
pip)
bash "$DOTFILES/scripts/install-pip-packages.sh" \
&& msg_yay "pip/python packages installed!"
;;
pip) bash "$DOTFILES/scripts/install-pip-packages.sh" ;;
z)
bash "$DOTFILES/scripts/install-z.sh" \
&& msg_yay "z has been installed!"

222
local/bin/msgr Executable file
View File

@@ -0,0 +1,222 @@
#!/usr/bin/env bash
# msgr / Messanger helper
# Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved.
# MIT License, https://opensource.org/license/mit/
# Modified from https://stackoverflow.com/a/28776166
(
[[ -n $ZSH_VERSION && $ZSH_EVAL_CONTEXT =~ :file$ ]] || \
[[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)
) && sourced=1 || sourced=0
# ╭──────────────────────────────────────────────────────────╮
# │ Colors │
# ╰──────────────────────────────────────────────────────────╯
CLR_RED="\033[1;31m"
CLR_YELLOW="\033[1;33m"
CLR_GREEN="\033[1;32m"
CLR_BLUE="\033[1;34m"
CLR_RESET="\033[0m"
# ╭──────────────────────────────────────────────────────────╮
# │ Color functions │
# ╰──────────────────────────────────────────────────────────╯
function __color_red()
{
local MSG="$1"
echo -e "${CLR_RED}${MSG}${CLR_RESET}"
}
function __color_yellow()
{
local MSG="$1"
echo -e "${CLR_YELLOW}${MSG}${CLR_RESET}"
}
function __color_green()
{
local MSG="$1"
echo -e "${CLR_GREEN}${MSG}${CLR_RESET}"
}
function __color_blue()
{
local MSG="$1"
echo -e "${CLR_BLUE}${MSG}${CLR_RESET}"
}
# ╭──────────────────────────────────────────────────────────╮
# │ Helpers │
# ╰──────────────────────────────────────────────────────────╯
function __log_marker()
{
echo -e "${CLR_BLUE}➜${CLR_RESET}"
}
function __log_marker_ok()
{
echo -e "${CLR_GREEN}✔${CLR_RESET}"
}
function __log_marker_ok_blue()
{
echo -e "${CLR_BLUE}✔${CLR_RESET}"
}
function __log_marker_warn()
{
echo -e "${CLR_YELLOW}⁕${CLR_RESET}"
}
function __log_marker_question()
{
echo -e "${CLR_YELLOW}?${CLR_RESET}"
}
function __log_marker_err()
{
echo -e "${CLR_RED}⛌${CLR_RESET}"
}
function __log_indent()
{
echo " "
}
# ╭──────────────────────────────────────────────────────────╮
# │ Log functions │
# ╰──────────────────────────────────────────────────────────╯
function msg()
{
echo -e "$(__log_marker) $1"
}
function msg_yay()
{
echo -e "🎉 $1"
}
function msg_yay_done()
{
echo -e "🎉 $1 ...$(__log_marker_ok)"
}
function msg_done()
{
echo -e "$(__log_marker) $1 ...$(__log_marker_ok)"
}
function msg_done_suffix()
{
echo -e "$(__log_marker) ...$(__log_marker_ok)"
}
function msg_prompt()
{
echo -e "$(__log_marker_question) $1"
}
function msg_prompt_done()
{
echo -e "$(__log_marker_question) $1 ...$(__log_marker_ok)"
}
function msg_nested()
{
echo -e "$(__log_indent)$(__log_marker) $1"
}
function msg_nested_done()
{
echo -e "$(__log_indent)$(__log_marker) $1 ...$(__log_marker_ok)"
}
function msg_run()
{
echo -e "${CLR_GREEN}➜ $1${CLR_RESET} $2"
}
function msg_run_done()
{
echo -e "${CLR_GREEN}➜ $1${CLR_RESET} $2 ...$(__log_marker_ok)"
}
function msg_ok()
{
echo -e "$(__log_marker_ok) $1"
}
function msg_warn()
{
echo -e "$(__log_marker_warn) $1"
}
function msg_err()
{
echo -e "$(__log_marker_err) $1"
}
# Create a prompt which you have to answer y/n to continue
ask()
{
while true; do
read -p "$1 ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 1
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 0
fi
done
}
# If this is being sourced, no need to run the next steps.
[ "$sourced" = 1 ] && return
function __tests()
{
msg "[ msg ]"
msg_done "[ msg_done ]"
msg_done_suffix "[ msg_done_suffix ]" && echo " ^-- (msg_done_suffix)"
msg_err "[ msg_err ]"
msg_nested "[ msg_nested ]"
msg_nested_done "[ msg_nested_done ]"
msg_ok "[ msg_ok ]"
msg_prompt "[ msg_prompt ]"
msg_prompt_done "[ msg_prompt_done ]"
msg_run "[ msg_run ]" "[ second_param ]"
msg_run_done "[ msg_run_done ]" "[ second_param ]"
msg_warn "[ msg_warn ]"
msg_yay "[ msg_yay ]"
msg_yay_done "[ msg_yay_done ]"
}
function usage()
{
echo "usage: msgr [type] [message] [optional second message]"
echo ""
echo "-- types and examples: --"
__tests
echo ""
}
# The main loop. first keyword after $0 triggers type, or help and usage examples.
case "$1" in
msg) msg "$2" ;;
done) msg_done "$2" ;;
done_suffix) msg_done_suffix "$2" ;;
err) msg_err "$2" ;;
nested) msg_nested "$2" ;;
nested_done) msg_nested_done "$2" ;;
ok) msg_ok "$2" ;;
prompt) msg_prompt "$2" ;;
prompt_done) msg_prompt_done "$2" ;;
run) msg_run "$2" ;;
run_done) msg_run_done "$2" "$3" ;;
warn) msg_warn "$2" ;;
yay) msg_yay "$2" ;;
yay_done) msg_yay_done "$2" ;;
tests) __tests "[first]" "[second]" ;;
*) usage && exit 0 ;;
esac

View File

@@ -4,6 +4,8 @@
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
msg "Starting to install pip packages"
! have pip && {
msg_err "Could not find pip, something really terrible is going on." && exit 1
}
@@ -19,7 +21,10 @@ for pkg in "${packages[@]}"; do
# Skip comments
if [[ ${pkg:0:1} == "#" ]]; then continue; fi
msg_nested "Installing pip package: $pkg"
python3 -m pip install --user "$pkg"
echo ""
done
msg_yay "Run pip package installation"

View File

@@ -6,145 +6,7 @@
# Helper env variables. Use like this: VERBOSE=1 ./script.sh
: "${VERBOSE:=0}"
# If this file has already been loaded, no need to reload it.
[ "$DOTFILES_SHARED_LOADED" = "yes" ] && return
export DOTFILES_SHARED_LOADED="yes"
# -- Colors -- #
CLR_RED="\033[1;31m"
CLR_YELLOW="\033[1;33m"
CLR_GREEN="\033[1;32m"
CLR_BLUE="\033[1;34m"
CLR_RESET="\033[0m"
# -- Color functions -- #
function __color_red()
{
local MSG="$1"
echo -e "${CLR_RED}${MSG}${CLR_RESET}"
}
function __color_yellow()
{
local MSG="$1"
echo -e "${CLR_YELLOW}${MSG}${CLR_RESET}"
}
function __color_green()
{
local MSG="$1"
echo -e "${CLR_GREEN}${MSG}${CLR_RESET}"
}
function __color_blue()
{
local MSG="$1"
echo -e "${CLR_BLUE}${MSG}${CLR_RESET}"
}
# -- Helpers -- #
function __log_marker()
{
echo -e "${CLR_BLUE}${CLR_RESET}"
}
function __log_marker_ok()
{
echo -e "${CLR_GREEN}${CLR_RESET}"
}
function __log_marker_ok_blue()
{
echo -e "${CLR_BLUE}${CLR_RESET}"
}
function __log_marker_warn()
{
echo -e "${CLR_YELLOW}${CLR_RESET}"
}
function __log_marker_question()
{
echo -e "${CLR_YELLOW}?${CLR_RESET}"
}
function __log_marker_err()
{
echo -e "${CLR_RED}${CLR_RESET}"
}
function __log_indent()
{
echo " "
}
# -- Log -- #
function msg()
{
echo -e "$(__log_marker) $1"
}
function msg_yay()
{
echo -e "🎉 $1"
}
function msg_yay_done()
{
echo -e "🎉 $1 ...$(__log_marker_ok)"
}
function msg_done()
{
echo -e "$(__log_marker) $1 ...$(__log_marker_ok)"
}
function msg_done_suffix()
{
echo -e "$(__log_marker) ...$(__log_marker_ok)"
}
function msg_prompt()
{
echo -e "$(__log_marker_question) $1"
}
function msg_prompt_done()
{
echo -e "$(__log_marker_question) $1 ...$(__log_marker_ok)"
}
function msg_nested()
{
echo -e "$(__log_indent)$(__log_marker) $1"
}
function msg_nested_done()
{
echo -e "$(__log_indent)$(__log_marker) $1 ...$(__log_marker_ok)"
}
function msg_run()
{
echo -e "${CLR_GREEN}$1${CLR_RESET} $2"
}
function msg_run_done()
{
echo -e "${CLR_GREEN}$1${CLR_RESET} $2 ...$(__log_marker_ok)"
}
function msg_ok()
{
echo -e "$(__log_marker_ok) $1"
}
function msg_warn()
{
echo -e "$(__log_marker_warn) $1"
}
function msg_err()
{
echo -e "$(__log_marker_err) $1"
}
source "$DOTFILES/local/bin/msgr"
# -- Menu builder -- #
function menu_section()
@@ -357,20 +219,6 @@ silent()
"$@" >&/dev/null
}
# Create a prompt which you have to answer y/n to continue
ask()
{
while true; do
read -p "$1 ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 1
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 0
fi
done
}
# Check if a file contains non-ascii characters
nonascii()
{