summaryrefslogtreecommitdiff
path: root/functions
diff options
context:
space:
mode:
authorDominik Ritter <dritter03@googlemail.com>2016-01-22 21:13:07 +0300
committerDominik Ritter <dritter03@googlemail.com>2016-01-23 03:45:39 +0300
commit15665a4f9524b6dc111e1655680b74f85321959e (patch)
tree33ec79bfbf751a1629446716739ed43e0ad6bc66 /functions
parented28b8d26fa543218656e2165e4c8b26f898f21a (diff)
Joining conditional segments now work as expected. If between the last
printed segment is a full but conditional segment, the joined one gets promoted. This fixes #186
Diffstat (limited to 'functions')
-rw-r--r--functions/utilities.zsh49
1 files changed, 49 insertions, 0 deletions
diff --git a/functions/utilities.zsh b/functions/utilities.zsh
index df022f2c..079e675a 100644
--- a/functions/utilities.zsh
+++ b/functions/utilities.zsh
@@ -134,3 +134,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
+}