feat!: refactor base, config, dfm and scripts

This commit is contained in:
2024-07-23 03:43:12 +03:00
parent adecceda7a
commit 1f2ca90ca5
36 changed files with 1543 additions and 897 deletions

View File

@@ -6,164 +6,14 @@
# Helper env variables. Use like this: VERBOSE=1 ./script.sh
: "${VERBOSE:=0}"
# 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
# Set variable that checks if the shared.sh script has been sourced only once
# If the script has been sourced more than once, the script not be sourced again
[ -z "$SHARED_SCRIPTS_SOURCED" ] && {
eval "$HOME/.dotfiles/config/shared.sh"
source "${DOTFILES}/config/shared.sh"
msgr done "(!) shared.sh not sourced"
DOTFILES_CURRENT_SHELL=$(ps -p $$ -oargs=)
export DOTFILES_CURRENT_SHELL
# Other variables
export OHMYPOSH_CFG="$HOME/.dotfiles/config/omp/own.toml"
# Remove directory from the PATH variable
# usage: path_remove ~/.local/bin
function path_remove
{
x-path-remove "$1"
}
# Append directory to the PATH
# usage: path_append ~/.local/bin
function path_append
{
x-path-remove "$1"
x-path-prepend "$1"
}
# Prepend directory to the PATH
# usage: path_prepend ~/.local/bin
function path_prepend
{
x-path-remove "$1"
x-path-prepend "$1"
}
# Run command silently
# Usage: silent uptime
silent()
{
"$@" >&/dev/null
}
# Check if a file contains non-ascii characters
nonascii()
{
LC_ALL=C grep -n '[^[:print:][:space:]]' "${@}"
}
source "$DOTFILES/local/bin/msgr"
# -- Menu builder -- #
function menu_section()
{
LINE=$(printf '%-18s [ %-15s ]\n' "$1" "$2")
echo -e " $(__log_marker) $LINE"
}
function menu_item()
{
LINE=$(printf '%-15s %-30s\n' "$1" "$2")
echo -e "$(__log_indent)$(__log_marker) $LINE"
}
# https://stackoverflow.com/a/85932
function fn_exists()
{
declare -f -F "$1" > /dev/null
return $?
}
# Takes a bash array ("cow:moo", "dinosaur:roar") and loops
# through the keys to build menu section listing.
function menu_usage_header()
{
MENU_CMD="$1"
shift
MENU_ARRAY=("$@")
KEYS=""
for item in "${MENU_ARRAY[@]}"; do
CMD=$(echo "${item}" | awk -F ":" '{print $1}')
KEYS+="${CMD} | "
done
# "???" removes 3 last characters, being " | " from the end
menu_section "$MENU_CMD" "${KEYS%???}"
}
# Takes the usage command "$0 dotfiles" and a
# bash array ("cow:moo" "dinosaur:roar") and loops
# through in building a menu for dfm command usage listing.
function menu_usage()
{
MENU_CMD="$1"
shift
MENU_ARRAY=("$@")
msg "$MENU_CMD"
for item in "${MENU_ARRAY[@]}"; do
CMD=$(echo "${item}" | awk -F ":" '{print $1}')
DESC=$(echo "${item}" | awk -F ":" '{print $2}')
menu_item "$CMD" "$DESC"
done
}
# Creates a random string
rnd()
{
echo $RANDOM | md5sum | head -c 20
}
# return sha256sum for file
# $1 - filename (string)
function get_sha256sum()
{
sha256sum "$1" | head -c 64
}
# Replaceable file
#
# $1 - filename (string)
# $2 - filename (string)
#
# Returns 1 when replaceable, 0 when not replaceable.
function replacable()
{
FILE1="$1"
FILE2="$2"
[[ ! -r "$FILE1" ]] && {
[[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($FILE1) does not exist"
return 0
}
[[ ! -r "$FILE2" ]] && {
[[ $VERBOSE -eq 1 ]] && msg_err "File 2 ($FILE2) does not exist, replaceable"
return 1
}
FILE1_HASH=$(get_sha256sum "$FILE1")
FILE2_HASH=$(get_sha256sum "$FILE2")
[[ $FILE1_HASH = "" ]] && {
[[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($FILE1)"
return 0
}
[[ $FILE2_HASH = "" ]] && {
[[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 2 ($FILE2), replaceable"
return 1
}
[[ "$FILE1_HASH" == "$FILE2_HASH" ]] && {
[[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $FILE1"
return 0
}
[[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($FILE1_HASH != $FILE2_HASH), replaceable"
return 1
# Set variable that checks if the shared.sh script has been sourced only once
# shellcheck disable=SC2034
export SHARED_SCRIPTS_SOURCED=1
}