Skip to content

Instantly share code, notes, and snippets.

@kalkin
Created December 3, 2021 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalkin/082ccb939daa4999e56937bc0d9abca6 to your computer and use it in GitHub Desktop.
Save kalkin/082ccb939daa4999e56937bc0d9abca6 to your computer and use it in GitHub Desktop.
Properly resize zsh prompt
# It's not possible to properly resize a zsh prompt after the terminal changed
# it size. People think it's a hard to solve issue. See here:
# https://github.com/romkatv/powerlevel10k/issues/175. There're sollutions like
# patching the ZSH it self https://github.com/romkatv/zsh/tree/fix-winchanged
# but this is a total overkill.
# A far simpler solution is to save the old COLUMN value and on resize to
# calculate how many lines the prompt takes after reflow, move the cursor up
# that many lines and then draw your prompt.
# Example
add-zsh-hook precmd prompt-kalkin-precmd
OLD_COL=0
draw-my-prompt() {
# DRAW THE PROMPT
}
prompt-kalkin-precmd() {
PROMPT=$(draw-my-prompt)
OLD_COL="$COLUMNS"
}
TRAPWINCH() {
# calculate how many lines the prompt takes after terminal reflow
local lines=$(( OLD_COL / COLUMNS ))
[ $(( OLD_COL % COLUMNS )) != 0 ] && lines=$(( lines + 1))
# move the cursor up
echo -e "\033[s\033[${lines}A"
PROMPT=$(draw-my-prompt)
zle reset-prompt
OLD_COL="$COLUMNS"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment