refactor(script): Trim package name if it doesn't fit full-length (#1)

This commit is contained in:
hygienic-books 2023-12-25 21:45:54 +01:00
parent 04caca48a5
commit 818082a0b3

View File

@ -180,6 +180,24 @@ function find_max_dataset_name_length () {
fi
}
function trim_single_remaining_package_name () {
local pkg_name
pkg_name="${shorter_pkg_list}"
case 1 in
# Trim to 1 to 3 characters, no trailing ellipsis (...)
$(( 1<=pkgs_list_max_length && pkgs_list_max_length<=3 )))
pkg_name="${pkg_name::${pkgs_list_max_length}}"
;;
# Show as many pkg name characters as we can while also
# fitting an ellipsis into the name (...) to indicate
# that we've cut the pkg name off at the end.
$(( pkgs_list_max_length>=4 )))
pkg_name="${pkg_name::$(( pkgs_list_max_length - 3 ))}"'...'
;;
esac
shorter_pkg_list="${pkg_name}"
}
function trim_pkg_list_oneline () {
local available_pkg_list_length
available_pkg_list_length="$((${max_zfs_snapshot_name_length} - ${max_dataset_name_length}))"
@ -190,18 +208,25 @@ function trim_pkg_list_oneline () {
fi
local shorter_pkg_list
shorter_pkg_list="${unabridged_pkg_list_oneline}"
while [[ "${#shorter_pkg_list}" -gt "${pkgs_list_max_length}" ]]; do
shorter_pkg_list="${shorter_pkg_list%${pkg_separator}*}"
if ! grep -Piq "${pkg_separator}" <<<"${shorter_pkg_list}"; then
# Only one package remains in package list, no need to continue
break
fi
done
if [[ "${#shorter_pkg_list}" -gt "${pkgs_list_max_length}" ]]; then
# If this is still too long we empty the package list
if [[ "${pkgs_list_max_length}" -le '0' ]]; then
# User wants zero characters of pkg names in snapshot name,
# no need to even find an appropriate pkg name string. Just
# set to empty string and we're done here.
shorter_pkg_list=''
else
shorter_pkg_list="${unabridged_pkg_list_oneline}"
while [[ "${#shorter_pkg_list}" -gt "${pkgs_list_max_length}" ]]; do
shorter_pkg_list="${shorter_pkg_list%${pkg_separator}*}"
if ! grep -Piq "${pkg_separator}" <<<"${shorter_pkg_list}"; then
# Only one package remains in package list, no need to continue
break
fi
done
if [[ "${#shorter_pkg_list}" -gt "${pkgs_list_max_length}" ]]; then
trim_single_remaining_package_name
fi
fi
trimmed_pkg_list_oneline="${shorter_pkg_list}"
}