zsh-users
 help / color / mirror / code / Atom feed
* Length of %? in prompt
@ 2002-02-26 16:05 Ian Lynagh
  2002-02-26 16:48 ` Peter Stephenson
  2002-02-26 17:10 ` Bart Schaefer
  0 siblings, 2 replies; 12+ messages in thread
From: Ian Lynagh @ 2002-02-26 16:05 UTC (permalink / raw)
  To: zsh-users


Hi all

If the length of %? will alter the number of dashes following it in my
prompt am I right in thinking that my only course of action is to
enumerate the (rather large!) set of possible cases with
"%(n?,---...,)"? If so I guess I will have to redesign  :-(


Thanks
Ian


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

* Re: Length of %? in prompt
  2002-02-26 16:05 Length of %? in prompt Ian Lynagh
@ 2002-02-26 16:48 ` Peter Stephenson
  2002-02-26 17:10 ` Bart Schaefer
  1 sibling, 0 replies; 12+ messages in thread
From: Peter Stephenson @ 2002-02-26 16:48 UTC (permalink / raw)
  To: Zsh users list

Ian Lynagh wrote:
> If the length of %? will alter the number of dashes following it in my
> prompt am I right in thinking that my only course of action is to
> enumerate the (rather large!) set of possible cases with
> "%(n?,---...,)"? If so I guess I will have to redesign  :-(

I haven't understood quite what you're trying to do, but you might be
able to use truncation.

  "%10>>anything%>>something else"

will limit `anything', whatever it expands to (it can include arbitrary
prompt escapes but also any other text), to 10 characters, truncating on
ther right.  Truncation is then turned off so that `something else' will
be output in full.  So to keep the length the same you can arrange for
`anything' to end with an arbitrarily large string of characters which
will be pruned to the right size.

This assumes zsh 4.  The truncation in older versions was less flexible
--- only the replacement text for prompt escapes was truncated.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 392070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: Length of %? in prompt
  2002-02-26 16:05 Length of %? in prompt Ian Lynagh
  2002-02-26 16:48 ` Peter Stephenson
@ 2002-02-26 17:10 ` Bart Schaefer
  2002-02-26 18:08   ` Ian Lynagh
  1 sibling, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2002-02-26 17:10 UTC (permalink / raw)
  To: Ian Lynagh, zsh-users

On Feb 26,  4:05pm, Ian Lynagh wrote:
}
} If the length of %? will alter the number of dashes following it in my
} prompt am I right in thinking that my only course of action is to
} enumerate the (rather large!) set of possible cases with
} "%(n?,---...,)"? If so I guess I will have to redesign  :-(

No, you don't need to do that.  You can use the truncation sequences %>>
and/or %<<.  Let's assume that you want the value of %? plus the hyphens
to take up a total of 10 characters; you'd use:

    %10>-->%?--------%<<

The number of hyphens following %? must be at least 9 in order for the
field to always be 10 characters wide -- truncation won't lengthen the
prompt, only shorten it.

An important thing to note is that %N>STRING> means that STRING replaces
the last strlen(STRING) characters of the remaining prompt, up to %<<,
any time it is more than N characters long.  E.g., with %N>--> you can't
make N less than 5 without digits from %? being truncated when the exit
status is more than two digits long.

So the minimal fixed-width replacement for %? using this scheme is:

    %4>->%?---%<<

which works out to up to three digits of %? followed by at least 1 hyphen.

You can put the hyphens in front, too, of course:

    %4<-<---%?%>>

It's not necessary to reverse the > and < at the beginning and end of the
truncation region, I just do it for visual symmetry.  These work too:

    %4>->%?---%>>
    %4<-<---%?%<<

Finally, you can even do this:

    %(?.----.%4>->%?---)

Here, the truncation ends at the closing paren.  This example does not
show the exit status unless it is nonzero.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: Length of %? in prompt
  2002-02-26 17:10 ` Bart Schaefer
@ 2002-02-26 18:08   ` Ian Lynagh
  2002-02-26 18:26     ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Ian Lynagh @ 2002-02-26 18:08 UTC (permalink / raw)
  To: zsh-users

On Tue, Feb 26, 2002 at 05:10:45PM +0000, Bart Schaefer wrote:
> On Feb 26,  4:05pm, Ian Lynagh wrote:
> }
> } If the length of %? will alter the number of dashes following it in my
> } prompt am I right in thinking that my only course of action is to
> } enumerate the (rather large!) set of possible cases with
> } "%(n?,---...,)"? If so I guess I will have to redesign  :-(
> 
> No, you don't need to do that.  You can use the truncation sequences %>>
> and/or %<<.  Let's assume that you want the value of %? plus the hyphens
> to take up a total of 10 characters; you'd use:
> 
>     %10>-->%?--------%<<

Oh, of course! In fact, if I am lucky this will make the whole thing a
bit simpler.

I don't understand this 0 case though - is it a bug?

ian@majestica:~% export PS1="%10>-->0--------%<< " 
0-------- export PS1="%10>-->10--------%<< " 
10-------- export PS1="%10>-->100--------%<< "
100------- export PS1="%10>-->1000--------%<< "
1000------ echo $ZSH_VERSION
4.0.4


Thanks
Ian


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

* Re: Length of %? in prompt
  2002-02-26 18:08   ` Ian Lynagh
@ 2002-02-26 18:26     ` Bart Schaefer
  2002-02-26 18:42       ` Ian Lynagh
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2002-02-26 18:26 UTC (permalink / raw)
  To: Ian Lynagh; +Cc: zsh-users

On Tue, 26 Feb 2002, Ian Lynagh wrote:

> I don't understand this 0 case though - is it a bug?
>
> ian@majestica:~% export PS1="%10>-->0--------%<< "
                                      123456789

There are only a total of 9 characters there, so no truncation is needed.
You need one more hyphen after the zero to get 10 characters.


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

* Re: Length of %? in prompt
  2002-02-26 18:26     ` Bart Schaefer
@ 2002-02-26 18:42       ` Ian Lynagh
  2002-02-26 19:21         ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Ian Lynagh @ 2002-02-26 18:42 UTC (permalink / raw)
  To: zsh-users

On Tue, Feb 26, 2002 at 10:26:09AM -0800, Bart Schaefer wrote:
> On Tue, 26 Feb 2002, Ian Lynagh wrote:
> 
> > I don't understand this 0 case though - is it a bug?
> >
> > ian@majestica:~% export PS1="%10>-->0--------%<< "
>                                       123456789
> 
> There are only a total of 9 characters there, so no truncation is needed.
> You need one more hyphen after the zero to get 10 characters.

Whooops.

One more thing...as the first % escapes the second in
"%%1v>-->%?---------%<< " it looks like there is no way to change this
value on the fly without recreating the prompt - is this correct?


Thanks again
Ian


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

* Re: Length of %? in prompt
  2002-02-26 18:42       ` Ian Lynagh
@ 2002-02-26 19:21         ` Bart Schaefer
  2002-02-28 15:34           ` Ian Lynagh
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2002-02-26 19:21 UTC (permalink / raw)
  To: Ian Lynagh; +Cc: zsh-users

On Tue, 26 Feb 2002, Ian Lynagh wrote:

> One more thing...as the first % escapes the second in
> "%%1v>-->%?---------%<< " it looks like there is no way to change this
> value on the fly without recreating the prompt - is this correct?

If you "setopt promptsubst" you can use a variable for the width:

setopt promptsubst
W=5
PS1='%$W>-->%?----------%<< '

You just have to make sure there are as many hyphens in the truncate
region as the largest value you ever expect to assign to W.

If for some other reason you want to avoid promptsubst, you will have to
recreate the prompt, or at least recreate one of the psvar values like
this:

W=5
PS1='%1v '
precmd {
  psvar[1]=${(%%):-%$W>-->%?----------%<<}
}

Just be sure that the prompt expansion of %? is the first thing in precmd,
or else it'll get the exit status of whatever other commands appear in
precmd rather than the exit status of the last interactive command.


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

* Re: Length of %? in prompt
  2002-02-26 19:21         ` Bart Schaefer
@ 2002-02-28 15:34           ` Ian Lynagh
  2002-02-28 16:45             ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Ian Lynagh @ 2002-02-28 15:34 UTC (permalink / raw)
  To: zsh-users

On Tue, Feb 26, 2002 at 11:21:19AM -0800, Bart Schaefer wrote:
> On Tue, 26 Feb 2002, Ian Lynagh wrote:
> 
> > One more thing...as the first % escapes the second in
> > "%%1v>-->%?---------%<< " it looks like there is no way to change this
> > value on the fly without recreating the prompt - is this correct?
> 
> If you "setopt promptsubst" you can use a variable for the width:

I was trying to avoid this in favour of psvar, but I think it might be
the best way to do this.

I have just a couple more problems. Firstly I can't find a way to have
ANSI escape sequences actually executed when they are passed through
psvar - is this possible?

And secondly with
    "%\$((\$COLUMNS-2))>%{%}k>"
the %{%} appear in the prompt and both
    "%\$((\$COLUMNS-2))>${(%%):-%{$fg_bold[cyan]%\}}k>"
and "%\$((\$COLUMNS-2))>\${(%%):-%{$fg_bold[cyan]%\}}k>"
seem to be counting the length of the ANSI escape sequence in the length
calculation - am I missing something?


Thanks
Ian, who's slowly getting there  :-)


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

* Re: Length of %? in prompt
  2002-02-28 15:34           ` Ian Lynagh
@ 2002-02-28 16:45             ` Bart Schaefer
  2002-02-28 17:12               ` Ian Lynagh
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2002-02-28 16:45 UTC (permalink / raw)
  To: Ian Lynagh; +Cc: zsh-users

On Thu, 28 Feb 2002, Ian Lynagh wrote:

> I have just a couple more problems. Firstly I can't find a way to have
> ANSI escape sequences actually executed when they are passed through
> psvar - is this possible?

No, it's not possible.  The contents of psvar are always passed through
zsh's internal "nice display" filter, which works sort of like "cat -v".

> And secondly with
>     "%\$((\$COLUMNS-2))>%{%}k>"
> the %{%} appear in the prompt

In %N>STRING> the STRING is not re-interpreted for prompt escapes, but it
is not "nice-filtered" either.  Which also explains this:

> and both
>     "%\$((\$COLUMNS-2))>${(%%):-%{$fg_bold[cyan]%\}}k>"
> and "%\$((\$COLUMNS-2))>\${(%%):-%{$fg_bold[cyan]%\}}k>"
> seem to be counting the length of the ANSI escape sequence in the length
> calculation - am I missing something?

However, it's not making any sense to me that you want to insert a color
change only when the string is truncated.  I think you're probably seeking
something like this:

PS1='%$((COLUMNS/2))> >blah blah blah %<<%{$fg_bold[cyan]%}'

where "blah blah blah" is the variable-width stuff you want truncated.


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

* Re: Length of %? in prompt
  2002-02-28 16:45             ` Bart Schaefer
@ 2002-02-28 17:12               ` Ian Lynagh
  2002-02-28 21:00                 ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Ian Lynagh @ 2002-02-28 17:12 UTC (permalink / raw)
  To: zsh-users

On Thu, Feb 28, 2002 at 08:45:03AM -0800, Bart Schaefer wrote:
> On Thu, 28 Feb 2002, Ian Lynagh wrote:
> 
> > I have just a couple more problems. Firstly I can't find a way to have
> > ANSI escape sequences actually executed when they are passed through
> > psvar - is this possible?
> 
> No, it's not possible.  The contents of psvar are always passed through
> zsh's internal "nice display" filter, which works sort of like "cat -v".

OK, ta

> >     "%\$((\$COLUMNS-2))>${(%%):-%{$fg_bold[cyan]%\}}k>"
> > and "%\$((\$COLUMNS-2))>\${(%%):-%{$fg_bold[cyan]%\}}k>"
> > seem to be counting the length of the ANSI escape sequence in the length
> > calculation - am I missing something?
> 
> However, it's not making any sense to me that you want to insert a color
> change only when the string is truncated.  I think you're probably seeking
> something like this:

I missed the ^N out for simplicity, and in graphics mode k is a
top-right corner character. What I'm doing is something like this:

/--stuff--more stuff--some more stuff--------------------

and I am then truncating this to the screen width with \ (using / and \
for top-left and top-right corner characters respectively). The border
is in graphics mode and bold cyan while the various stuffs are in
various colours and normal text mode, so if it gets truncated in the
middle of them I am getting a text letter k in whatever colour happens
to be there. Therefore I want to set the colour and have a ^N in the
truncating string.

Any ideas?


Thanks
Ian


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

* Re: Length of %? in prompt
  2002-02-28 17:12               ` Ian Lynagh
@ 2002-02-28 21:00                 ` Bart Schaefer
  2002-03-01 12:38                   ` Ian Lynagh
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2002-02-28 21:00 UTC (permalink / raw)
  To: Ian Lynagh; +Cc: zsh-users

On Thu, 28 Feb 2002, Ian Lynagh wrote:

> What I'm doing is something like this:
>
> /--stuff--more stuff--some more stuff--------------------
>
> and I am then truncating this to the screen width with \ (using / and \
> for top-left and top-right corner characters respectively). The border
> is in graphics mode and bold cyan while the various stuffs are in
> various colours and normal text mode, so if it gets truncated in the
> middle of them I am getting a text letter k in whatever colour happens
> to be there. Therefore I want to set the colour and have a ^N in the
> truncating string.

OK, the point is that you don't want the upper right corner to be a
different color or character just because the string was truncated.  Thus
what you want to do here is sacrifice one additional character so that the
"truncating string" (let's call it the "ellipsis" since it plays the role
of the "..." in a truncated sentence) and the truncated string (let's call
that the "sentence" for the same reason) both end with the same character,
and that character is not a terminal control sequence.  Then, _after_ the
%<< that closes the sentence, you emit the correct color and the upper
right corner.  Don't worry about changing the color only if it's wrong,
just always force it to be right.

So something like (still using / and \ rather than graphics chars):

UL="$fg_bold[cyan]/"	# upper left
UR="$fg_bold[cyan]\\"	# upper right
PS1='$UL%$((COLUMNS-4))>->-stuff--more stuff--some more stuff---%<<$UR
\$ $reset_color'

(There's an embedded newline in that PS1 example.)

For a much more complicated example of this same thing, see

	Functions/Prompts/prompt_adam2_setup

in the 4.0.4 distribution.


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

* Re: Length of %? in prompt
  2002-02-28 21:00                 ` Bart Schaefer
@ 2002-03-01 12:38                   ` Ian Lynagh
  0 siblings, 0 replies; 12+ messages in thread
From: Ian Lynagh @ 2002-03-01 12:38 UTC (permalink / raw)
  To: zsh-users

On Thu, Feb 28, 2002 at 01:00:06PM -0800, Bart Schaefer wrote:
> On Thu, 28 Feb 2002, Ian Lynagh wrote:
> 
> > What I'm doing is something like this:
> >
> So something like (still using / and \ rather than graphics chars):
> 
> UL="$fg_bold[cyan]/"	# upper left
> UR="$fg_bold[cyan]\\"	# upper right
> PS1='$UL%$((COLUMNS-4))>->-stuff--more stuff--some more stuff---%<<$UR
> \$ $reset_color'

This won't help as the - still needs to be in the right colour etc, but

PS1='$UL%$((COLUMNS-3))>>-stuff--more stuff--some more stuff---%<<$UR
\$ $reset_color'

does what I want.

> For a much more complicated example of this same thing, see
> 
> 	Functions/Prompts/prompt_adam2_setup
> 
> in the 4.0.4 distribution.

OK, I'll take a look  :-)


Thanks
Ian


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

end of thread, other threads:[~2002-03-01 12:38 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-26 16:05 Length of %? in prompt Ian Lynagh
2002-02-26 16:48 ` Peter Stephenson
2002-02-26 17:10 ` Bart Schaefer
2002-02-26 18:08   ` Ian Lynagh
2002-02-26 18:26     ` Bart Schaefer
2002-02-26 18:42       ` Ian Lynagh
2002-02-26 19:21         ` Bart Schaefer
2002-02-28 15:34           ` Ian Lynagh
2002-02-28 16:45             ` Bart Schaefer
2002-02-28 17:12               ` Ian Lynagh
2002-02-28 21:00                 ` Bart Schaefer
2002-03-01 12:38                   ` Ian Lynagh

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).