Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] Add customizable `vcs` prompt theme (was Re: [RFC][PATCH] `newuser` prompt theme)



On Mon, May 31, 2021 at 4:01 AM Lawrence Velázquez <larryv@xxxxxxx> wrote:
On Sun, May 9, 2021, at 2:04 PM, Lawrence Velázquez wrote:
> On Mon, May 3, 2021, at 7:38 AM, Marlon Richert wrote:
> > On Mon, May 3, 2021 at 7:11 AM Lawrence Velázquez <larryv@xxxxxxx> wrote:
> > >
> > > On Sun, May 2, 2021, at 10:38 PM, Bart Schaefer wrote:
> > > > On Sun, May 2, 2021 at 10:22 AM Lawrence Velázquez <larryv@xxxxxxx> wrote:
> > > > >
> > > > > ping for further review
> > > >
> > > > I've lost track of what we're reviewing here:  "prompt newuser" or
> > > > "prompt vcs" or ...?
> > >
> > > The patch in workers/48620, which as I understand it used to be
> > > "newuser" but is "vcs" now?  I think?
> >
> > Yes, that is correct, but it appears I forgot to update the subject
> > line to reflect this. That's fixed now.

I decided to make some simplifications to this prompt theme. Here's a new version of the patch.

From 78c0c003c022a457921d6dd54dcef211c565e783 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@xxxxxxxxx>
Date: Wed, 9 Jun 2021 15:53:47 +0300
Subject: [PATCH] Add customizable `vcs` prompt theme

---
 Functions/Prompts/prompt_vcs_setup | 194 +++++++++++++++++++++++++++++
 1 file changed, 194 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..4a17b3ff2
--- /dev/null
+++ b/Functions/Prompts/prompt_vcs_setup
@@ -0,0 +1,194 @@
+##
+# Prompt that can be customized through vcs_info
+#
+
+autoload -Uz add-zle-hook-widget add-zsh-hook vcs_info
+
+# Prompt segments
+readonly -gHA _prompt_vcs_defaults=(
+    start:chpwd     $'\n%B%F{blue}%~%%b%f/'
+    start:left      $'%%(?,%F{green},%F{red})%#%%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{yellow}# %^%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 the following three entries:
+  #   1st string is printed after changing dirs.
+  #   2nd string is the left prompt.
+  #   3rd string is the right prompt and updated asynchronously.
+  
+  # Normal prompt:
+  zstyle ':vcs_info:*:prompt_vcs:*' nvcsformats \\
+      "${(q+)_prompt_vcs_defaults[start:chpwd]//'%%'/%}" \\
+      "${(q+)_prompt_vcs_defaults[start:left]//'%%'/%}" \\
+      "${(q+)_prompt_vcs_defaults[start:right]//'%%'/%}"
+
+  # Prompt inside VCS repo:
+  zstyle ':vcs_info:*:prompt_vcs:*' formats \\
+      ${(q+)_prompt_vcs_defaults[start:chpwd]} \\
+      ${(q+)_prompt_vcs_defaults[start:left]} \\
+      ${(q+):-%u%c$_prompt_vcs_defaults[start:branch]$_prompt_vcs_defaults[start:repo]}
+  
+  # Prompt during an ongoing VCS action:
+  zstyle ':vcs_info:*:prompt_vcs:*' actionformats \\
+      ${(q+)_prompt_vcs_defaults[start:chpwd]} \\
+      ${(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:right]//'%%'/%}"
+  _prompt_vcs_zstyle ':vcs_info:*:prompt_vcs:*' formats \
+      "$_prompt_vcs_defaults[start:chpwd]" \
+      "$_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]" \
+      "%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}<&-
+
+  print -Pr -- "$_prompt_vcs_info_msg_[1]"
+  PS1="$_prompt_vcs_info_msg_[2]"
+  RPS1="$_prompt_vcs_info_msg_[3]"
+}
+
+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-widget
+    ;;
+    ( 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-widget() {
+  emulate -L zsh
+
+  [[ -n $1 ]] || 
+      return  # Don't accidentally close fd 0.
+  {
+    local -i fd=$1
+    zle -F "$fd"  # Detach ourselves, so we don't get called more than once.
+
+    [[ -z $2 ]] ||
+        return  # Error occured.
+
+    local -a reply
+    IFS=$'\0' read -Aru "$fd"
+  } always {
+    exec {fd}<&-
+  }
+  [[ $reply[-1] == $PWD ]] ||
+      return  # Abort if the info is not for the current dir.
+      
+  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\t%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:fg=white,bg=blue,bold
+      suffix:fg=white,bg=blue,bold
+      paste:bold
+  )
+
+  add-zsh-hook chpwd prompt_vcs_chpwd
+  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-widget # Callback widget for async VCS update.
+
+  prompt_vcs_chpwd
+}
+
+prompt_vcs_setup "$@"
-- 
2.30.1 (Apple Git-130)



Messages sorted by: Reverse Date, Date, Thread, Author