From: Marlon Richert <marlon.richert@gmail.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] Add customizable `vcs` prompt theme (was Re: [RFC][PATCH] `newuser` prompt theme)
Date: Thu, 10 Jun 2021 17:01:44 +0300 [thread overview]
Message-ID: <CAHLkEDvxfU93Fzk2aRw9-f+H+91HhYcYQ++v3rWnGqqv++VEpQ@mail.gmail.com> (raw)
In-Reply-To: <CAHLkEDupdk6Y95_Ju68GG-t0mtjd7yXgrwE+BAZwCNtUiok=mA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2317 bytes --]
Actually, I just realized that what I was doing with line-init is not
actually solving my problem. So, here's a new version of the patch
that simply lets chpwd call precmd. See attachment.
On Thu, Jun 10, 2021 at 4:47 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> On Thu, Jun 10, 2021 at 12:30 PM Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
> >
> > There is //'%%'/% in a few places. What's the logic behind it?
>
> Some prompt escape codes (such as %b for bold) need %% when used in
> vcs_info -- except when they are used in nvcsformats.
>
>
> > I think invoking prompt_vcs_setup twice in the same shell will produce
> > an error. I haven't tried it though.
>
> I just tried it and I didn't get any errors. Why do you think it would
> produce an error?
>
>
> > prompt_vcs_chpwd always prints at least one line to the TTY regardless
> > of configuration options. Is this intended?
>
> Yes, that was my intention, but I can see how it could be useful to be
> able to customize that. I'll change it.
>
>
> > Why is prompt_vcs_precmd checking whether prompt_vcs_fd is a readable
> > fd? How can it not be?
>
> I don't know how or why, but if I don't check for that, then I get
> this error repeatedly:
>
> prompt_vcs_precmd:6: failed to close file descriptor 11: bad file descriptor
>
>
> > There is a comment in prompt_vcs_precmd that says it's going to kill a
> > process while in fact it closes the file descriptor. That may
> > *eventually* kill the process when it decides to print something
> > (assuming it hasn't blocked SIGPIPE or is checking for write errors).
>
> True. I'll change it to actually kill the process, too.
>
>
> > What is the purpose of the "Info is not for current dir" check? Is
> > this to account for the possibility of `cd` from a zle widget? If so,
> > perhaps handle this by hooking chpwd?
>
> The purpose is to account for the possibility that we receive the info
> _after_ we've already changed to a new dir. However, if I correct the
> piece of code you mentioned previously, then that shouldn't be
> necessary.
>
>
> > prompt_vcs_fd-widget has a check for [[ -n $1 ]]. When is it false?
>
> When prompt_vcs_fd-widget is being called incorrectly. :) Perhaps it
> should not return 1 in that case, though.
>
>
> Thanks again. New version of the patch attached.
[-- Attachment #2: 0001-Add-customizable-vcs-prompt-theme.txt --]
[-- Type: text/plain, Size: 7300 bytes --]
From f08adb8d99e3ec162aacb8a217028b7a2dc5d4ca Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Thu, 10 Jun 2021 16:58:33 +0300
Subject: [PATCH] Add customizable 'vcs' prompt theme
---
Functions/Prompts/prompt_vcs_setup | 199 +++++++++++++++++++++++++++++
1 file changed, 199 insertions(+)
create mode 100644 Functions/Prompts/prompt_vcs_setup
diff --git a/Functions/Prompts/prompt_vcs_setup b/Functions/Prompts/prompt_vcs_setup
new file mode 100644
index 000000000..5e3ecf615
--- /dev/null
+++ b/Functions/Prompts/prompt_vcs_setup
@@ -0,0 +1,199 @@
+##
+# Prompt that can be customized through vcs_info
+#
+zmodload -F zsh/system p:sysparams
+autoload -Uz add-zle-hook-widget add-zsh-hook vcs_info
+
+# Prompt segments
+# Normal %b and %(...) need extra % in vcs_info -- except in nvcsformats.
+readonly -gHA _prompt_vcs_ps1_default=(
+ left $'%%(?,%F{green},%F{red})%#%%b%f%%s '
+ right '%B%F{blue}%n%b%f%k@%F{magenta}%m%f'
+ chpwd $'\n%B%F{blue}%~%%b%f/\n'
+ staged '%B%F{green}+%b%f'
+ unstaged '%B%F{red}*%b%f'
+ action '%B%F{red}%a%%b%f'
+ branch '%B%F{cyan}%b%%b%f'
+ repo '|%B%F{blue}%r%%b%f'
+)
+
+prompt_vcs_help() {
+ print -r -- \
+"Customizable prompt theme that includes VCS info for the current dir (for
+example, git status) and asynchronously checks for repo changes.
+
+To customize it, copy-paste any of the code below to your .zshrc file and edit
+it there:
+
+ # For each of the following three entries:
+ # 1st string is the left prompt.
+ # 2nd string is the right prompt.
+ # 3rd string is printed whenever you change dirs (before the left prompt).
+
+ # Normal prompt:
+ zstyle ':vcs_info:*:prompt_vcs:*' nvcsformats \\
+ "${(q+)_prompt_vcs_ps1_default[left]//'%%'/%}" \\
+ "${(q+)_prompt_vcs_ps1_default[right]//'%%'/%}" \\
+ "${(q+)_prompt_vcs_ps1_default[chpwd]//'%%'/%}"
+
+ # Prompt inside VCS repo:
+ zstyle ':vcs_info:*:prompt_vcs:*' formats \\
+ ${(q+)_prompt_vcs_ps1_default[left]} \\
+ ${(q+):-%u%c$_prompt_vcs_ps1_default[branch]$_prompt_vcs_ps1_default[repo]} \\
+ ${(q+)_prompt_vcs_ps1_default[chpwd]}
+
+ # Prompt during ongoing VCS action:
+ zstyle ':vcs_info:*:prompt_vcs:*' actionformats \\
+ ${(q+)_prompt_vcs_ps1_default[left]} \\
+ ${(q+):-%u%c$_prompt_vcs_ps1_default[action]$_prompt_vcs_ps1_default[repo]} \\
+ ${(q+)_prompt_vcs_ps1_default[chpwd]}
+
+ # These customize the values of %c and %u, respectively:
+ zstyle ':vcs_info:*:prompt_vcs:*' stagedstr ${(q+)_prompt_vcs_ps1_default[staged]}
+ zstyle ':vcs_info:*:prompt_vcs:*' unstagedstr ${(q+)_prompt_vcs_ps1_default[unstaged]}
+
+For more info on the settings above, see
+http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html and the end of
+http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#vcs_005finfo-Configuration"
+}
+
+# Sets a style if it hasn't been set yet.
+_prompt_vcs_zstyle() {
+ local -a val
+ zstyle -g val "$1" "$2"
+ (( $#val )) ||
+ zstyle "$@"
+}
+
+_prompt_vcs_info() {
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' max-exports 3 # Default is 2.
+
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' nvcsformats \
+ "${_prompt_vcs_ps1_default[left]//'%%'/%}" \
+ "${_prompt_vcs_ps1_default[right]//'%%'/%}" \
+ "${_prompt_vcs_ps1_default[chpwd]//'%%'/%}"
+
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' formats \
+ "$_prompt_vcs_ps1_default[left]" \
+ "%u%c$_prompt_vcs_ps1_default[branch]$_prompt_vcs_ps1_default[repo]" \
+ "$_prompt_vcs_ps1_default[chpwd]"
+
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' actionformats \
+ "$_prompt_vcs_ps1_default[left]" \
+ "%u%c$_prompt_vcs_ps1_default[action]$_prompt_vcs_ps1_default[repo]" \
+ "$_prompt_vcs_ps1_default[chpwd]"
+
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' stagedstr \
+ "$_prompt_vcs_ps1_default[staged]"
+
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' unstagedstr \
+ "$_prompt_vcs_ps1_default[unstaged]"
+
+ vcs_info prompt_vcs # Call with namespace.
+
+ print -rNC1 -- \
+ "${(q+)vcs_info_msg_0_}" "${(q+)vcs_info_msg_1_}" "${(q+)vcs_info_msg_2_}"
+}
+
+prompt_vcs_chpwd() {
+ emulate -L zsh
+
+ # Don't check for changes at this point, for performance reasons.
+ typeset -ga _prompt_vcs_info_msg_=( "${(0@Q)$(
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-changes no
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-staged-changes no
+ _prompt_vcs_info
+ )}" )
+ PS1="$_prompt_vcs_info_msg_[1]"
+ RPS1="$_prompt_vcs_info_msg_[2]"
+ print -nPr -- "$_prompt_vcs_info_msg_[3]"
+
+ # Call precmd just in case, because chpwd can happen without a new prompt.
+ prompt_vcs_precmd
+}
+
+prompt_vcs_precmd() {
+ emulate -L zsh
+
+ # Terminate any pending vcs_info request before starting a new one.
+ # Second test checks if fd is readable, to avoid this error being printed:
+ # failed to close file descriptor XX: bad file descriptor
+ if (( prompt_vcs_fd )) && { : <&$prompt_vcs_fd } 2>/dev/null; then
+ zle -F "$prompt_vcs_fd" 2> /dev/null
+ exec {prompt_vcs_fd}<&-
+ fi
+ if (( prompt_vcs_pid )); then
+ [[ -o monitor ]] &&
+ (( prompt_vcs_pid *= -1 ))
+ kill -KILL $prompt_vcs_pid 2> /dev/null
+ fi
+
+ # Asynchronously check for changes.
+ typeset -gH prompt_vcs_fd=
+ exec {prompt_vcs_fd}< <(
+ print -r -- $sysparams[pid]
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-changes yes
+ _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-staged-changes yes
+ _prompt_vcs_info
+ )
+ typeset -gH prompt_vcs_pid=
+ read -u $prompt_vcs_fd -- prompt_vcs_pid
+
+ # Add callback. Needs to be a widget, so we can refresh the prompt.
+ zle -Fw "$prompt_vcs_fd" prompt_vcs_fd-widget
+}
+
+# Callback widget for our async fetch of VCS changes
+zle -N prompt_vcs_fd-widget
+prompt_vcs_fd-widget() {
+ emulate -L zsh
+
+ (( $1 )) ||
+ return 64 # EX_USAGE; see man 3 sysexits
+
+ local -i fd=$1
+ local -a reply
+ {
+ zle -F "$fd" # Detach ourselves, so we don't get called more than once.
+
+ [[ -z $2 ]] ||
+ return 74 # EX_IOERR
+
+ IFS=$'\0' read -Aru "$fd"
+ typeset -gHa _prompt_vcs_info_msg_=( "${(@Q)reply}" )
+ PS1="$_prompt_vcs_info_msg_[1]"
+ RPS1="$_prompt_vcs_info_msg_[2]"
+ zle .reset-prompt
+ } always {
+ exec {fd}<&-
+ }
+}
+
+prompt_vcs_setup() {
+ prompt_opts=( cr percent sp ) # Tell promptinit which options to set.
+
+ # Indent left continuation prompt for each open shell construct.
+ local -a indent=( '%('{1..36}'_, ,)' )
+ PS2="${(j::)indent}"
+ RPS2='%F{yellow}# %^%f'
+
+ PS4=$'#->%(?,%F{green},%B%F{red}%S)%?%b%f%s\t%e+%F{green}%1N%f:%I'
+ PS4+=' %(1_,%F{yellow}%K{black}%_%f%k ,)'
+ SPROMPT='Correct %B%F{red}%S%R%b%f%s to %B%F{green}%r%b%f?'
+ SPROMPT+=$'\n%{\e[2m%}%Uy%ues %Un%uo %Ue%udit %Ua%ubort%b '
+ PROMPT_EOL_MARK='%F{cyan}%S%#%f%s'
+ zle_highlight=(
+ isearch:fg=black,bg=yellow
+ special:fg=cyan,bold
+ region:fg=white,bg=blue,bold
+ suffix:fg=white,bg=blue,bold
+ paste:bold
+ )
+
+ add-zsh-hook chpwd prompt_vcs_chpwd
+ add-zsh-hook precmd prompt_vcs_precmd
+
+ prompt_vcs_chpwd
+}
+
+prompt_vcs_setup "$@"
--
2.30.1 (Apple Git-130)
next prev parent reply other threads:[~2021-06-10 14:02 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-13 19:41 [RFC][PATCH] `newuser` prompt theme Marlon
2021-04-13 20:31 ` Roman Perepelitsa
2021-04-13 20:44 ` Bart Schaefer
2021-04-14 5:09 ` Marlon Richert
2021-04-14 5:17 ` Bart Schaefer
2021-04-14 5:45 ` Marlon
2021-04-14 12:05 ` Daniel Shahaf
2021-04-14 13:17 ` Marlon
2021-04-14 14:09 ` Daniel Shahaf
2021-04-15 1:07 ` Bart Schaefer
2021-04-15 3:50 ` Marlon Richert
2021-04-15 20:27 ` Daniel Shahaf
2021-04-15 23:42 ` Daniel Shahaf
2021-04-15 11:11 ` Mikael Magnusson
2021-04-15 16:44 ` vcs_info's global variables (was: Re: [RFC][PATCH] `newuser` prompt theme) Daniel Shahaf
2021-04-16 16:04 ` [RFC][PATCH] `newuser` prompt theme Marlon
2021-04-16 17:13 ` Daniel Shahaf
2021-04-16 19:19 ` Marlon Richert
2021-04-16 19:30 ` Daniel Shahaf
2021-04-19 16:46 ` Marlon Richert
2021-05-02 17:18 ` Lawrence Velázquez
2021-05-03 2:38 ` Bart Schaefer
2021-05-03 4:11 ` Lawrence Velázquez
2021-05-03 11:38 ` [PATCH] Add customizable `vcs` prompt theme (was Re: [RFC][PATCH] `newuser` prompt theme) Marlon Richert
2021-05-09 18:04 ` Lawrence Velázquez
2021-05-31 1:01 ` Lawrence Velázquez
2021-06-09 12:58 ` Marlon Richert
2021-06-09 13:22 ` Roman Perepelitsa
2021-06-09 18:06 ` Marlon Richert
2021-06-10 9:30 ` Roman Perepelitsa
2021-06-10 13:47 ` Marlon Richert
2021-06-10 14:01 ` Marlon Richert [this message]
2021-06-10 17:45 ` Roman Perepelitsa
2021-06-20 22:13 ` Marlon Richert
2021-06-23 17:26 ` Roman Perepelitsa
2022-08-08 10:12 ` Marlon Richert
2022-08-08 10:17 ` Roman Perepelitsa
2021-04-14 14:09 ` sysexits.h codes? (was: " Daniel Shahaf
2021-04-30 19:40 ` Marlon Richert
2021-04-30 21:16 ` Daniel Shahaf
2021-04-30 21:34 ` Bart Schaefer
2021-05-01 15:09 ` Daniel Shahaf
2021-04-30 21:40 ` Bart Schaefer
2021-05-01 13:39 ` Marlon Richert
2021-05-01 14:43 ` Daniel Shahaf
2021-05-03 11:36 ` Marlon Richert
2021-05-03 16:04 ` Daniel Shahaf
2021-05-04 11:13 ` Marlon Richert
2021-05-21 11:38 ` Marlon Richert
2021-04-14 15:30 ` [RFC][PATCH] `newuser` prompt theme Arseny Maslennikov
2021-04-14 18:52 ` Daniel Shahaf
2021-04-14 14:00 ` precmd hooks and $? Bart Schaefer
2021-04-14 15:18 ` docs patches for precmd hooks and $?, and vcs_info Daniel Shahaf
2021-04-15 2:35 ` Bart Schaefer
2021-04-15 16:17 ` Archives render attachments as first-class messages (was: docs patches for precmd hooks and $?, and vcs_info) Daniel Shahaf
2021-04-15 18:54 ` [RFC][PATCH] Reset ZLE hooks when changing prompt themes (was Re: [RFC][PATCH] `newuser` prompt theme) Marlon
2021-04-15 21:34 ` Daniel Shahaf
2021-04-16 22:34 ` Marlon Richert
2021-04-25 16:08 ` Lawrence Velázquez
2021-05-02 17:03 ` Lawrence Velázquez
2021-05-02 17:59 ` Bart Schaefer
2021-05-05 6:10 ` Marlon Richert
2021-05-16 15:27 ` Lawrence Velázquez
2021-05-16 19:31 ` Marlon Richert
2021-05-17 4:19 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAHLkEDvxfU93Fzk2aRw9-f+H+91HhYcYQ++v3rWnGqqv++VEpQ@mail.gmail.com \
--to=marlon.richert@gmail.com \
--cc=roman.perepelitsa@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).