zsh-users
 help / color / mirror / code / Atom feed
* vanishing spaces
@ 2024-03-30 15:18 Ray Andrews
  2024-03-30 16:07 ` Bart Schaefer
  2024-03-30 16:07 ` Marc Chantreux
  0 siblings, 2 replies; 4+ messages in thread
From: Ray Andrews @ 2024-03-30 15:18 UTC (permalink / raw)
  To: Zsh Users

     aptitude columnizes its output nicely: (cut down here to avoid 
wrapping):

% aptitude search '~i?name(nvidia)'
i A glx-alternative-nvidia                  - allows the selection of 
NVIDIA i A libegl-nvidia-tesla-470-0               - NVIDIA binary EGL 
library
i A libgl1-nvidia-tesla-470-glvnd-glx       - NVIDIA binary OpenGL/GLX
i A libgles-nvidia-tesla-470-1              - NVIDIA binary OpenGL|ES 1.x

     ... I have reason to capture the output of several versions of 
aptitude searches into a variable. I hope 'eval' is the right way:

output=$( eval $* )  # Save output to an array variable.

     ... where: '$*' ... is the command string that's been put together 
elsewhere.

print -l $output
i A glx-alternative-nvidia - allows the selection of NVIDIA as GLX provider
i A libegl-nvidia-tesla-470-0 - NVIDIA binary EGL library (Tesla 470 
version)
i A libgl1-nvidia-tesla-470-glvnd-glx - NVIDIA binary OpenGL/GLX library

     ... the nice columns disappear. However:

eval $*

     ... shows it with columns intact so eval itself is not the issue 
it's somewhere in the capture of the variable.  How can I fix that?  I'm 
used to doing battle with splitting issues but this seems different.  
'typeset -p' shows me nothing useful.  Is this one of those things were 
zsh is helpfully removing an empty element?  Somehow the padding spaces 
are their own separate element and since empty, removed?  That's my only 
guess.  I've tried a dozen variations on quoting with no luck. I know 
how to preserve empty lines, but that doesn't work here.






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

* Re: vanishing spaces
  2024-03-30 15:18 vanishing spaces Ray Andrews
@ 2024-03-30 16:07 ` Bart Schaefer
  2024-03-30 16:07 ` Marc Chantreux
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2024-03-30 16:07 UTC (permalink / raw)
  To: Zsh Users

On Sat, Mar 30, 2024 at 8:18 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>      aptitude columnizes its output [...]
>      ... I have reason to capture the output of several versions of
> aptitude searches into a variable. I hope 'eval' is the right way:
>
> output=$( eval $* )  # Save output to an array variable.

That's not an array variable, it's a string.  You need
  output=( $( ... ) )
for array assignment.  However, simple array assignment is not what
you want there, or you'll end up with every word of the output in its
own element.

You might not need the "eval" there, because $( ... ) is already a
command substitution; you only need the "eval" if there are elements
of $* that  you want further expanded.  It may work to use just $(
"$@" )

> print -l $output
> i A glx-alternative-nvidia - allows the selection of NVIDIA as GLX provider
[etc]
>      ... the nice columns disappear.

It would appear from "print -l" that you're trying to get each line of
the output into a separate array element.  Is that correct?

I'm not sure from what you've shown us why you're losing the internal
spacing, unless perhaps aptitude is doing it because it's output is
not a terminal.  What does

print -r -- "$( eval $* )"

show?


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

* Re: vanishing spaces
  2024-03-30 15:18 vanishing spaces Ray Andrews
  2024-03-30 16:07 ` Bart Schaefer
@ 2024-03-30 16:07 ` Marc Chantreux
  2024-03-30 17:16   ` Ray Andrews
  1 sibling, 1 reply; 4+ messages in thread
From: Marc Chantreux @ 2024-03-30 16:07 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Sat, Mar 30, 2024 at 08:18:30AM -0700, Ray Andrews wrote:

> % aptitude search '~i?name(nvidia)'

aptitude search '~i~nnvidia' for short. check it out:

	aptitude-doc-en: /usr/share/doc/aptitude/html/en/ch02s04s05.html

>     ... I have reason to capture the output of several versions of aptitude
> searches into a variable. I hope 'eval' is the right way:

about the vanishing spaces: it's because you need to protect them with
quotes both while reading and writing them:

bad:

	output=$( eval $* )
	echo $output

good:

	output="$( eval $* )"
	echo "$output"

Aside: I don't understand why you eval it. it's ok to write

	output="$( aptitude search '~i~nnvidia' )"

plus: I try to avoid eval as much as possible because it's fragile.
if your command is stored in "$@" with all the parameters set correctly,
you don't need eval:

	set -- aptitude search '~i~nnvidia'
	output=$( "$@" )

quotes are important here also as "$@" will be expanded as

	"aptitude" "search" "~i~nnvidia"

HTH,
marc


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

* Re: vanishing spaces
  2024-03-30 16:07 ` Marc Chantreux
@ 2024-03-30 17:16   ` Ray Andrews
  0 siblings, 0 replies; 4+ messages in thread
From: Ray Andrews @ 2024-03-30 17:16 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1456 bytes --]



On 2024-03-30 09:07, Marc Chantreux wrote:
> aptitude search '~i~nnvidia' for short. check it out:

Works fine, thanks.
> good:
>
> 	output="$( eval $* )"
> 	echo "$output"
I tried to minimalize the issue:

% var="$(aptitude search '~i~n(nvidia)')"
% print "$var"

...  but I still loose the spaces.  No variation on quoting helps. Tried 
with:
emulate -L zsh
... thinking it might be some option or other, but no change.

> Aside: I don't understand why you eval it. it's ok to write
In my function I have nested expansions, so the eval seemed necessary.

Bart:

> That's not an array variable, it's a string.  You need
   output=( $( ... ) )
for array assignment.

... of course, sorry, I pasted an experimental line by accident.

> What does

print -r -- "$( eval $* )"

show?

... no improvement.

% print -r -- "$(aptitude search '~i~n(nvidia)')"

... doesn't fix it either.

Seems Marc is using Debian (or derivative) so if he gets it showing properly then I must have some local anomaly.

Hey ... just occurs to me now ... the aptitude columns are 'smart' -- the wider your terminal the wider the gap between columns!  So, as you intuit Bart, maybe the columnizing can't work without a terminal. -- it's a feature specific to a known width of output.

... yeah, that's it.  'aptitude --disable-columns' ... same output.  So this never was a zsh issue.  I wish we had some way of killing threads, I'd just nuke this thread as a red herring.






[-- Attachment #2: Type: text/html, Size: 2242 bytes --]

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

end of thread, other threads:[~2024-03-30 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-30 15:18 vanishing spaces Ray Andrews
2024-03-30 16:07 ` Bart Schaefer
2024-03-30 16:07 ` Marc Chantreux
2024-03-30 17:16   ` Ray Andrews

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