This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/util.sh

80 lines
2 KiB
Bash

# Get the number of avaialble cores (threads).
function _core_count() {
cat /proc/cpuinfo | grep ^processor | wc -l
}
# Parse integers from mixed string.
function _parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
}
# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, skipped - count of skipped files, status - status line (not recommended to use).
# Usage: _iterate_targets <FUNCTION> [FILES]
function _iterate_targets() {
local IFS=$'\n'
local foo="${1}"
local targets=("${@:2}")
local total=${#targets[@]}
local count=0
local failed=0
local skipped=0
local code=0
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
_error "No targets provided."
return 1
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${color_bwhite}${status}${color_default}"
# Call function.
${foo} "${target}"
# Show error.
if [[ ${?} != 0 ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
# Add newline if not the last one.
[[ "${count}" = "${total}" ]] || echo
done
# Show skipped.
if [[ ${skipped} != 0 ]]; then
echo
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
fi
# Show error.
if [[ ${failed} != 0 ]]; then
[[ "${skipped}" = 0 ]] && echo
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# Report an error to stdout.
# Always returns code 1.
# Usage: _error <MESSAGE>
function _error() {
echo -e "${color_bred}${*}${color_default}"
return 1
}
# Report a warning to stdout.
# Usage: _warn <MESSAGE>
function _warn() {
echo -e "${color_byellow}${*}${color_default}"
}