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

Re: PATCH: allow configuration of wrap length and number of path elements shown for adam1 prompt



Oops -- I just noticed that I accidentally included two nearly-identical commits/patches; I wanted to fix up a bit of the commit message and remove an errant $base_prompt; sadly, neither commit is 100% correct!

In the commit message, I missed using the generic user@host and instead now everyone on the internet knows that I have a machine named "strawberry". :)

In the code, the line for the prompt when not wrapping should (1) not have an extra $base_prompt, and (2) add 1 for the first %( and not subtract one for the second %(. You'll want this:

    path_prompt="%B%F{$prompt_adam1_color2}%($((${prompt_path_ellipsize_num} + 1))~|...|)%${prompt_path_ellipsize_num}~%F{white}"

Sorry for any confusion, and thanks for considering this patch!

Dan


--
Ceci n'est pas une .signature.

On Sunday, June 22nd, 2025 at 09:58, Dan Drake <dan@xxxxxxxxxxxx> wrote:

Hello zsh-workers,

I'm using the adam1 prompt, but didn't quite like how it wrapped to a second line, and how it would only show at most 3 path elements before using "...". The patch below allows users to configure those values, just like the colors, instead of using the hard-coded values of 40 and 3.

The commit message, I hope, explains the logic for the change and the new behavior. I'm not subscribed to this list, so please include me in any replies.




commit 0dc97f6b992cdb15cd8b95346665263a2c032f06
Author: Dan Drake <zsh@xxxxxxxxxxxx>
Date:   Sun Jun 22 08:50:08 2025 -0500

    make adam1 prompt wrap len, ellipsized path elements configurable

    The adam1 prompt will wrap to the next line when the current path to be
    shown is too long. The logic right now uses a hard-coded value of 40 to
    decide when to wrap:

    ```
    if [[ $prompt_length -lt 40 ]]; then
      # show prompt with no newline
    else
      # show prompt with a newline
    fi
    ```

    The particular string used to find the length is $base_prompt_etc; that
    string uses a $( ternary _expression_ with ~ for the path; that _expression_
    uses hard-coded values so that when the path has more than 3 elements,
    the leading elements are ellipsized, and only the last 3 elements are
    shown:

    ```
    base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
    ```

    Now, the length at which the prompt wraps, as well as the maximum number
    of path elements shown before ellipsizing, are configurable, just like
    the colors. They default to the previous values of 40 and 3.

    Users may set those values just like they set the colors: for example,

    ```
    prompt adam1 gray blue blue 70 7
    ```

    will only wrap when the length of the prompt string exceeds 80, and will
    ellipsize the path when there are more than 7 elements in the path. As a
    concrete example:

    ```
    user@host /tmp % mkdir -p 2/3/4/5/6/7/8/9/10/11
    user@host /tmp % cd 2/3/4/5/6/7
    user@host /tmp/2/3/4/5/6/7 % cd 8
    user@host ...2/3/4/5/6/7/8 %
    ```

    And for the wrapping:

    ```
    dan@strawberry /tmp % mkdir long_name_to_show_adam1_prompt_wrap
    dan@strawberry /tmp % cd long_name_to_show_adam1_prompt_wrap
    dan@strawberry /tmp/long_name_to_show_adam1_prompt_wrap % mkdir -p 1/2/3/4/5/6/7/8
    dan@strawberry /tmp/long_name_to_show_adam1_prompt_wrap % cd 1
    dan@strawberry /tmp/long_name_to_show_adam1_prompt_wrap/1 % cd 2
    dan@strawberry /tmp/long_name_to_show_adam1_prompt_wrap/1/2 % cd 3
    dan@strawberry /tmp/long_name_to_show_adam1_prompt_wrap/1/2/3
     %
    ```

    The length calculation does not exactly match the expected value of 70;
    I suspect the string length is including the color escape sequences, but
    this patch does allow users to configure the wrap length and number of
    ancestor directories shown before using an ellipsis.

diff --git a/Functions/Prompts/prompt_adam1_setup b/Functions/Prompts/prompt_adam1_setup
index 27a613ca1..8be92c8f1 100644
--- a/Functions/Prompts/prompt_adam1_setup
+++ b/Functions/Prompts/prompt_adam1_setup
@@ -22,6 +22,8 @@ prompt_adam1_setup () {
   prompt_adam1_color1=${1:-'blue'}
   prompt_adam1_color2=${2:-'cyan'}
   prompt_adam1_color3=${3:-'green'}
+  prompt_wrap_length=${4:-40}
+  prompt_path_ellipsize_num=${5:-4}

   base_prompt="%K{$prompt_adam1_color1}%n@%m%k "
   post_prompt="%b%f%k"
@@ -39,10 +41,10 @@ prompt_adam1_precmd () {
   local prompt_length space_left

   base_prompt_expanded_no_color=$(print -P "$base_prompt_no_color")
-  base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
+  base_prompt_etc=$(print -P "$base_prompt%(${prompt_path_ellipsize_num}~|...|)%$(( $prompt_path_ellipsize_num - 1))~")
   prompt_length=${#base_prompt_etc}
-  if [[ $prompt_length -lt 40 ]]; then
-    path_prompt="%B%F{$prompt_adam1_color2}%(4~|...|)%3~%F{white}"
+  if [[ $prompt_length -lt $prompt_wrap_length ]]; then
+    path_prompt="%B%F{$prompt_adam1_color2}%(${prompt_path_ellipsize_num}~|...|)%$(( $prompt_path_ellipsize_num - 1))~%F{white}"
   else
     space_left=$(( $COLUMNS - $#base_prompt_expanded_no_color - 2 ))
     path_prompt="%B%F{$prompt_adam1_color3}%${space_left}<...<%~$prompt_newline%F{white}"
commit 8c5bb95e86ab3427ff9bbed485b1723ffb17b978
Author: Dan Drake <dan@xxxxxxxxxxxx>
Date:   Sun Jun 22 08:50:08 2025 -0500

    make adam1 prompt wrap len, ellipsized path elements configurable
   
    The adam1 prompt will wrap to the next line when the current path to be
    shown is too long. The logic right now uses a hard-coded value of 40 to
    decide when to wrap:
   
    ```
    if [[ $prompt_length -lt 40 ]]; then
      # show prompt with no newline
    else
      # show prompt with a newline
    fi
    ```
   
    The particular string used to find the length is $base_prompt_etc; that
    string uses a $( ternary _expression_ with ~ for the path; that _expression_
    uses hard-coded values so that when the path has more than 3 elements,
    the leading elements are ellipsized, and only the last 3 elements are
    shown:
   
    ```
    base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
    ```
   
    Now, the length at which the prompt wraps, as well as the maximum number
    of path elements shown before ellipsizing, are configurable, just like
    the colors. They default to the previous values of 40 and 3.
   
    Users may set those values just like they set the colors: for example,
   
    ```
    prompt adam1 gray blue blue 70 7
    ```
   
    will only wrap when the length of the prompt string exceeds 80, and will
    ellipsize the path when there are more than 7 elements in the path. As a
    concrete example:
   
    ```
    user@host /tmp % mkdir -p 2/3/4/5/6/7/8/9/10/11
    user@host /tmp % cd 2/3/4/5/6/7
    user@host /tmp/2/3/4/5/6/7 % cd 8
    user@host ...2/3/4/5/6/7/8 %
    ```
   
    And for the wrapping:
   
    ```
    user@host /tmp % mkdir long_name_to_show_adam1_prompt_wrap
    user@host /tmp % cd long_name_to_show_adam1_prompt_wrap
    user@host /tmp/long_name_to_show_adam1_prompt_wrap % mkdir -p 1/2/3/4/5/6/7/8
    user@host /tmp/long_name_to_show_adam1_prompt_wrap % cd 1
    user@host /tmp/long_name_to_show_adam1_prompt_wrap/1 % cd 2
    user@host /tmp/long_name_to_show_adam1_prompt_wrap/1/2 % cd 3
    user@host /tmp/long_name_to_show_adam1_prompt_wrap/1/2/3
     %
    ```
   
    The length calculation does not exactly match the expected value of 70;
    I suspect the string length is including the color escape sequences, but
    this patch does allow users to configure the wrap length and number of
    ancestor directories shown before using an ellipsis.

diff --git a/Functions/Prompts/prompt_adam1_setup b/Functions/Prompts/prompt_adam1_setup
index 27a613ca1..b4b91285b 100644
--- a/Functions/Prompts/prompt_adam1_setup
+++ b/Functions/Prompts/prompt_adam1_setup
@@ -22,6 +22,8 @@ prompt_adam1_setup () {
   prompt_adam1_color1=${1:-'blue'}
   prompt_adam1_color2=${2:-'cyan'}
   prompt_adam1_color3=${3:-'green'}
+  prompt_wrap_length=${4:-40}
+  prompt_path_ellipsize_num=${5:-3}
 
   base_prompt="%K{$prompt_adam1_color1}%n@%m%k "
   post_prompt="%b%f%k"
@@ -39,10 +41,10 @@ prompt_adam1_precmd () {
   local prompt_length space_left
 
   base_prompt_expanded_no_color=$(print -P "$base_prompt_no_color")
-  base_prompt_etc=$(print -P "$base_prompt%(4~|...|)%3~")
+  base_prompt_etc=$(print -P "$base_prompt%($((${prompt_path_ellipsize_num} + 1))~|...|)%${prompt_path_ellipsize_num}~")
   prompt_length=${#base_prompt_etc}
-  if [[ $prompt_length -lt 40 ]]; then
-    path_prompt="%B%F{$prompt_adam1_color2}%(4~|...|)%3~%F{white}"
+  if [[ $prompt_length -lt $prompt_wrap_length ]]; then
+    path_prompt="%B%F{$prompt_adam1_color2}$base_prompt%($((${prompt_path_ellipsize_num} + 1))~|...|)%${prompt_path_ellipsize_num}~%F{white}"
   else
     space_left=$(( $COLUMNS - $#base_prompt_expanded_no_color - 2 ))
     path_prompt="%B%F{$prompt_adam1_color3}%${space_left}<...<%~$prompt_newline%F{white}"






--
Ceci n'est pas une .signature.



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