agkozak / agkozak-zsh-prompt

A fast, asynchronous Zsh prompt with color ASCII indicators of Git, exit, SSH, virtual environment, and vi mode status. Framework-agnostic and customizable.
MIT License
290 stars 26 forks source link

Under some conditions, when typing next cmd during previous cmd execution, prompt formatting gets mangled #39

Open AndydeCleyre opened 2 years ago

AndydeCleyre commented 2 years ago

Hello!

Sometimes I begin typing the next command while the previous one is running, and this works, even with this bug. But the prompt display gets temporarily mangled in this event.

This is hard for me to reliably reproduce with a clean setup, and so I'm sorry for such a messy report.

I think that to reproduce we need:

Here's my prompt-related code:

setopt promptsubst

() {
  emulate -L zsh

  local usual_distro='Arch Linux' usual_host='pop-os' usual_user='andy'
  local ptime='%D{%L:%M}'

  local agkozakpath p10kpath
  agkozakpath=~/Code/plugins/zsh/agkozak-zsh-prompt/agkozak-zsh-prompt.plugin.zsh
  p10kpath=~/Code/plugins/zsh/powerlevel10k/powerlevel10k.zsh-theme

  if [[ -r $agkozakpath ]] {
    if [[ $HOST != $usual_host || $USERNAME != $usual_user ]] {
      AGKOZAK_USER_HOST_DISPLAY=1
    } else {
      AGKOZAK_USER_HOST_DISPLAY=0
    }
    AGKOZAK_CUSTOM_SYMBOLS=('⇣⇡' '⇣' '⇡' '+' 'x' '!' '>' '?' 'S')
    AGKOZAK_LEFT_PROMPT_ONLY=1
    AGKOZAK_PROMPT_CHAR=('%F{white}%B->>%b%f' '#' ':')
    AGKOZAK_PROMPT_DIRTRIM=4
    AGKOZAK_PROMPT_DIRTRIM_STRING=…
    if [[ $TMUX ]] && [[ $TMUX != *tmate* ]] {
      AGKOZAK_CUSTOM_RPROMPT='${(j: :)${(f)"$(tmux lsw -F "#{?#{==:#{pane_tty},$TTY},%B%F{white#},%F{blue#}}#{?#{!=:#W,zsh},#W,%%}#{?#{!=:#{window_panes},1},+,}%f%b" 2>/dev/null)"}} %F{green}'$ptime'%f'
    } else {
      AGKOZAK_CUSTOM_RPROMPT='%F{green}'$ptime'%f'
    }

    . $agkozakpath

  } elif [[ -r $p10kpath ]] {

    # ...

  } else {
    # ...
  }
}

PROMPT2='%B%F{blue}->…%f%b '

When run within a tmux session, run sleep 1, and then keep typing while that finishes:

~/Code/zpy (develop S)
->> sleep 1                                                           % 12:37
xxxxxxxxxxxxxx%                                                               }~/Code/zpy} (develop S)${AGKOZAK_PROMPT_WHITESPACE}}${AGKOZAK_PROMPT_CHAR[1]:-%} xxxxxxxxxxxxxxxxxxxxx

image

agkozak commented 2 years ago

Hello, @AndydeCleyre! Thanks for the report. I have seen something like that myself on occasion, but I have never been able to pinpoint the exact conditions. I'll give your configuration a try.

agkozak commented 2 years ago

Your configuration is great -- I've gotten the problem to occur a couple of times already. I'll get to work on this.

agkozak commented 2 years ago

I only seem to have this problem if I use zdharma-continuum/fast-syntax-highlighting (it doesn't happen with zsh-users/zsh-syntax-highlighting). In fact, I run into it reliably with a mere

source /path/to/agkozak-zsh-prompt.plugin.zsh
source /path/to/fast-syntax-highlighting.plugin.zsh

in my .zshrc, and sleep 1, followed by fast typing, at the command line -- as you pointed out.

Reversing the order in which I load the two plugins seems to help slightly; if any garbling occurs, it is corrected when I switch to vi command mode. Interesting.

I'll keep playing around with this.

AndydeCleyre commented 2 years ago

Thanks! The FSH interaction was something I only considered while writing this issue, and your corroboration spurred me to try a bisect on FSH. Over here, it looks like the problem started with:

commit 08a4ce96953764f03d617f1c52e9a2a3ad6f58c2
Author: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date:   Sun Feb 23 01:01:14 2020 +0100

    plugin.zsh: Use emulate instead of bare setopt

diff --git a/fast-syntax-highlighting.plugin.zsh b/fast-syntax-highlighting.plugin.zsh
index f11177d..4b88d3f 100644
--- a/fast-syntax-highlighting.plugin.zsh
+++ b/fast-syntax-highlighting.plugin.zsh
@@ -72,7 +72,9 @@ _zsh_highlight()
     return $ret
   fi

-  setopt localoptions warncreateglobal noksharrays noshwordsplit extendedglob typesetsilent nokshglob
+  emulate -LR zsh
+  setopt extendedglob warncreateglobal typesetsilent noshortloops
+
   local REPLY # don't leak $REPLY into global scope
   local -a reply
AndydeCleyre commented 2 years ago

So I checked output of setopt just after that sets the options, from before and after:

--- BEFORE.txt  2022-04-04 22:25:12.080597430 -0400
+++ AFTER.txt   2022-04-04 22:26:08.085803758 -0400
@@ -1,22 +1,13 @@
-alwaystoend
-autopushd
-nocaseglob
-completeinword
 extendedglob
-noflowcontrol
-globdots
-histignorealldups
-histignorespace
-histverify
 interactive
-interactivecomments
 localoptions
+localpatterns
+localtraps
 login
 monitor
-promptsubst
-pushdignoredups
-sharehistory
 shinstdin
+noshortloops
 typesetsilent
 warncreateglobal
 zle
AndydeCleyre commented 2 years ago

I'm currently testing with promptsubst added to the list. So far, so good.

EDIT: Nope, reproduced.

AndydeCleyre commented 2 years ago

I haven't seen this since locally reverting https://github.com/zdharma-continuum/fast-syntax-highlighting/commit/08a4ce96953764f03d617f1c52e9a2a3ad6f58c2. I'll open an issue there linked to this one.

agkozak commented 2 years ago

Fascinating. Thanks for staying on top of this issue, @AndydeCleyre.