2023-11-23 00:13:51 +03:00
# Get the number of avaialble cores (threads).
2023-12-07 01:44:42 +03:00
function _core_count( ) {
2023-12-05 21:50:45 +03:00
cat /proc/cpuinfo | grep ^processor | wc -l
2023-11-23 00:13:51 +03:00
}
2023-12-10 07:20:48 +03:00
# 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'
}
2023-12-16 21:56:30 +03:00
# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
2023-12-17 20:12:33 +03:00
# 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).
2023-12-16 21:56:30 +03:00
# 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
2023-12-17 20:12:33 +03:00
local skipped = 0
2023-12-17 16:27:21 +03:00
local code = 0
2023-12-16 21:56:30 +03:00
# set dafult value to target all supported archives.
if [ [ " ${ targets } " = "" ] ] ; then
2023-12-17 16:27:21 +03:00
_error "No targets provided."
return 1
2023-12-16 21:56:30 +03:00
fi
# iterate each target.
for target in " ${ targets [@] } " ; do
# increment counter.
( ( count++) )
# status info.
2023-12-16 22:52:36 +03:00
local status = " [ ${ count } / ${ total } ] ${ target } "
2023-12-16 21:56:30 +03:00
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
2023-12-17 16:27:21 +03:00
# Add newline if not the last one.
[ [ " ${ count } " = " ${ total } " ] ] || echo
2023-12-16 21:56:30 +03:00
done
2023-12-17 20:12:33 +03:00
# Show skipped.
if [ [ ${ skipped } != 0 ] ] ; then
echo
echo -e " ${ color_byellow } Skipped: ${ skipped } . ${ color_default } "
fi
2023-12-16 21:56:30 +03:00
# Show error.
if [ [ ${ failed } != 0 ] ] ; then
2023-12-17 20:12:33 +03:00
[ [ " ${ skipped } " = 0 ] ] && echo
2023-12-16 21:56:30 +03:00
echo -e " ${ color_bred } Failed: ${ failed } . ${ color_default } "
false
fi
}
2023-12-17 16:27:21 +03:00
2023-12-17 20:12:33 +03:00
# Report an error to stdout.
2023-12-17 16:27:21 +03:00
# Always returns code 1.
2023-12-17 20:12:33 +03:00
# Usage: _error <MESSAGE>
2023-12-17 16:27:21 +03:00
function _error( ) {
echo -e " ${ color_bred } ${ * } ${ color_default } "
2023-12-17 20:12:33 +03:00
return 1
}
# Report a warning to stdout.
# Usage: _warn <MESSAGE>
function _warn( ) {
echo -e " ${ color_byellow } ${ * } ${ color_default } "
2023-12-17 16:27:21 +03:00
}
2023-12-17 20:44:54 +03:00
# Skip current iteration.
# Usage: _skip [MESSAGE]
function _skip( ) {
( ( skipped++) )
[ [ " ${ * } " = "" ] ] || echo -e " ${ color_byellow } ${ * } ${ color_default } "
}