summaryrefslogtreecommitdiff
path: root/functions/utilities.zsh
diff options
context:
space:
mode:
Diffstat (limited to 'functions/utilities.zsh')
-rw-r--r--functions/utilities.zsh60
1 files changed, 56 insertions, 4 deletions
diff --git a/functions/utilities.zsh b/functions/utilities.zsh
index f855caf5..5ca5b431 100644
--- a/functions/utilities.zsh
+++ b/functions/utilities.zsh
@@ -27,6 +27,9 @@ function set_default() {
}
# Converts large memory values into a human-readable unit (e.g., bytes --> GB)
+# Takes two arguments:
+# * $size - The number which should be prettified
+# * $base - The base of the number (default Bytes)
printSizeHumanReadable() {
typeset -F 2 size
size="$1"+0.00001
@@ -60,10 +63,10 @@ printSizeHumanReadable() {
# worthy. The callback function has access to
# the inner variable $item.
function getRelevantItem() {
- setopt shwordsplit # We need to split the words in $interfaces
-
- local list callback
- list=$1
+ local -a list
+ local callback
+ # Explicitly split the elements by whitespace.
+ list=(${=1})
callback=$2
for item in $list; do
@@ -134,3 +137,52 @@ print_deprecation_warning() {
fi
done
}
+
+# A helper function to determine if a segment should be
+# joined or promoted to a full one.
+# Takes three arguments:
+# * $1: The array index of the current segment
+# * $2: The array index of the last printed segment
+# * $3: The array of segments of the left or right prompt
+function segmentShouldBeJoined() {
+ local current_index=$1
+ local last_segment_index=$2
+ # Explicitly split the elements by whitespace.
+ local -a elements
+ elements=(${=3})
+
+ local current_segment=${elements[$current_index]}
+ local joined=false
+ if [[ ${current_segment[-7,-1]} == '_joined' ]]; then
+ joined=true
+ # promote segment to a full one, if the predecessing full segment
+ # was conditional. So this can only be the case for segments that
+ # are not our direct predecessor.
+ if (( $(($current_index - $last_segment_index)) > 1)); then
+ # Now we have to examine every previous segment, until we reach
+ # the last printed one (found by its index). This is relevant if
+ # all previous segments are joined. Then we want to join our
+ # segment as well.
+ local examined_index=$((current_index - 1))
+ while (( $examined_index > $last_segment_index )); do
+ local previous_segment=${elements[$examined_index]}
+ # If one of the examined segments is not joined, then we know
+ # that the current segment should not be joined, as the target
+ # segment is the wrong one.
+ if [[ ${previous_segment[-7,-1]} != '_joined' ]]; then
+ joined=false
+ break
+ fi
+ examined_index=$((examined_index - 1))
+ done
+ fi
+ fi
+
+ # Return 1 means error; return 0 means no error. So we have
+ # to invert $joined
+ if [[ "$joined" == "true" ]]; then
+ return 0
+ else
+ return 1
+ fi
+}