Files
dotfiles/local/bin/msgr

223 lines
5.5 KiB
Bash
Executable File

#!/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