nutools/lib/ulib/prefixes

53 lines
1.1 KiB
Bash

##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Gestion de chemins standardisés
##@cooked nocomments
##@require base
##@require sysinfos
uprovide prefixes
urequire base sysinfos
UTOOLS_PREFIXES=(USER HOME)
function get_USER_prefix() {
echo "$USER"
}
function get_HOME_prefix() {
echo "$HOME"
}
function has_prefix() {
array_contains UTOOLS_PREFIXES "$1" && return 0
local prefix
for prefix in "${UTOOLS_PREFIXES[@]}"; do
if beginswith "$1" "$prefix/"; then
return 0
fi
done
return 1
}
function expand_prefix() {
local prefix
for prefix in "${UTOOLS_PREFIXES[@]}"; do
if beginswith "$1" "$prefix/" || [ "$1" == "$prefix" ]; then
echo "$(get_${prefix}_prefix)${1#$prefix}"
return
fi
done
echo "$1"
}
function list_prefixes() {
local prefix
for prefix in "${UTOOLS_PREFIXES[@]}"; do
echo "$prefix"
done
}
function dump_prefixes() {
local prefix
for prefix in "${UTOOLS_PREFIXES[@]}"; do
echo "$prefix=$(expand_prefix "$prefix")"
done
}