From 818082a0b3511bfa7cfba84c69dfddfa519d6245 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Mon, 25 Dec 2023 21:45:54 +0100 Subject: [PATCH] refactor(script): Trim package name if it doesn't fit full-length (#1) --- pacman-zfs-snapshot.sh | 45 ++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/pacman-zfs-snapshot.sh b/pacman-zfs-snapshot.sh index 0113462..f716792 100755 --- a/pacman-zfs-snapshot.sh +++ b/pacman-zfs-snapshot.sh @@ -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}" }