Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual aid to determine the current keymap with Vi keybindings #2

Closed
antoineco opened this issue Aug 1, 2022 · 4 comments
Closed
Labels
enhancement New feature or request

Comments

@antoineco
Copy link

First of all, excellent job with the prompt 👏 I discovered today that Zim had a "new" default prompt after reinstalling my machine, and it is so simple and functional that I want to use it instead of Pure.

Is your feature request related to a problem? Please describe.

When Vi keybindings are enabled (bindkey -v) there is no visual aid to determine whether zle's current keymap is viins (insert mode, default) or vicmd (command mode, when pressing ESC).

I think making such visual aid part of the prompt would be sensible because:

  1. Vi keybindings are built into Zsh
  2. The keymap configuration is part of Zim's default configuration

Describe the solution you'd like

  • Option 1 (preferred)

    Change the prompt symbol to another character of the ASCII table while the vicmd keymap is active. There aren't that many but, as an example, the extended ASCII table contains § (char 167), which should be available in any font and is quite subtle.

    It looks like below (viins / vicmd):

    image
    image

  • Option 2

    Change the color of the prompt symbol to something other than green or red while the vicmd keymap is active. I suggest cyan because it is somewhat close to green, yet distinguishable.

    Potential issue: the red color coding — which indicates that the previous command failed — is lost while in vicmd mode, unless translated to yet another color like magenta. I'm not yet sure how I'd feel about that.

    It looks like below (viins / vicmd):

    image
    image

Describe alternatives you've considered

Spaceship includes a text indicator ([I] / [N]) on the first line, which I find bloated.

Starship allows configuring the shape and color of the prompt symbol in different states, which I find interesting and well designed. But considering the minimal nature of asciiship, I think a sane default that people can customize by forking the prompt's code would be enough.

Changing the terminal's cursor shape while changing mode e.g. from beam to block and back — like in Vim itself — would be controversial and error prone.

Additional context

-

@antoineco antoineco added the enhancement New feature or request label Aug 1, 2022
@ericbn
Copy link
Member

ericbn commented Aug 1, 2022

Hi @antoineco. So good to know you like our lightweight prompt theme! :- D

Not sure which indicator to pick for the vicmd keymap. We're already using # when root, % otherwise. I'd rather keep just the lower ASCII chars for this prompt. Also, it's already green or red depending on the exit status of the last command. I agree changing the terminal cursor shape is controversial and error prone.

If you want to experiment with it yourself, we have two themes that have vicmd keymap indicators implemented:

It's quite a good amount of additional code.

@ericbn
Copy link
Member

ericbn commented Aug 1, 2022

This is what I've come up with: changing the prompt indicator to standout when in vicmd.

diff --git a/asciiship.zsh-theme b/asciiship.zsh-theme
index def650b..7641305 100644
--- a/asciiship.zsh-theme
+++ b/asciiship.zsh-theme
@@ -1,5 +1,23 @@
 # vim:et sts=2 sw=2 ft=zsh

+_prompt_asciiship_vimode() {
+  case ${KEYMAP} in
+    vicmd) print -n '%S%#%s' ;;
+    *) print -n '%#' ;;
+  esac
+}
+
+_prompt_asciiship_keymap_select() {
+  zle reset-prompt
+  zle -R
+}
+if autoload -Uz is-at-least && is-at-least 5.3; then
+  autoload -Uz add-zle-hook-widget && \
+      add-zle-hook-widget -Uz keymap-select _prompt_asciiship_keymap_select
+else
+  zle -N zle-keymap-select _prompt_asciiship_keymap_select
+fi
+
 typeset -g VIRTUAL_ENV_DISABLE_PROMPT=1

 setopt nopromptbang prompt{cr,percent,sp,subst}
@@ -31,5 +49,5 @@ fi

 PS1='
 %(2L.%B%F{yellow}(%L)%f%b .)%(!.%B%F{red}%n%f%b in .${SSH_TTY:+"%B%F{yellow}%n%f%b in "})${SSH_TTY:+"%B%F{green}%m%f%b in "}%B%F{cyan}%~%f%b${(e)git_info[prompt]}${VIRTUAL_ENV:+" via %B%F{yellow}${VIRTUAL_ENV:t}%b%f"}${duration_info}
-%B%(1j.%F{blue}*%f .)%(?.%F{green}.%F{red}%? )%#%f%b '
+%B%(1j.%F{blue}*%f .)%(?.%F{green}.%F{red}%? )$(_prompt_asciiship_vimode)%f%b '
 unset RPS1

@antoineco
Copy link
Author

antoineco commented Aug 2, 2022

I hadn't though about standout, that could be a good approach I think! Underline comes to mind too.

I know that the screen-* / tmux-* terminfos that ship with macOS don't include italics and standout, but

  • those are much less common than xterm-*
  • a lot of macOS users who rely on these capabilities either enable them manually or recompile their terminfo from a more recent ncurses to enable them

I'll try your patch today and report here.
Thanks for looking into this so quickly!

@antoineco
Copy link
Author

antoineco commented Aug 2, 2022

Contrary to what I remembered, standout does work perfectly fine in tmux on macOS, with the default tmux config (TERM=screen-256color) and the OS' default terminfo.
It also works in various terminal emulators I've tried on macOS, with various xterm terminfo variants.

tmux 3.3, screen-256color

image

Terminal.app, xterm-256color

image

Kitty, xterm-kitty

image


Patch applied, I think it looks great:

image

image


Underline wouldn't be very clear (and is probably not as widely supported as standout):

image

image

@ericbn ericbn closed this as completed in 701675a Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants