agkozak / polyglot

Color, ASCII-only Git prompt for zsh, bash, ksh93, mksh, pdksh, oksh, dash, yash, busybox ash, and osh
MIT License
181 stars 13 forks source link

Prompt Rendering Issues in OpenBSD with ksh & tmux Environments #17

Closed scuzzilla closed 1 year ago

scuzzilla commented 1 year ago

Summary

When running the latest version of Polyglot on OpenBSD with ksh and tmux, I encountered 'prompt rendering' issues. These issues, for instance, manifest after exiting man pages. It's notable that the problem arises only in conjunction with tmux.

Setup

toto@obsd ~ $ uname -a
OpenBSD obsd.cuzzilla.org 7.3 GENERIC.MP#3 amd64

toto@obsd ~ $ echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2

toto@obsd ~ $ tmux -V
tmux openbsd-7.3

Steps to Reproduce

  1. (tmux is running) Open a man page:

    toto@obsd ~ $ man ls
  2. Exit the man page.

  3. Observe the erroneous prompt rendering:

    ├⎺├⎺@⎺␉⎽␍ · 
agkozak commented 1 year ago

I'd like to reproduce the problem on my end. I've set up OpenBSD in a virtual machine, and I'm using tmux, but I don't see the prompt garbled after using man. A few questions:

  1. Which terminal are you using? I'm using xterm because it was preinstalled, but you may be using something more modern.
  2. Which pager is man using? Are MANPAGER or PAGER set?
  3. If it turns out you are using the default pager, less, do you have a LESS environment variable, and what is its value?

Thanks!

scuzzilla commented 1 year ago

Hi @agkozak,

To address your queries:

  1. Terminal: I'm currently logged in to a virtual terminal directly, without any terminal emulation, so I'm not using X or any graphical environment.

  2. Pager Settings: I have the following settings in my .kshrc for the pager:

    export PAGER=$(which most)

    So, I'm using most as my pager.

  3. LESS Environment Variable: Yes, I have the LESS environment variable set. Here's its value from my .kshrc:

    export LESS='-iMRS -x2'

I hope this provides clarity for you to reproduce the issue. Let me know if you need more details or any further information.

Best, Salvatore.

agkozak commented 1 year ago

Yes, I can reproduce the problem exactly as you see it, and your fix works.

I've done a lot of testing with this prompt, and I've had to change the non-printing character at least once, that I can remember, so that it would satisfy all setups. I could ultimately only get \016 to work on all of them -- except, it turns out, your case, with tmux involved. Your solution may be the right one. Give me a little while to do some research.

And thanks, as always, for your contributions.

agkozak commented 1 year ago

Yes, the problem can occur on NetBSD even without tmux. I'll keep testing.

agkozak commented 1 year ago

@scuzzilla -

Your patch works as intended as long as you're using a default OpenBSD or NetBSD console.

Unfortunately, if I ssh into an OpenBSD instance using a common terminal like mintty or Windows Terminal, \021 causes problems with wrapping or causes non-printing characters to be displayed. So I don't think we can see it as a blanket solution for OpenBSD and NetBSD.

What I might consider doing is allowing the user to set POLYGLOT_NP themselves, e.g., make line 700 of the master branch

: ${POLYGLOT_NP:-"\016"}

so that the user can put

POLYGLOT_NP="\021"

into their dotfiles -- if that is the non-printing character that addresses their use case.

I don't think there's any ideal solution to this problem; pdksh's support for color is imperfect, and I actually removed support for color at one point in the past for exactly this reason. But I think we're approaching the closest thing to a solution.

scuzzilla commented 1 year ago

Hi @agkozak,

Regarding Microsoft terminal applications: I found (and tested) that the issue can be circumvented by setting the correct terminal emulation. For instance, in the "PowerShell Console", setting

$env:TERM = "vt100" 

does the trick.

I've also tested with Cygwin (mintty) and PuTTY, and the behavior is consistent and without issues.

As for the 'Windows Terminal', it wasn't available in my basic Windows 10 installation. I'm not sure if it's bundled in the default installation. However, given the solutions and workarounds for other Microsoft terminal applications, I'm hopeful.

I believe that with my contribution, along with some concise documentation addressing Microsoft terminal scenarios, we should be able to handle most use cases effectively.

Looking forward to further directions or suggestions.

Best, Salvatore.

agkozak commented 1 year ago

These are all great observations. Let me think about this for a day or two.

scuzzilla commented 1 year ago

Hi @agkozak,

I hope you're doing well. I was just touching base to see if you've had a chance to consider the observations I shared a few days ago.

Regards, Salvatore.

agkozak commented 1 year ago

Yes, I think I know what I'd like to do. I'll write it up for you in a few hours. Thanks for your patience.

agkozak commented 1 year ago

I propose doing the following:

  # pdksh uses an arbitrary non-printing character to delimit color escape
  # sequences in the prompt. In practice, however, it is impossible to find
  # one single non-printing character that will work with all operating systems
  # and terminals. The Polyglot Prompt defaults to \021 for OpenBSD/NetBSD
  # and \016 for everything else. If you want to specify your own non-printing
  # character, do so thus:
  #
  # POLYGLOT_NP="\016" # Set this variable to whatever value you like
  #
  # Or set POLYGLOT_PDKSH_COLORS=0 to disable color entirely in pdksh.

  case ${POLYGLOT_UNAME} in
    NetBSD*|OpenBSD*) POLYGLOT_NP=${POLYGLOT_NP:-"\021"} ;;
    *) POLYGLOT_NP=${POLYGLOT_NP:-"\016"} ;;
  esac

  if _polyglot_is_pdksh &&
     _polyglot_has_colors &&
     [ ${POLYGLOT_PDKSH_COLORS:-1} -ne 0 ]; then

    PS1=$(print "$POLYGLOT_NP\r")
    case $POLYGLOT_UNAME in
      NetBSD*|OpenBSD*) PS1=$PS1$(print "$POLYGLOT_NP") ;;
    esac
    PS1=$PS1$(print "\033[31;1m$POLYGLOT_NP")

etc.

That way we

  1. Make use of your solution for OpenBSD/NetBSD;
  2. Allow the user to tinker via setting POLYGLOT_NP themselves;
  3. Allow the user to avoid problems by turning color off (POLYGLOT_PDKSH_COLORS=0)

What do you think? If you agree, make the necessary changes, and I'll add a section to the documentation explaining how it all works.

scuzzilla commented 1 year ago

Hi @agkozak,

Thank you for reviewing my comments. The proposed changes are completely reasonable, and I've already incorporated them into the original PR.

Please let me know if there's anything else you'd like to add or modify before merging.

Best, Salvatore.

agkozak commented 1 year ago

Fantastic. Thanks for all your help.