From 19d9f17f772d00539f10546e7ce89ae4bbb0f34d Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Wed, 12 Apr 2023 19:06:42 +0800 Subject: [PATCH] testing new helper functions in jbmono --- helpers | 196 +++++++++++++++++++++++++++++++++++++++++++++++++ install/jbmono | 18 ++--- 2 files changed, 201 insertions(+), 13 deletions(-) create mode 100644 helpers diff --git a/helpers b/helpers new file mode 100644 index 0000000..25c27da --- /dev/null +++ b/helpers @@ -0,0 +1,196 @@ +#!/bin/bash + +installed() { + command -v "$1" >/dev/null 2>&1 +} + +title() { + [ "$1" ] && title="$1" || title="$(grep -m 1 -oP "(?<=^##makedesc:\s).*$" ${BASH_SOURCE[1]})" + info + info "===============================================" + info "$title" + info "===============================================" + info +} + +require() { + if ! installed "$1"; then + if [ "$2" ]; then + die "'$1' must to be installed in your system" 200 + else + info "Installing '$1' because it is required to continue" + sudo apt install "$1" + [ $? -gt 0 ] && die "installation cancelled" 201 + fi + fi +} + +download() { + require wget 1 + wget "$1" -O "$2" +} + +abspath() { + echo $(realpath -q "${1/#\~/$HOME}") +} + +is_writable() { + [ -w "$(abspath $1)" ] +} + +is_dir() { + [ -d "$(abspath $1)" ] +} + +is_file() { + [ -f "$(abspath $1)" ] +} + +is_function() { + declare -F "$1" > /dev/null +} + +regex_match() { + printf "%s" "$1" | grep -qP "$2" +} + +in_array() { + local find=$1 + shift + for e in "$@"; do + [[ "$e" == "$find" ]] && return 0 + done + return 1 +} + +implode() { + local d=${1-} + local f=${2-} + if shift 2; then + printf %s "$f" "${@/#/$d}" + fi +} + +IINFO="( i )" +INOTE="( * )" +IWARN="( # )" +IERROR="( ! )" +IFATAL="( @ )" +ISUCCESS="( ! )" +IASK="( ? )" +IDEBUG="(DBG)" +IVRB="( + )" + +BOLD="\e[1m" +DIM="\e[2m" +NOTBOLD="\e[22m" # sometimes \e[21m +NOTDIM="\e[22m" +NORMAL="\e[20m" +RESET="\e[0m" + +FRESET="\e[39m" +FBLACK="\e[30m" +FWHITE="\e[97m" +FRED="\e[31m" +FGREEN="\e[32m" +FYELLOW="\e[33m" +FBLUE="\e[34m" +FLRED="\e[91m" +FLGREEN="\e[92m" +FLYELLOW="\e[93m" +FLBLUE="\e[94m" + +BRESET="\e[49m" +BBLACK="\e[40m" +BWHITE="\e[107m" +BRED="\e[41m" +BGREEN="\e[42m" +BYELLOW="\e[43m" +BBLUE="\e[44m" +BLRED="\e[101m" +BLGREEN="\e[102m" +BLYELLOW="\e[103m" +BLBLUE="\e[104m" + +dt() { + echo "[$(date +'%H:%M:%S')] " +} + +ask() { + IFS= read -rp "$(print ${BOLD}${BBLUE}${FWHITE}${IASK}${BRESET}\ ${BOLD}$1 ): " $2 +} + +print() { + echo -e "$*${RESET}" +} + +debug() { + if [ "$2" ]; then + print "${DIM}${BOLD}${RESET}${DIM} ${FUNCNAME[1]:-?}():${BASH_LINENO:-?}\t$1 " + else + print "${DIM}${BOLD}${RESET}${DIM}$1 " + fi +} + + +verbose() { + print "${BOLD}${IVRB}${RESET}${FYELLOW} $1 " +} + +info() { + print "${BOLD}${FWHITE}${BLBLUE}${IINFO}${RESET}${FWHITE} $1 " +} + +note() { + print "${BOLD}${DIM}${FWHITE}${INOTE}${RESET} $1 " +} + +success() { + print "${BOLD}${BGREEN}${FWHITE}${ISUCCESS}${BRESET}$FGREEN $1 " +} + +warn() { + print "${BOLD}${BYELLOW}${FBLACK}${IWARN}${BRESET}${FYELLOW} Warning:${RESET} $1 " +} + +error() { + print "${BOLD}${BLRED}${FWHITE}${IERROR} Error: ${BRESET}${FLRED} $1 " >&2 + # print_stacktrace +} + +fatal() { + print "${BOLD}${BRED}${FWHITE}${IFATAL} FATAL: $1 " >&2 + print_stacktrace +} + +die() { + error "$1" + [ "$2" ] && exit $2 || exit 100 +} + +die_fatal() { + fatal "$1" + [ "$2" ] && exit $2 || exit 100 +} + +die_help() { + error "$1" + [ "$2" ] && is_function "$2".help && $2.help + [ "$3" ] && exit $3 || exit 100 +} + +print_stacktrace() { + STACK="" + local i + local stack_size=${#FUNCNAME[@]} + debug "Callstack:" + # for (( i=$stack_size-1; i>=1; i-- )); do + for (( i=1; i<$stack_size; i++ )); do + local func="${FUNCNAME[$i]}" + [ x$func = x ] && func=MAIN + local linen="${BASH_LINENO[$(( i - 1 ))]}" + local src="${BASH_SOURCE[$i]}" + [ x"$src" = x ] && src=non_file_source + debug " at $func $src:$linen" + done +} diff --git a/install/jbmono b/install/jbmono index a610105..a98c633 100755 --- a/install/jbmono +++ b/install/jbmono @@ -1,22 +1,14 @@ #!/bin/bash ##makedesc: Install JetBrains Mono fonts +[ -f `dirname $0`/../helpers ] && source `dirname $0`/../helpers || exit 255 # https://www.jetbrains.com/lp/mono/#how-to-install -echo -echo "===============================================" -echo "Installing JetBrains Mono fonts..." -echo "===============================================" -echo - -installed() { - command -v "$1" >/dev/null 2>&1 -} - -! installed wget && sudo apt install wget -! installed unzip && sudo apt install unzip +title +require unzip mkdir -p "$HOME/install/jbmono" "$HOME/.local/share/fonts/" -wget https://download.jetbrains.com/fonts/JetBrainsMono-2.304.zip -O "$HOME/install/jbmono.zip" && \ + +download "https://download.jetbrains.com/fonts/JetBrainsMono-2.304.zip" "$HOME/install/jbmono.zip" && \ unzip -oj "$HOME/install/jbmono.zip" "fonts/ttf/*.ttf" -d "$HOME/.local/share/fonts/" && \ fc-cache -vf "$HOME/.local/share/fonts/"