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

Re: Shortened bit branch in prompt



On Wed, Feb 10, 2021 at 12:45:13PM +0000, Peter Stephenson wrote:
> > 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.

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




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