summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorromkatv <roman.perepelitsa@gmail.com>2019-05-27 13:45:01 +0300
committerromkatv <roman.perepelitsa@gmail.com>2019-05-27 13:45:01 +0300
commit784dc059cd818b8023ec4f811bde1bbb222b1726 (patch)
treecfee9f1b85f0324dce13500843a4bb0459d00ea6
parentbbebf516abeb55c96ad6e83478581b1230df6ec2 (diff)
add _p9k_prompt_length
-rwxr-xr-xfunctions/utilities.zsh23
1 files changed, 23 insertions, 0 deletions
diff --git a/functions/utilities.zsh b/functions/utilities.zsh
index 0619c035..f5f4f7dd 100755
--- a/functions/utilities.zsh
+++ b/functions/utilities.zsh
@@ -45,6 +45,29 @@ function _p9k_g_expand() {
typeset -g $1=${(g::)${(P)1}}
}
+# If we execute `print -P $1`, how many characters will be printed on the last line?
+# Assumes that `%{%}` and `%G` don't lie.
+#
+# _p9k_prompt_length '' => 0
+# _p9k_prompt_length 'abc' => 3
+# _p9k_prompt_length $'abc\nxy' => 2
+# _p9k_prompt_length $'\t' => 8
+# _p9k_prompt_length '%F{red}abc' => 3
+# _p9k_prompt_length $'%{a\b%Gb%}' => 1
+function _p9k_prompt_length() {
+ emulate -L zsh
+ local -i x y=$#1 m
+ if (( y )); then
+ while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do (( y *= 2 )); done
+ local xy
+ while (( y > x + 1 )); do
+ m=$(( x + (y - x) / 2 ))
+ typeset ${${(%):-$1%$m(l.x.y)}[-1]}=$m
+ done
+ fi
+ _P9K_RETVAL=$x
+}
+
typeset -g _P9K_BYTE_SUFFIX=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
# 42 => 42B