zsh-users
 help / color / mirror / code / Atom feed
* strange interaction between read command and pipe to less from function
@ 2023-03-26 14:33 Jim
  2023-03-26 14:55 ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Jim @ 2023-03-26 14:33 UTC (permalink / raw)
  To: zsh


[-- Attachment #1.1: Type: text/plain, Size: 1150 bytes --]

Hi everyone,

I'm at a loss trying to figure out what is going on with a function I am
testing.
It has the following read statement which is part of a loop:

for N ({0..255}) {
  read -s -t1 -d $'\a' $'c?\e]4;'${N}$';?\a' && XC+=("$N ${c##*;}")
}

This works just fine in retrieving the hex color codes in both xterm and
xfce4-terminal.

The issue is when the output of my function is piped to less.  The keys do
not respond
correctly.  Where 'space' would normally page the output, or 'q' would quit
less, I now
have to follow the keystroke(space, q, gg ...) with the 'enter' key to have
the action take
place.

Commenting out the read statement and less acts normally from output I
generated
to test with or without the read statement.

I attached the function file with an added '.txt' extension to name(FYI).

Any thoughts on what is happening would be appreciated. The read statement
was
taken from something I found on one of the 'Stack' sites if I remember
correctly.

I guess I could be using the read statement incorrectly, but I don't know
what it
could be.

ZSH_PATCHLEVEL:  zsh-5.9-0-g73d3173

Thanks in advance, and regards,

Jim Murphy

[-- Attachment #1.2: Type: text/html, Size: 1667 bytes --]

[-- Attachment #2: cvt_256_colors_2_rgb_hex_fmt.txt --]
[-- Type: text/plain, Size: 759 bytes --]

emulate -L zsh -o extendedglob
# Function:  cvt_256_colors_2_rgb_hex_fmt
local     E N c
local -a  T S XC NRGB
local -aU RGB
for N ({0..255}) {
  :
  read -s -t1 -d $'\a' $'c?\e]4;'${N}$';?\a' && XC+=("$N ${c##*;}")
}
for E ($XC) {
  S=(${(z)E}) ; T=(${(s./.)S[2]#*:})
  NRGB+=("${(l.3..0.)S[1]} ${${T[1]}[1,2]}${${T[2]}[1,2]}${${T[3]}[1,2]}")
  RGB+=("${${T[1]}[1,2]}${${T[2]}[1,2]}${${T[3]}[1,2]}")
}
#print -- "XC: $#XC   NRGB: $#NRGB   RGB: $#RGB"
#print -- "$XC[1]   $XC[256]"
#print -- "$NRGB[1]   $NRGB[256]"
#print -- "$RGB[1]   $RGB[256]"
#print -l -- $XC
#print -l -- $NRGB
#print -l -- $RGB
# Next line is just output to see if read command is doing something strange
for E (${(ok)parameters}) print -- $E
# vim: ts=2 sw=2 sts=2 sta ai et ft=zsh :

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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 14:33 strange interaction between read command and pipe to less from function Jim
@ 2023-03-26 14:55 ` Roman Perepelitsa
  2023-03-26 15:28   ` Jim
  0 siblings, 1 reply; 7+ messages in thread
From: Roman Perepelitsa @ 2023-03-26 14:55 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Sun, Mar 26, 2023 at 4:34 PM Jim <linux.tech.guy@gmail.com> wrote:
>
> [...] a function I am testing [...] has the following read statement which is part of a loop:
>
> for N ({0..255}) {
>   read -s -t1 -d $'\a' $'c?\e]4;'${N}$';?\a' && XC+=("$N ${c##*;}")
> }
>
> The issue is when the output of my function is piped to less.

Both the function and less read from the TTY. Which of them will
succeed when you press a key is anyone's guess.

Roman.


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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 14:55 ` Roman Perepelitsa
@ 2023-03-26 15:28   ` Jim
  2023-03-26 15:49     ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Jim @ 2023-03-26 15:28 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh

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

On Sun, Mar 26, 2023 at 9:56 AM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> On Sun, Mar 26, 2023 at 4:34 PM Jim <linux.tech.guy@gmail.com> wrote:
> >
> > [...] a function I am testing [...] has the following read statement
> which is part of a loop:
> >
> > for N ({0..255}) {
> >   read -s -t1 -d $'\a' $'c?\e]4;'${N}$';?\a' && XC+=("$N ${c##*;}")
> > }
> >
> > The issue is when the output of my function is piped to less.
>
> Both the function and less read from the TTY. Which of them will
> succeed when you press a key is anyone's guess.
>
> Roman.
>

OK, but if the read is already completed, shouldn't less be in control?
I just added 'sleep 2' before the print statement and still see the same
result.
Even if the last read didn't receive any input it should have timed
out(-t1).
What am I not understanding?  Using command as follows:

cvt_256_colors_2_rgb_hex_fmt | less

Thanks for the quick response.

Jim

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

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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 15:28   ` Jim
@ 2023-03-26 15:49     ` Roman Perepelitsa
  2023-03-26 17:04       ` Jim
  0 siblings, 1 reply; 7+ messages in thread
From: Roman Perepelitsa @ 2023-03-26 15:49 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Sun, Mar 26, 2023 at 5:28 PM Jim <linux.tech.guy@gmail.com> wrote:
>
> cvt_256_colors_2_rgb_hex_fmt | less

In this command both cvt_256_colors_2_rgb_hex_fmt and less are going
to read and write to the same TTY. This won't do anything useful.

Perhaps you don't realize that your `read` command reads and writes to
the TTY? What do you expect from running it simultaneously with less?

Roman.


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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 15:49     ` Roman Perepelitsa
@ 2023-03-26 17:04       ` Jim
  2023-03-26 17:23         ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Jim @ 2023-03-26 17:04 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh

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

Me again,

On Sun, Mar 26, 2023 at 10:49 AM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> On Sun, Mar 26, 2023 at 5:28 PM Jim <linux.tech.guy@gmail.com> wrote:
> >
> > cvt_256_colors_2_rgb_hex_fmt | less
>
> In this command both cvt_256_colors_2_rgb_hex_fmt and less are going
> to read and write to the same TTY. This won't do anything useful.
>
> Perhaps you don't realize that your `read` command reads and writes to
> the TTY? What do you expect from running it simultaneously with less?
>
> Roman.
>

Now I'm totally confused.  A bit by your answer and the word
'simultaneously'
as I thought the function would run first then less. That set aside I dug
back
into a previous function I was working on that used the same read statement.
And yet that function when piped through less, less worked as expected. Both
functions did 'basically' the same thing  with a somewhat different output.
The
one main difference was I had done a few checks before the read command
which included this:

  V=$(ps -o args= $(ps -o ppid= $$))

So I added it before the read.  Now 'less' responds as expected. I hope you
are not as confused as I am.  Especially as the command is run before the
read and not after. Also removed the sleep command.

????

Again thanks for your help,

Jim

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

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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 17:04       ` Jim
@ 2023-03-26 17:23         ` Roman Perepelitsa
  2023-03-26 23:50           ` Lawrence Velázquez
  0 siblings, 1 reply; 7+ messages in thread
From: Roman Perepelitsa @ 2023-03-26 17:23 UTC (permalink / raw)
  To: linuxtechguy; +Cc: zsh

On Sun, Mar 26, 2023 at 7:04 PM Jim <linux.tech.guy@gmail.com> wrote:
>
> Now I'm totally confused.  A bit by your answer and the word 'simultaneously'
> as I thought the function would run first then less.

When you run `cvt_256_colors_2_rgb_hex_fmt | less`, two processes run
simultaneously. One is executing `cvt_256_colors_2_rgb_hex_fmt`, the
other is executing `less`.

> I hope you are not as confused as I am.

I am not. When two commands simultaneously write and read the same
TTY, anything goes. By adding something to the function you've changed
the timings, so now the race condition manifests differently.

Roman.


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

* Re: strange interaction between read command and pipe to less from function
  2023-03-26 17:23         ` Roman Perepelitsa
@ 2023-03-26 23:50           ` Lawrence Velázquez
  0 siblings, 0 replies; 7+ messages in thread
From: Lawrence Velázquez @ 2023-03-26 23:50 UTC (permalink / raw)
  To: Jim; +Cc: Roman Perepelitsa, zsh-users

On Sun, Mar 26, 2023, at 1:23 PM, Roman Perepelitsa wrote:
> On Sun, Mar 26, 2023 at 7:04 PM Jim <linux.tech.guy@gmail.com> wrote:
>>
>> Now I'm totally confused.  A bit by your answer and the word 'simultaneously'
>> as I thought the function would run first then less.
>
> When you run `cvt_256_colors_2_rgb_hex_fmt | less`, two processes run
> simultaneously. One is executing `cvt_256_colors_2_rgb_hex_fmt`, the
> other is executing `less`.

A demonstration:

	% foo() for arg do print -n $arg >$TTY; done
	% repeat 10 do foo {a..j} | foo {0..9}; echo; done
	0ab1c2d3e4f5gh6i7j89
	0a1b2c3de4f5g6h7i8j9
	01a2b3c4d5e6fg7h8i9j
	0a1bc2de3fgh4ij56789
	01ab2c3d4e5f6g78h9ij
	01a2b34c56d78e9fghij
	0a1bc2de3fgh4ij56789
	ab0cd1ef2gh3ij456789
	a0bc1d2e3f4g5h6i7j89
	0ab1c2d3ef4gh5i6j789

Note the nondeterministic output.

-- 
vq


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

end of thread, other threads:[~2023-03-26 23:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-26 14:33 strange interaction between read command and pipe to less from function Jim
2023-03-26 14:55 ` Roman Perepelitsa
2023-03-26 15:28   ` Jim
2023-03-26 15:49     ` Roman Perepelitsa
2023-03-26 17:04       ` Jim
2023-03-26 17:23         ` Roman Perepelitsa
2023-03-26 23:50           ` Lawrence Velázquez

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