zsh-workers
 help / color / mirror / code / Atom feed
From: Marlon Richert <marlon.richert@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [RFC][PATCH] `newuser` prompt theme
Date: Mon, 19 Apr 2021 19:46:54 +0300	[thread overview]
Message-ID: <E68B37FB-ADEC-44DF-9E0F-E550180DFC41@gmail.com> (raw)
In-Reply-To: <20210416193037.GD15670@tarpaulin.shahaf.local2>

[-- Attachment #1: Type: text/plain, Size: 485 bytes --]

On 16 Apr 2021, at 22:30, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> 
>> +prompt_vcs_chpwd() {
>> +  exec {fd}< <(
>> +    # Fetch only staged changes at this point, for performance reasons.
>> +    _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-staged-changes yes
> 
> Should this set «check-for-changes no», to override a «yes» that may
> have been explicitly set on a less-specific context pattern?

Yes, that would make sense. Added. See attachment.


[-- Attachment #2: 0001-Add-customizable-vcs-prompt-theme.txt --]
[-- Type: text/plain, Size: 8421 bytes --]

From 12867d2439f3c3f550190438b8b7cb53441bfc42 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Mon, 19 Apr 2021 19:43:16 +0300
Subject: [PATCH] Add customizable `vcs` prompt theme

---
 Functions/Prompts/prompt_vcs_setup | 228 +++++++++++++++++++++++++++++
 1 file changed, 228 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..1ab73da2b
--- /dev/null
+++ b/Functions/Prompts/prompt_vcs_setup
@@ -0,0 +1,228 @@
+##
+# Prompt that can be customized through vcs_info
+#
+
+autoload -Uz add-zle-hook-widget add-zsh-hook vcs_info
+
+# Standardized exit codes. See `man 3 sysexits`.
+if [[ ${(t)sysexits} != *readonly* ]]; then
+  # Avoid error in case this has been set already.
+  readonly -ga sysexits=(
+      USAGE
+      DATAERR
+      NOINPUT
+      NOUSER
+      NOHOST
+      UNAVAILABLE
+      SOFTWARE
+      OSERR
+      OSFILE
+      CANTCREAT
+      IOERR
+      TEMPFAIL
+      PROTOCOL
+      NOPERM
+      CONFIG
+  )
+fi
+
+# Prompt segments
+readonly -gHA _prompt_vcs_defaults=(
+    start:chpwd     $'\n%B%F{blue}%~%%b%f/\n'
+    start:left      $'%%(?,%F{green},%F{red}%v%k\n%B%%S)%#%%b%f%%s '
+    start:right     '%B%F{blue}%n%b%f%k@%F{magenta}%m%f'
+    start:staged    '%B%F{green}+%b%f'
+    start:unstaged  '%B%F{red}*%b%f'
+    start:action    '%B%F{red}%a%%b%f'
+    start:branch    '%B%F{cyan}%b%%b%f'
+    start:repo      '|%B%F{blue}%r%%b%f'
+    cont:indent     '  '
+    cont:left       ''
+    cont:right      '%F{red}%^%f'
+)
+
+prompt_vcs_help() {
+  print -r -- \
+"This prompt theme can by customized by copy-pasting any of the code below to
+your .zshrc file and editing it there:'
+
+  # For each of these,
+  # * the first string is used for \$PS1 after changing dirs,
+  # * the second string is used for \$PS1 otherwise, and
+  # * the third string is used for \$RPS1 and updated asynchronously.
+  # In these, %v expands to the name (if any) & number of the last exit code.
+  zstyle ':vcs_info:*:prompt_vcs:*' nvcsformats \
+      "${${:-$_prompt_vcs_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]}//'%%'/%}" \
+      "${_prompt_vcs_defaults[start:left]//'%%'/%}" \
+      "${_prompt_vcs_defaults[start:right]//'%%'/%}"
+  zstyle ':vcs_info:*:prompt_vcs:*' formats \\
+      ${(q+):-$_prompt_vcs_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]} \\
+      ${(q+)_prompt_vcs_defaults[start:left]} \\
+      ${(q+):-%u%c$_prompt_vcs_defaults[start:branch]$_prompt_vcs_defaults[start:repo]}
+  zstyle ':vcs_info:*:prompt_vcs:*' actionformats \\
+      ${(q+):-$_prompt_vcs_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]} \\
+      ${(q+)_prompt_vcs_defaults[start:left]} \\
+      ${(q+):-%u%c$_prompt_vcs_defaults[start:action]$_prompt_vcs_defaults[start:repo]}
+
+  # These set the values of %c and %u, respectively:
+  zstyle ':vcs_info:*:prompt_vcs:*' stagedstr ${(q+)_prompt_vcs_defaults[start:staged]}
+  zstyle ':vcs_info:*:prompt_vcs:*' unstagedstr ${(q+)_prompt_vcs_defaults[start:unstaged]}
+
+For more info on the config 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_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]}//'%%'/%}" \
+      "${_prompt_vcs_defaults[start:left]//'%%'/%}" \
+      "${_prompt_vcs_defaults[start:right]//'%%'/%}"
+  _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' formats \
+      "$_prompt_vcs_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]" \
+      "$_prompt_vcs_defaults[start:left]" \
+      "%u%c$_prompt_vcs_defaults[start:branch]$_prompt_vcs_defaults[start:repo]"
+  _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' actionformats \
+      "$_prompt_vcs_defaults[start:chpwd]$_prompt_vcs_defaults[start:left]" \
+      "$_prompt_vcs_defaults[start:left]" \
+      "%u%c$_prompt_vcs_defaults[start:action]$_prompt_vcs_defaults[start:repo]"
+  _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' stagedstr \
+      "$_prompt_vcs_defaults[start:staged]"
+  _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' unstagedstr \
+      "$_prompt_vcs_defaults[start: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
+
+  local -i fd=-1; local -a reply
+  exec {fd}< <(
+    # Fetch only staged changes at this point, for performance reasons.
+    _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-changes no
+    _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-staged-changes yes
+    _prompt_vcs_info
+  )
+  IFS=$'\0' read -Aru "$fd"
+  typeset -ga _prompt_vcs_info_msg_=( "${(@Q)reply}" )
+  exec {fd}<&-
+
+  PS1="$_prompt_vcs_info_msg_[1]"
+  RPS1="$_prompt_vcs_info_msg_[3]"
+}
+
+prompt_vcs_precmd() {
+  local exitstatus=$? # Exit status of last command
+  emulate -L zsh
+
+  # Assign human-friendly exit status string to %v.
+  psvar[1]=
+  case $exitstatus in
+    ( <128-> )
+      psvar[1]="SIG$signals[exitstatus-127] "
+      ;|
+    ( <64-78> )
+      psvar[1]="EX_$sysexits[exitstatus-63] "
+      ;|
+    ( <1-> )
+      psvar[1]+="($exitstatus)"
+      ;;
+    # No need to show EX_OK.
+  esac
+}
+
+prompt_vcs_line-init() {
+  emulate -L zsh
+
+  case $CONTEXT in
+    start ) # (R)PS1
+      # Asynchronously check for unstaged changes. Do this here & not in
+      # precmd, so you can press Enter on an empty line to update VCS info.
+      local -i fd=-1
+      exec {fd}< <(
+        _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' check-for-changes yes
+        _prompt_vcs_info
+        print -r -- "$PWD"
+      )
+
+      # Add callback. Needs to be a widget, so we can refresh the prompt.
+      zle -Fw "$fd" prompt_vcs_fd-handler
+      ;;
+    cont )  # (R)PS2
+      # Indent left continuation prompt for each open shell construct.
+      local fmt="$_prompt_vcs_defaults[cont:indent]"
+      local -a indent=( '%('{1..$(( COLUMNS / ${(m)#fmt} ))}"_,$fmt,)" )
+      PS2="${(j::)indent}$_prompt_vcs_defaults[cont:left]"
+
+      RPS2="$_prompt_vcs_defaults[cont:right]"
+      ;;
+  esac
+}
+
+# Callback widget function for our async fetch of unstaged VCS changes
+prompt_vcs_fd-handler() {
+  emulate -L zsh
+
+  local -i fd=$1; local sig=$2; local -a reply
+  {
+    zle -F "$fd"  # Detach ourselves, so we don't get called more than once.
+
+    [[ $sig != (|hup) ]] &&
+        return  # Error occured
+
+    IFS=$'\0' read -Aru "$fd"
+
+    [[ $reply[-1] != $PWD ]] &&
+        return  # Abort if the info is not for the current dir.
+  } always {
+    exec {fd}<&-
+  }
+  shift -p reply
+  typeset -ga _prompt_vcs_info_msg_=( "${(@Q)reply}" )
+  RPS1="$_prompt_vcs_info_msg_[3]"
+  zle .reset-prompt
+}
+
+prompt_vcs_line-finish() {
+  emulate -L zsh
+  PS1="$_prompt_vcs_info_msg_[2]"
+}
+
+prompt_vcs_setup() {
+  prompt_opts=( cr percent sp ) # Tell promptinit which options to set.
+
+  PS4=$'# ?=%(?,%F{green},%B%F{red}%S)%?%b%f%s e=%e %F{green}%1N%f:%I %(1_,%F{yellow}%K{black}%_%f%k ,)'
+  SPROMPT='Correct %B%F{red}%U%R%b%f%u to %B%F{green}%r%b%f? [%Sy%ses|%Sn%so|%Se%sdit|%Sa%sbort] '
+  PROMPT_EOL_MARK='%S%F{cyan}%#%s%f'
+  zle_highlight=(
+      isearch:fg=black,bg=yellow
+      special:fg=cyan,bold
+      region:bg=blue
+      suffix:bg=blue
+      paste:none
+  )
+
+  add-zsh-hook chpwd    prompt_vcs_chpwd
+  add-zsh-hook precmd   prompt_vcs_precmd
+  add-zle-hook-widget line-init   prompt_vcs_line-init
+  add-zle-hook-widget line-finish prompt_vcs_line-finish
+  zle -N prompt_vcs_fd-handler # Callback widget for async VCS update.
+
+  prompt_vcs_chpwd
+}
+
+prompt_vcs_setup "$@"
-- 
2.31.1


[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




  reply	other threads:[~2021-04-19 16:47 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-13 19:41 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 [this message]
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
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=E68B37FB-ADEC-44DF-9E0F-E550180DFC41@gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=d.s@daniel.shahaf.name \
    --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).