From this post, Get Function Into PS1 (Zsh)? , it seems to be possible to execute a function inside a ZSH prompt`.
Here how to use a function into PROMPT :
setopt PROMPT_SUBST
slash_color () { dirs | awk -F "/" ' {for (i=1; i<=NF; i++) printf "\033[38;5;75m"$i"\033[38;5;206m/"} '; }
PS1='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|$(slash_color)%F{13}|%F{7} '
Below the result :
ISSUE : But now, I am faced to another problem : when I am inside a directory, the first completion with TAB, for example "vim" ⇨Tab shift all the display to roughly one hundred of space characters to the right.
To illustrate, here is a screen capture :
As you can see, just after typing vim te
and after press ⇨Tab key, the command vim te
is pushed to the right: I don't understand where this shifting could come from.
I am using zsh-5.8 from MacPorts and I also could reproduce the issue by compiling the sources of zsh-5.8 on macOS Catalina.
If someone could have an explanation/suggestion/clue, this would be fine to tell it.
PS: I suspect that using a simple ZSH environment variable could fix this strange behavior.
I have done some research and find these 2 interesting links :
It seems the shifting that occurs in my case is caused by the interpretation of ANSI escape characters but I didn't understand all the details.
However, i tried to modify slash_color()
function like this :
slash_color () {
dirs | awk -F "/" '{ blue="\e[38;5;75m"; \
pink="\e[38;5;206m"; \
for (i=1; i<NF; i++) \
printf blue $i pink "/"; \
printf blue $NF pink; \
printf "\n"
}'
}
But this doesn't work, I get the following error about awk
:
awk: cmd. line:1: warning: escape sequence `\e' treated as plain `e'
In my original issue (shifting to the right), everything happens like ZSH was badly computing the length of the real visible PATH of PROMPT and overestimating it? That would explain why I am shifted to the right.
Could anyone take a look please at the two links that I gave above, it could contain the solution in my case.
UPDATE 1: @Allan, here you can see my last attempt :
# Prompt for zsh : path separated by %F{13} slash to better see where we are
setopt PROMPT_SUBST
slash_color() {
dirs | awk -F "/" '{ blue="\033[38;5;75m"; \
pink="\033[38;5;206m"; \
for (i=1; i<NF; i++) \
printf blue $i pink "/"; \
printf blue $NF pink; \
printf "\n";
}';
}
# Last method using my_precmd_hook_function
my_precmd_hook_function() {
slash_path=$(slash_color)
}
autoload -U add-zsh-hook
add-zsh-hook precmd my_precmd_hook_function
PROMPT='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|''$slash_path''%F{13}|%F{7} '
Rendering of PATH with pink forward slash is good but the issue of shifting to the right remains when I type cd
+⇨Tab or ls
+⇨Tab. The presence of printf "\n";
doesn't change anything on the result.
Below a capture illustrating this :
SOLUTION FINALLY FOUND : my perseverance has paid off, everyting is working fine, no more shifting and colorized forward slash in PATH, with :
# Prompt for zsh : path separated by %F{13} slash to better see where we are
setopt PROMPT_SUBST
# Path with colorized forward slash
slash_color() {
dirs | awk -F "/" '{ blue="%{\033[38;5;75m%}"; \
pink="%{\033[38;5;206m%}"; \
for (i=1; i<NF; i++) \
printf blue $i pink "/"; \
printf blue $NF pink; \
}';
}
# Prompt final
PROMPT=$'%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|$(slash_color)%F{13}|%F{7} '
# Zsh reverse auto-completion
bindkey '^[[Z' reverse-menu-complete
Thanks a lot for all your advices.
slash_color ()
puts the mess in the display of the prompt, especially, non visible characters like ANSI escape codes seem to be added to the prompt when I try to do aTAB
completion and all commandcd foo/
is shifted on the right. This issue occurs both onzsh-5.7.1
andzsh-5.8
. – youpilat13 Apr 19 '20 at 14:35zsh-5.8
fixes it, but here in this post, this is another problem. – youpilat13 Apr 19 '20 at 15:27