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

PATCH, take 2: allow configuration of wrap length and number of path elements shown for adam1 prompt



This supersedes workers/53800 -- it's the correct version of the (two..) patches there.


commit 4e711d53e814bef5e1697b17bed89e2411a5061e
Author: Dan Drake <zsh@xxxxxxxxxxxx>
Date:   Sun Jun 22 12:55:10 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 70, 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}"

diff --git a/Functions/Prompts/prompt_adam1_setup b/Functions/Prompts/prompt_adam1_setup
index 27a613ca1..b88ffc7c1 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} + 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}%($((${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