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

Re: Shortened bit branch in prompt



> On 10 February 2021 at 12:05 Dominik Vogt <dominik.vogt@xxxxxx> wrote:
> I want to change that so
> 
>  * If the branch name is max. 25 characters, print it umodified.
>  * If the branch ame is loger, print
> 
>      <first 17 characters>...<last five characters>
> 
>    E.g. if the branch name is abcdefghijklmnopqrstuvwxyz0123456789,
>    the prompt should have
> 
>      abcdefghijklmnopq...56789
> 
> Of course this should be done with zsh functionality only.

It's possible to be completely general about printing a variable substitution
based on the length, in this case of variable "foo":

print ${${${${${:-$(( ${#foo} > 25 ))}##1}:-Printed if long}##0}:-Printed if short}

Those arbitrary chunks are subject to further expansion, so put the expressions
you need there --- in particular, $foo, ${foo[1.17]}...${foo[-5,-1]}, I haven't
put those in myself just for the sake of clarity of what's going on.

There are probably ways of shortening this, but by the standards of the squiggle
factory that's arguably quite readable.  (Only arguably.)

pws




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