zsh-users
 help / color / mirror / code / Atom feed
* Justifying text output
@ 2004-03-12  2:01 Thorsten Kampe
  2004-03-12 10:55 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-12  2:01 UTC (permalink / raw)
  To: zsh-users

I wrote a little script[1] that compiles the main zsh config files. Is
there any way to make the "[ ok ]"/"[ failed ]" messages on the right
justified?

Now:
! ERROR:    /etc/zsh/zprofile - directory not writable  [ failed ]
* compiling /home/thorsten/.zsh/.zlogin  [ ok ]
* compiling /home/thorsten/.zsh/.zshrc  [ ok ]
* compiling /home/thorsten/.zshenv  [ ok ]

Should be like that
! ERROR:    /etc/zsh/zprofile - directory not writable  [ failed ]
* compiling /home/thorsten/.zsh/.zlogin                 [ ok ]
* compiling /home/thorsten/.zsh/.zshrc                  [ ok ]
* compiling /home/thorsten/.zshenv                      [ ok ]

The problem is that there may be an error or no error whether I
execute this script as root or as a normal user. And some files don't
exist (/etc/zprofile and /etc/profile.d/zshell.zsh on Gentoo and
/etc/zsh/zprofile in Cygwin). So the output differs from case to case.

Thorsten

[1]
,---
| #! /bin/zsh -f
| emulate -LR zsh
| 
| autoload -U colors
| colors  # zshcontrib(1)
| 
| ltgreen=$fg_bold[green]
| ltred=$fg_bold[red]
| white=$fg_no_bold[white]
| 
| for file in /etc/profile.d/zshell.zsh \
|             /etc/zsh/zprofile         \
|             /etc/zprofile             \
|             ~/.zsh/.zlogin            \
|             ~/.zsh/.zshrc             \
|             ~/.zshenv
| do if   [[ -e $file && ! -r $file ]]
|    then echo "${ltred}* ${white}ERROR:    $file - file not readable  [ ${ltred}failed ${white}]"
|    elif [[ -e $file && ! -w $(dirname $file) ]]
|    then echo "${ltred}! ${white}ERROR:    $file - directory not writable  [ ${ltred}failed ${white}]"
|    elif [[ -r $file && -w $(dirname $file) ]]
|    then echo "${ltgreen}* ${white}compiling $file  [ ${ltgreen}ok ${white}]"
|         zcompile -R $file
|    fi
| done
`---


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

* Re: Justifying text output
  2004-03-12  2:01 Justifying text output Thorsten Kampe
@ 2004-03-12 10:55 ` Bart Schaefer
  2004-03-12 23:24   ` Thorsten Kampe
  2004-03-12 10:59 ` Peter Stephenson
  2004-03-12 11:28 ` Oliver Kiddle
  2 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2004-03-12 10:55 UTC (permalink / raw)
  To: zsh-users

On Mar 12,  3:01am, Thorsten Kampe wrote:
}
} there any way to make the "[ ok ]"/"[ failed ]" messages on the right
} justified?

Change each "echo" from e.g. this:

  echo "${ltgreen}* ${white}compiling $file  [ ${ltgreen}ok ${white}]"

To this:

  echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"

Note the placement of the quotes and closing curly-brace.

The (r:70:) parameter flag counts all characters -- it doesn't know that
${green} and ${white} expand to zero-width color changes -- so you have
to increase the padding to compensate.  You could do e.g.:

  ((PAD = COLUMNS - 12 + ${#ltgreen} + ${#white}))

and then

  echo ${(r:PAD:):-"${ltgreen}* ${white}compiling $file"} ...


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

* Re: Justifying text output
  2004-03-12  2:01 Justifying text output Thorsten Kampe
  2004-03-12 10:55 ` Bart Schaefer
@ 2004-03-12 10:59 ` Peter Stephenson
  2004-03-12 22:33   ` Thorsten Kampe
  2004-03-13  3:29   ` Thorsten Kampe
  2004-03-12 11:28 ` Oliver Kiddle
  2 siblings, 2 replies; 12+ messages in thread
From: Peter Stephenson @ 2004-03-12 10:59 UTC (permalink / raw)
  To: zsh-users

Thorsten Kampe wrote:
> I wrote a little script[1] that compiles the main zsh config files. Is
> there any way to make the "[ ok ]"/"[ failed ]" messages on the right
> justified?

There are various ways; most involve assigning the variable-length part
of the message to a string.  You should probably not put any escape
characters in there so it doesn't include them in the length
calculation (though if the escape sequences are all the same you would
get away with it).

The standard ksh way is (using a simplified example) to specify left
justification for a parameter:

 typeset -L 40 msg
 for msg in "message one" "a longer message here" "short"; do
 print ${msg} "[ WOW ]"
 done

giving:

 message one                              [ WOW ]
 a longer message here                    [ WOW ]
 short                                    [ WOW ]

You can avoid using the `-L 40' if you change ${msg} to ${(r.40.)msg}.
That's a zsh extension which pads to the given width with spaces.  The
`r' appears instead of `l' because it indicates where the padding goes,
not where the justification is.

However, you can actually get away without assigning the message to a
parameter:

 print ${(r.40.):-"message one"} "WOW"
 print ${(r.40.):-"a longer message here"} "WOW"
 print ${(r.40.):-"short"} "WOW"

giving

 message one                              WOW
 a longer message here                    WOW
 short                                    WOW

That works because zsh treats the absence of a parameter name as an
unset parameter, so it uses the normal logic for :-.  In other words,
`if the length of the parameter is zero, substitute the following string
instead'.   The (r.40.) padding flag works just the same.  You can
include further substitutions in the text, so this would still work in
your case.

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


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Justifying text output
  2004-03-12  2:01 Justifying text output Thorsten Kampe
  2004-03-12 10:55 ` Bart Schaefer
  2004-03-12 10:59 ` Peter Stephenson
@ 2004-03-12 11:28 ` Oliver Kiddle
  2004-03-12 22:33   ` Thorsten Kampe
  2 siblings, 1 reply; 12+ messages in thread
From: Oliver Kiddle @ 2004-03-12 11:28 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: zsh-users

Thorsten Kampe wrote:
> I wrote a little script[1] that compiles the main zsh config files. Is
> there any way to make the "[ ok ]"/"[ failed ]" messages on the right
> justified?

I'll add a third way, also compatible with ksh: using printf.

printf "${ltred}* ${white}%-60s [ ${ltred}failed${white} ]\n" "ERROR:    $file - file not readable"

They key bit is the '%-60s' in the format string which tells it to
print the string right justified with a width of 60 spaces.

Oliver


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

* Re: Justifying text output
  2004-03-12 10:59 ` Peter Stephenson
@ 2004-03-12 22:33   ` Thorsten Kampe
  2004-03-13  3:29   ` Thorsten Kampe
  1 sibling, 0 replies; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-12 22:33 UTC (permalink / raw)
  To: zsh-users

* Peter Stephenson (2004-03-12 11:59 +0100)
> Thorsten Kampe wrote:
>> I wrote a little script[1] that compiles the main zsh config files. Is
>> there any way to make the "[ ok ]"/"[ failed ]" messages on the right
>> justified?
> 
> There are various ways; most involve assigning the variable-length part
> of the message to a string.  You should probably not put any escape
> characters in there so it doesn't include them in the length
> calculation (though if the escape sequences are all the same you would
> get away with it).
> 
> The standard ksh way is (using a simplified example) to specify left
> justification for a parameter:

A "parameter" is a variable in "zshspeak"? And typeset declares the
type of the variable because Bourne shells "think" everything is a
string? ("Unlike many other programming languages, Bash does not
segregate its variables by "type". Essentially, Bash variables are
character strings" -- Advanced bash scripting Guide)
 
>  typeset -L 40 msg
>  for msg in "message one" "a longer message here" "short"; do
>  print ${msg} "[ WOW ]"
>  done
> [...]
> You can avoid using the `-L 40' if you change ${msg} to ${(r.40.)msg}.
> That's a zsh extension which pads to the given width with spaces.  The
> `r' appears instead of `l' because it indicates where the padding goes,
> not where the justification is.

Where is that documented?
 
> However, you can actually get away without assigning the message to a
> parameter:
> 
>  print ${(r.40.):-"message one"} "WOW"
>  print ${(r.40.):-"a longer message here"} "WOW"
>  print ${(r.40.):-"short"} "WOW"

Again: I couldn't find anything about than in the manpage (man
zshbuiltins for "printf"). What does ":-" do? Substring selection.
String formatting?

> That works because zsh treats the absence of a parameter name as an
> unset parameter, so it uses the normal logic for :-.  In other words,
> `if the length of the parameter is zero, substitute the following string
> instead'.   The (r.40.) padding flag works just the same.  You can
> include further substitutions in the text, so this would still work in
> your case.

Plain magic to me.

Thorsten


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

* Re: Justifying text output
  2004-03-12 11:28 ` Oliver Kiddle
@ 2004-03-12 22:33   ` Thorsten Kampe
  2004-03-12 23:03     ` Pavol Juhas
  0 siblings, 1 reply; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-12 22:33 UTC (permalink / raw)
  To: zsh-users

* Oliver Kiddle (2004-03-12 12:28 +0100)
> Thorsten Kampe wrote:
>> I wrote a little script[1] that compiles the main zsh config files. Is
>> there any way to make the "[ ok ]"/"[ failed ]" messages on the right
>> justified?
> 
> I'll add a third way, also compatible with ksh: using printf.
> 
> printf "${ltred}* ${white}%-60s [ ${ltred}failed${white} ]\n" "ERROR:    $file - file not readable"
> 
> They key bit is the '%-60s' in the format string which tells it to
> print the string right justified with a width of 60 spaces.

Thanks. The key bit is to understand it, but I haven't found any
documentation about that (just a small one in the susv3. Are there any
explanations available (along with this "${(r:70:):-" and
"${(r.40.):-" stuff)?

Thorsten


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

* Re: Justifying text output
  2004-03-12 22:33   ` Thorsten Kampe
@ 2004-03-12 23:03     ` Pavol Juhas
  0 siblings, 0 replies; 12+ messages in thread
From: Pavol Juhas @ 2004-03-12 23:03 UTC (permalink / raw)
  To: zsh-users

On Fri, Mar 12, 2004 at 11:33:40PM +0100, Thorsten Kampe wrote:
...
> Thanks. The key bit is to understand it, but I haven't found any
> documentation about that (just a small one in the susv3. Are there any
> explanations available (along with this "${(r:70:):-" and
> "${(r.40.):-" stuff)?

man zshexpn
then search for 
1   :-
2   Parameter Expansion Flags
3   l:expr
4   r:expr

HTH,

Pavol


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

* Re: Justifying text output
  2004-03-12 10:55 ` Bart Schaefer
@ 2004-03-12 23:24   ` Thorsten Kampe
  2004-03-13  6:24     ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-12 23:24 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (2004-03-12 11:55 +0100)
> On Mar 12,  3:01am, Thorsten Kampe wrote:
> }
> } there any way to make the "[ ok ]"/"[ failed ]" messages on the right
> } justified?
> 
> Change each "echo" from e.g. this:
> 
>   echo "${ltgreen}* ${white}compiling $file  [ ${ltgreen}ok ${white}]"
> 
> To this:
> 
>   echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"

I'm sorry but that outputs something like this:
*                                                             [ ok ] compiling                                                             [ ok ]                                                                       [ ok ]


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

* Re: Justifying text output
  2004-03-12 10:59 ` Peter Stephenson
  2004-03-12 22:33   ` Thorsten Kampe
@ 2004-03-13  3:29   ` Thorsten Kampe
  1 sibling, 0 replies; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-13  3:29 UTC (permalink / raw)
  To: zsh-users

* Peter Stephenson (2004-03-12 11:59 +0100)

Okay, I think I understand now this "typeset -L 40 msg" > "print
${(r.40.)msg}" > "print ${(r.40.):-"message"}" evolution...

Thanks


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

* Re: Justifying text output
  2004-03-12 23:24   ` Thorsten Kampe
@ 2004-03-13  6:24     ` Bart Schaefer
  2004-03-13 17:48       ` Thorsten Kampe
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2004-03-13  6:24 UTC (permalink / raw)
  To: zsh-users

On Mar 13, 12:24am, Thorsten Kampe wrote:
} Subject: Re: Justifying text output
}
} * Bart Schaefer (2004-03-12 11:55 +0100)
} > 
} >   echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"
} 
} I'm sorry but that outputs something like this:
} *                                                             [ ok ] compiling                                                             [ ok ]                                                                       [ ok ]

I think you must have gotten some double-quotes or curly-braces in the
wrong place, somehow.  Or something.

schaefer[501] file=somerandomstring
schaefer[502] echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"
* compiling somerandomstring                                          [ ok ]
schaefer[503] 


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

* Re: Justifying text output
  2004-03-13  6:24     ` Bart Schaefer
@ 2004-03-13 17:48       ` Thorsten Kampe
  2004-03-13 18:58         ` Oliver Kiddle
  0 siblings, 1 reply; 12+ messages in thread
From: Thorsten Kampe @ 2004-03-13 17:48 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (2004-03-13 07:24 +0100)
> On Mar 13, 12:24am, Thorsten Kampe wrote:
> } Subject: Re: Justifying text output
> }
> } * Bart Schaefer (2004-03-12 11:55 +0100)
> } > 
> } >   echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"
> } 
> } I'm sorry but that outputs something like this:
> } *                                                             [ ok ] compiling                                                             [ ok ]                                                                       [ ok ]
> 
> I think you must have gotten some double-quotes or curly-braces in the
> wrong place, somehow.  Or something.
> 
> schaefer[501] file=somerandomstring
> schaefer[502] echo ${(r:70:):-"${ltgreen}* ${white}compiling $file"}"[ ${ltgreen}ok ${white}]"
> * compiling somerandomstring                                          [ ok ]
> schaefer[503]

I figured it out: "setopt shwordsplit" made the "extra spaces" and
"setopt rcexpandparam" made the multiple "[ ok ]". Please advise if
setting them makes sense - meaning making the life of a zsh non-expert
easier or more difficult.

Thorsten


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

* Re: Justifying text output
  2004-03-13 17:48       ` Thorsten Kampe
@ 2004-03-13 18:58         ` Oliver Kiddle
  0 siblings, 0 replies; 12+ messages in thread
From: Oliver Kiddle @ 2004-03-13 18:58 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: zsh-users

Thorsten Kampe wrote:

> I figured it out: "setopt shwordsplit" made the "extra spaces" and
> "setopt rcexpandparam" made the multiple "[ ok ]". Please advise if
> setting them makes sense - meaning making the life of a zsh non-expert
> easier or more difficult.

Most zsh experts probably set rcexpandparam but not shwordsplit.

Setting shwordsplit makes zsh behave more like other Bourne type
shells. Variables containing spaces are split into separate words when
you expand the variable. Unless you're particularly used to it
working this way, you'll probably find that leaving it unset will make
life easier. If you want something to be split at spaces, it is
generally better to use an array.

rcexpandparam on the other hand tends to make life easier. To see what
it does, try this:

% a=(a b c)
% echo 1${a}2
1a b c2
% setopt rcexpandparam 
% echo 1${a}2	      
1a2 1b2 1c2

Given that you seem to be writing a function or script here, you might
want to consider adding:
  emulate -L zsh
to the top of it. That resets all the options to sane values locally
for the function. That way you don't risk breaking your script if you
change your option settings later.

Oliver


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

end of thread, other threads:[~2004-03-13 19:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-12  2:01 Justifying text output Thorsten Kampe
2004-03-12 10:55 ` Bart Schaefer
2004-03-12 23:24   ` Thorsten Kampe
2004-03-13  6:24     ` Bart Schaefer
2004-03-13 17:48       ` Thorsten Kampe
2004-03-13 18:58         ` Oliver Kiddle
2004-03-12 10:59 ` Peter Stephenson
2004-03-12 22:33   ` Thorsten Kampe
2004-03-13  3:29   ` Thorsten Kampe
2004-03-12 11:28 ` Oliver Kiddle
2004-03-12 22:33   ` Thorsten Kampe
2004-03-12 23:03     ` Pavol Juhas

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