zsh-users
 help / color / mirror / Atom feed
* Shortened bit branch in prompt
@ 2021-02-10 12:05 Dominik Vogt
  2021-02-10 12:45 ` Peter Stephenson
  2021-02-11  1:43 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Dominik Vogt @ 2021-02-10 12:05 UTC (permalink / raw)
  To: Zsh Users

I have this function to put the name of the current git branch in
the shell prompt if the working directory is inside a git tree:

--
__git_ps1 () {
	local b="$(
		git symbolic-ref -q HEAD 2> /dev/null ||
		{ [[ $? == 1 ]] && printf '*none*'} )"
	if [[ -n "$b" ]]
	then
		printf " (%s)" "${${b##refs/heads/}:0:25}"  <---------------
	fi
}
--

Since a customer uses absurdly long branch names, it uses only the
first 25 characters of the name.

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.

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Shortened bit branch in prompt
  2021-02-10 12:05 Shortened bit branch in prompt Dominik Vogt
@ 2021-02-10 12:45 ` Peter Stephenson
  2021-02-10 15:40   ` Dominik Vogt
  2021-02-11  1:43 ` Bart Schaefer
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2021-02-10 12:45 UTC (permalink / raw)
  To: Zsh Users

> On 10 February 2021 at 12:05 Dominik Vogt <dominik.vogt@gmx.de> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Shortened bit branch in prompt
  2021-02-10 12:45 ` Peter Stephenson
@ 2021-02-10 15:40   ` Dominik Vogt
  0 siblings, 0 replies; 4+ messages in thread
From: Dominik Vogt @ 2021-02-10 15:40 UTC (permalink / raw)
  To: zsh-users

On Wed, Feb 10, 2021 at 12:45:13PM +0000, Peter Stephenson wrote:
> > On 10 February 2021 at 12:05 Dominik Vogt <dominik.vogt@gmx.de> 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.

Okay, that's past my abilities of reading expansions ...  I've
done it with a bit of builtin scripting instead, using the $b[n,m]
syntax that I didn't know yet.

__git_ps1 () {
	local b

	b="$(git symbolic-ref -q HEAD 2> /dev/null)"
	case "$?" in
		128) return ;;
		0) ;;
		*) b='*none*' ;;
	esac
	b="${b##refs/heads/}"
	test ${#b} -gt 25 && b="$b[1,17]...$b[-5,-1]"
	test ${#b} -gt 0 && printf " (%s)" "$b"
}

Thanks for the help!

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Shortened bit branch in prompt
  2021-02-10 12:05 Shortened bit branch in prompt Dominik Vogt
  2021-02-10 12:45 ` Peter Stephenson
@ 2021-02-11  1:43 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2021-02-11  1:43 UTC (permalink / raw)
  To: dominik.vogt, Zsh Users

On Wed, Feb 10, 2021 at 4:05 AM Dominik Vogt <dominik.vogt@gmx.de> wrote:
>
>  * If the branch name is max. 25 characters, print it umodified.
>  * If the branch ame is loger, print
>
>      <first 17 characters>...<last five characters>

Could you do this using the %> and %< prompt expansion escapes?

x=abcdefghijklmnopqrstuvwxyz0123456789
print -r -- ${(%):-%20>...>$x%>>%5<<$x%<<}

That still needs a conditional to determine (( $#x > 25 )).  Perhaps then:

setopt extendedglob
pat="(${(l:20::?:):-})?*(${(l:5::?:):-})"
print -r -- ${x/(#b)${~pat}/${match[1]}...${match[2]}}

I don't recall whether ${(*)...} to temporarily turn on extended
globbing is very common in the wild, yet.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-02-11  1:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-10 12:05 Shortened bit branch in prompt Dominik Vogt
2021-02-10 12:45 ` Peter Stephenson
2021-02-10 15:40   ` Dominik Vogt
2021-02-11  1:43 ` Bart Schaefer

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git