Files
linux-workspace/functions.sh
2025-09-03 20:19:22 +02:00

141 lines
2.1 KiB
Bash

# Linux Workspace Functions
if [ -z "$time_ms" ]; then
readonly time_ms='date +%s%3N'
fi
function is_fast_init() {
(( $LWS_FAST ))
}
function is_slow_init() {
! is_fast_init
}
function xlog() {
if true_false "$LWS_DEBUG"; then
echo "$@"
fi
}
function jql() {
jq -C . "$@" | less -r
}
function ffile() {
local filename="$1"
shift
file $(type -p "$filename") $@
}
# print time in seconds with nanoseconds precission
function utime() {
date '+%s.%N'
}
function sqltime() {
date '+%Y-%m-%d %H:%M:%S'
}
function lines() {
tr ":" "\n"
}
function path() {
echo "$PATH" | lines
}
function in_path() {
[[ "$PATH" =~ (^|:)"${1}"(:|$) ]]
}
function append_path() {
if ! in_path "$1"; then
PATH="$PATH:$1"
fi
}
function append_path_try() {
if [[ -e "$1" ]]; then
append_path "$1"
fi
}
function prepend_path() {
if ! in_path "$1"; then
PATH="$1:$PATH"
fi
}
function prepend_path_try() {
if [[ -e "$1" ]]; then
prepend_path "$1"
fi
}
function remove_path() {
PATH=$(path | grep -v "$1" | tr "\n" ":")
}
function uniq_path() {
path | awk '!seen[$0]++' | tr "\n" ":" | sed -r 's/:+/:/g' | sed -r 's/^:|:$//g'
}
function set_uniq_path() {
PATH="$(uniq_path)"
}
function is_alias() {
alias "$1" > /dev/null 2>&1
}
function can_run() {
local application="$1"
command -v "$application" > /dev/null
}
function source_directory() {
local mask="$1"
xlog "LOAD DIR $mask"
for file in $mask; do
if [[ -e "$file" ]]; then
if (( $LWS_DEBUG )); then
start=$($time_ms)
source "$file"
stop=$($time_ms)
echo "LOAD FILE $file $((stop - start)) ms"
else
source "$file"
fi
fi
done
}
function source_directory_sh() {
source_directory "$1/*.sh"
}
# use: dump_args "$@"
function dump_args() {
for i in $*; do
echo $i
done
}
function source_try() {
local script="$1"
if [[ -e "$script" ]]; then
source "$script"
fi
}
function true_false() {
# lower case and compare
# [[ "${1,,}" =~ ^(1|true|yes)$ ]]
[[ "${1@L}" =~ ^(1|true|yes)$ ]]
}
function is_wsl() {
grep -q -i wsl /proc/version
}