2024-04-06 03:03:58 +03:00
{ . . . }: {
text = ''
# Get the number of avaialble cores (threads).
function _core_count ( ) {
cat /proc/cpuinfo | grep ^ processor | wc - l
}
# Get the number of available memory (in mebibytes).
function _mem_free ( ) {
free - m | sed - n - e ' 2 p' | awk ' { print $ 7 } '
}
# 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 " N o t a r g e t s p r o v i d e d . "
return 1
fi
# iterate each target.
for target in " ' ' ${ targets [ @ ] } " ; do
# increment counter.
( ( count ++ ) )
# status info.
local status = " [ ' ' ${ count } / ' ' ${ total } ] ' ' ${ target } "
_info " ' ' ${ status } "
# Call function.
'' ${ foo } " ''$ { t a r g e t } "
# Show error.
if [ [ '' ${ ? } ! = 0 ] ] ; t h e n
( ( failed ++ ) )
_error " ' ' ${ status } : F a i l e d . "
fi
# Add newline if not the last one.
[ [ " ' ' ${ count } " = " ' ' ${ total } " ] ] || _info
done
# Show skipped.
if [ [ '' ${ skipped } ! = 0 ] ] ; t h e n
_warn
_warn " ' ' ${ color_byellow } S k i p p e d : ' ' ${ skipped } . ' ' ${ color_default } "
fi
# Show error.
if [ [ '' ${ failed } ! = 0 ] ] ; t h e n
[ [ " ' ' ${ skipped } " = 0 ] ] && _error
_error " ' ' ${ color_bred } F a i l e d : ' ' ${ failed } . ' ' ${ color_default } "
false
fi
}
# Skip current iteration.
# Usage: _iterate_skip [MESSAGE]
function _iterate_skip ( ) {
( ( skipped ++ ) )
[ [ " ' ' ${ * } " != " " ] ] && _warn " ' ' ${ color_byellow } ' ' ${ * } ' ' ${ color_default } "
}
# Report an error.
# Always returns code 1.
# Usage: _error <MESSAGE>
function _error ( ) {
> & 2 echo - e " ' ' ${ color_bred } ' ' ${ * } ' ' ${ color_default } "
return 1
}
# Report a warning.
# Usage: _warn <MESSAGE>
function _warn ( ) {
> & 2 echo - e " ' ' ${ color_byellow } ' ' ${ * } ' ' ${ color_default } "
}
# Report a debug.
# Usage: _debug <MESSAGE>
function _debug ( ) {
> & 2 echo - e " ' ' ${ color_bwhite } ' ' ${ * } ' ' ${ color_default } "
}
# Report an info.
# Usage: _info <MESSAGE>
function _info ( ) {
> & 2 echo - e " ' ' ${ color_bwhite } ' ' ${ * } ' ' ${ color_default } "
}
# Check if array contains an element (strict).
# Usage: _contains <ELEMENT> <ARRAY>
function _contains ( ) {
local IFS = $ ' \ n'
local target = " ' ' ${ 1 } "
local array = " ' ' ${ @ : 2 } "
if [ [ " ' ' ${ target } " = " " ] ] || [ [ " ' ' ${ array } " = " " ] ] ; then
help _contains
return 2
fi
for item in '' ${ array [ @ ] } ; d o
[ [ " ' ' ${ item } " = " ' ' ${ target } " ] ] && return 0
done
return 1
}
2024-05-04 13:54:42 +03:00
# Find an index of an element in array.
# Usage: _index_of <ELEMENT> <ARRAY>
function _index_of ( ) {
local element = " ' ' ${ 1 } "
local array = " ' ' ${ @ : 2 } "
local index = 0
for item in '' ${ array [ @ ] } ; d o
[ [ " ' ' ${ item } " = " ' ' ${ element } " ] ] && break
( ( index ++ ) )
done
echo " ' ' ${ index } "
}
2024-04-06 03:03:58 +03:00
# Check if inside Tmux.
function _is_tmux ( ) {
[ [ " ' ' ${ TERM_PROGRAM } " = " t m u x " ] ]
}
# Check if root.
function _is_root ( ) {
[ [ " ' ' ${ UID } " = 0 ] ]
}
# Ring a bell.
function _bell ( ) {
echo - e ' \ a'
}
# Get users.
function _get_users ( ) {
local users = ( " v o r o n i n d " " d a s h a " )
echo '' ${ users [ @ ] }
}
# Force the command to be called twice within the specified period in seconds. Used primarily in important keyboard shortcuts like poweroff.
# Usage: _twice <PERIOD> <COMMAND>
function _twice ( ) {
local IFS = $ ' \ n'
local file = " / t m p / . t w i c e "
local period = '' ${ 1 }
local command = " ' ' ${ @ : 2 } "
if [ [ " $ ( c a t ' ' ${ file } 2 > / d e v / n u l l ) " = " ' ' ${ command } " ] ] ; then
'' ${ command }
return 0
fi
echo " ' ' ${ command } " > " ' ' ${ file } "
sleep '' ${ period }
rm " ' ' ${ file } " 2 > /dev/null
}
'' ;
}