I've recently been getting really excited about zsh, and making myself an extravagant prompt, but I'm stuck trying to align my RPROMPT. I have an output like this:
| [14:59:44] git:(master) |
| /home/zoey/.oh-my-zsh |
| yes, zoey? : 2016-05-26 |
where |
represent the terminal edges. What I want is for the date to be vertically aligned with the time; as where it is, it is prone to be overwritten by a long command, whereas the git info is unlikely to get very long.
Is there any way to the vertical displacement of RPROMPT? Preferably something that is beginner-level zsh, as I'm used to bash, but I can copy-paste stuff if needed.
Here's the relevant bit of my .zshrc
:
# show all but the last dir in the current path
function trim_pwd
{
echo $(pwd | sed -e "s:/$::;s:[^/]*$::")
}
setopt PROMPT_SUBST
# red for error, green for ok
ERRCOL='%(?:%F{green}:%F{red})'
PROMPT='${ERRCOL}[%f%B%F%D{%H:%M:%S}%f%b${ERRCOL}]%f %B$(git_prompt_info)%b
%F{green}%n%f@%F{yellow}%m%f:%F{blue}$(trim_pwd)%B%1/%b%f
%F{magenta}yes, %Bzoey%b?%f : '
RPROMPT='%F%D{%Y-%m-%d}%f'
# update clock per second
TRAPALRM() {
zle reset-prompt
}
TMOUT=1
If I remove all of the formatting stuff, and just show the plain prompt:
setopt PROMPT_SUBST
PROMPT='[%D{%H:%M:%S}] $(git_prompt_info)
%n@%m:%/
yes, zoey? : '
RPROMPT='%D{%Y-%m-%d}'
Edit: I am now able to handle variable-length plain text and fixed-width colored text with:
ERRCOL="%(?:%F{green}:%F{red})"
() {
left="${ERRCOL}[%F%B%D{%H:%M:%S}%b${ERRCOL}]%f "
right="%F%D{%Y-%m-%d}%f"
local bare_left='[00:00:00] '
local bare_right='0000-00-00'
local middle_width=$((${COLUMNS}-1-${#bare_left}-${#bare_right}))
git_prompt_info=$(git_prompt_info)
middle=${(r:$middle_width:: :)git_prompt_info}
PROMPT='${left}${middle}${right}'
PROMPT+=$'\n : '
}
But the variable-length colored text in git_prompt_info
is still counted. I have tried using the invisible
substitution from this answer, but it doesn't seem to work, or I'm using it wrong:
right="%F%D{%Y-%m-%d}%f"
local invisible='%([BSUbfksu]|([FBK]|){*})'
local bare_right=${(S)right//$~invisible}
echo ${(%)bare_right}
echo ${(%)right}
prints the same colored line twice, i.e the substitution does nothing.