lxqt / qterminal

A lightweight Qt-based terminal emulator
https://lxqt.github.io
GNU General Public License v2.0
595 stars 150 forks source link

percent (%) character displayed before prompt when zsh is used #778

Open M4he opened 3 years ago

M4he commented 3 years ago
Expected Behavior
Current Behavior
Possible Solution

I have found the following two end-user workarounds (not solutions) so far. Both with downsides. The permanent solution would be to fix whatever is causing this in qterminal directly.

Steps to Reproduce (for bugs)
  1. Boot up a Debian 10 Xfce live image in a virtual machine (*-xfce.iso from here). All following instructions refer to the live system.
  2. In the live system, open up a terminal and:
    sudo apt update
    sudo apt install qterminal zsh
    chsh -s /bin/zsh   # (the password for the live user is "live")
  3. Create a file called .zshrc in the current home directory (~/.zshrc) with the following contents:
    autoload -Uz compinit promptinit
    compinit
    promptinit
    prompt adam1
  4. Log out ("Applications" menu -> "Log Out") and back in (username is user, password is live). The command echo $SHELL should now display /bin/zsh.
  5. Open up qterminal. The issue should appear. If not, try closing and re-opening a few times.
Context

The zsh shell is often used in conjunction with oh-my-zsh to provide convenient theming and extension management and configuration for zsh. As of now, it doesn't seem possible to use such zsh configuration with qterminal without having buggy behavior of the prompt in new terminal instances (erroneous newlines or % characters displayed above the prompt).

It would be great if qterminal could be compatible with zsh configurations the same way that all other popular terminal emulators are.

System Information
Notes and References

Notes:

References:

yan12125 commented 3 years ago

Thanks for this great report! As we've previously discussed on IRC, this is a valid bug report even the reporter is using some very old qterminal as I'm able to reproduce the issue on Arch Linux with the latest git-master.

on Ubuntu it only occasionally happens without oh-my-zsh

That is a useful information. I moved away from oh-my-zsh some time ago. It might be the reason that I seldom see such an issue recently.

ssliman commented 3 years ago

@M4he

Go inside the .zshrc file at the top you'll find:

# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH <- uncomment this line

That's what worked for me

M4he commented 3 years ago

@xZliman

I tried adding your quoted line¹ (uncommented) to the very top of my ~/.zhsrc file but it doesn't make a difference for me.

¹ I did not have the comment in mine, because my file was based on an older template apparently.

gitzhangzhao commented 3 years ago

@M4he I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this: 2021-06-09_22-14 I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi This really solved my problem. 2021-06-09_22-18 I hope this will help you.

EigenTheory commented 3 years ago

@M4he I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this: 2021-06-09_22-14 I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi This really solved my problem. 2021-06-09_22-18 I hope this will help you.

This worked great for me and I tried all other solutions! This was the only solution that provided results.

flaviocamilo commented 3 years ago

@M4he I also encountered this problem, I use oh-my-zsh and powerlevel10k, I found that powerlevel10k instant prompt feature caused this problem: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" fi Adding this statement at the top of the .zshrc prints a reflected % on the first line of the newly opened terminal, like this: 2021-06-09_22-14 I tracked the start-up of the zsh, but in the end I didn't find the cause of the problem, but it must have been caused by powerlevel10k's instant prompt, my simple solution is to discard all the output: if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null fi This really solved my problem. 2021-06-09_22-18 I hope this will help you.

I can reproduce this solution.

Thanks!

romkatv commented 3 years ago

Hey everyone. I'm the developer of powerlevel10k. @flaviocamilo has tagged me on https://github.com/romkatv/powerlevel10k/issues/1445.

When you see the inverted percent before the first prompt upon opening a new terminal, this is almost always caused by a bug in the terminal or the window manager (wm). You can diagnose the nature of the bug with the following steps.

First, add this snippet at the very top of ~/.zshrc:

PS1='%~ '
RPS1='%n@%m'
return

This sets left and right prompt to very simple values and returns before evaluating anything else in your zsh config. If you can reproduce the problem with this .zshrc (that is, you still occasionally get the inverted percent sign upon opening a new terminal), this means the issue is not with your zsh theme or your zsh config in general.

The next step is to figure out which of the two common terminal/wm bugs is the culprit. The most common variety is that the TTY reports its size incorrectly for a short while after being created. To see whether this is the case add the following snippet at the very top of ~/.zshrc:

print -r -- "$COLUMNS x $LINES"
sleep 1
print -r -- "$COLUMNS x $LINES"

When you open a new terminal, you should see the same dimensions printed twice and they should match the real dimensions of your terminal window. If you see different numbers reported on the first and the second line, or if they don't match the real dimensions of the terminal window, this is an indication of the bug I was referring to earlier.

If you always see correct dimensions printed twice, check for another terminal/wm bug that can cause the inverted percent sign. This bug involves incorrect line wrapping during a short duration after the TTY is created. To check for it, add the following snippet at the very top of ~/.zshrc:

() {
  emulate -L zsh -o prompt_percent
  local -i n='COLUMNS - 1'
  print -Prn -- X${(pl.$n.. .)}$'\r%E'
}

When you open a new terminal, you should NOT see "X" at the top of your terminal window. If you do see it, that's a bug in the terminal/wm.

Now, why do you see the inverted percent sign when using powerlevel10k instant prompt but not when using other themes? This is simply because other themes are unable to display prompt fast enough. Powerlevel10k displays prompt very quickly after you open a terminal window, so it can be affected by a terminal/wm bug that only manifests itself for a short duration after a TTY is created.

my simple solution is to discard all the output:


if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" > /dev/null
fi

By redirecting the output to /dev/null you've disabled instant prompt. If you really want to disable instant prompt, simply delete this whole snippet. It'll have the same effect (no instant prompt) and will be simpler and faster. Alternatively, you can leave the original instant prompt snippet unchanged and set POWERLEVEL9K_INSTANT_PROMPT=off in ~/.p10k.zsh (search that file for "POWERLEVEL9K_INSTANT_PROMPT" -- this parameter is already present there but with a different value). You can also do this by running p10k configure and selecting "Off" when asked what you want w.r.t. instant prompt.

These are just workarounds though. Ideally the bug needs to be fixed in the terminal or the window manager.

gitzhangzhao commented 3 years ago

sleep x reduces the frequency of % occurrence while retaining instant prompt, add sleep 0.1 at the top of ~/.zshrc. Sleep time can be tried by yourself. For me, if the sleep time is between 0.05 and 0.1, about 15 zsh starts will have 1 %. The benefit is that the shell starts faster than simply disable instant prompt.

romkatv commented 3 years ago

@gitzhangzhao Could you try it with the latest version of Powerlevel10k? I’ve implemented a partial workaround that might get rid of the inverted percent sign even without the sleep.

gitzhangzhao commented 3 years ago

@gitzhangzhao Could you try it with the latest version of Powerlevel10k? I’ve implemented a partial workaround that might get rid of the inverted percent sign even without the sleep.

@romkatv It worked, thank you very much!

romkatv commented 3 years ago

@gitzhangzhao Thanks for verifying!

It's really just a workaround though. As I mentioned earlier, this is a bug either in the terminal or in the window manager. The bug is not specific to powerlevel10k and can easily be reproduced with a tiny zsh config. It's not specific to zsh either, which can be demonstrated by instructing the terminal to launch stty size instead of the login shell. You should see that the output of stty size doesn't match the real terminal dimensions when you open a new terminal.