zsh-users
 help / color / mirror / code / Atom feed
From: Dominik Vogt <dominik.vogt@gmx.de>
To: zsh-users@zsh.org
Subject: Re: Shortened bit branch in prompt
Date: Wed, 10 Feb 2021 16:40:43 +0100	[thread overview]
Message-ID: <20210210154043.GA2143@gmx.de> (raw)
In-Reply-To: <1216298849.3722151.1612961113500@mail2.virginmedia.com>

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


  reply	other threads:[~2021-02-10 15:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-10 12:05 Dominik Vogt
2021-02-10 12:45 ` Peter Stephenson
2021-02-10 15:40   ` Dominik Vogt [this message]
2021-02-11  1:43 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210210154043.GA2143@gmx.de \
    --to=dominik.vogt@gmx.de \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).