I will occasionally use the pickaxe functionality of git
to locate changes of interest. This can be quite slow, obviously (the same would apply to, say, hg grep
), but more significantly it is bursty: a few results clumped together, separated by inactivity.
For these reasons, I try and read the results as they come, in practice by piping the output to less
(in fact, this is what git
does by default): when a burst comes, you can't possibly hit ctrl-S
fast enough for the software flow control functionality to be useful these days.
But I often hit a… behaviour of less
where if I scroll enough that even just one line of content to show on screen has yet to come out of the git
command, less
gets stuck on that and I lose its prompt and most ways to control it, until more content comes. Now, I understand why it happens, but if that was the behaviour of a shell command, I'd use ctrl-Z
to recover a prompt (and then do bg
). Is there an equivalent in less
to recover the pager prompt?
In this situation, ctrl-Z
is useless: Unix job control is inappropriate for commands that manage the screen contents. Besides, I don't want to get back to the shell prompt anyway: typically, what I want to do is check back something I might have glimpsed while I was repeatedly hitting the space bar, so I don't want to leave less.
Ctrl-C
is also useless: while it does allow me to recover a prompt, this will additionally result in the received content being frozen; that is, when my curiosity is satisfied and I try and get back to the latest content (with a few taps of the space bar, or G
, etc.), nothing more than what I had comes up, no matter how long I wait. Even with F
(tail -f
mode).
I use this command to simulate the git
behaviour:
(echo -e 'foo\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n.' ; sleep 3 ; echo -e 'bar\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n.' ; sleep 10; echo baz) | less
sleep 5
at the start was bothering me, so I looked at ways to fix that. Here is what I found: Linux actually avoid the issue I mention on MacOS in a rather heavy-handed way: opening a FIFO for writing (without reading) blocks until someone else has the same FIFO open for reading, thus delaying the writes that could otherwise have been subject to SIGPIPE. So this solves the issue, but such rendez-vous-based synchronization is almost always problematic: for instance, if I needed to involve multiple FIFO, I would need to remember to open them in the same order, or I will deadlock. – Pierre Lebeaupin Feb 20 '23 at 00:52>fifo
and< fifo
) in two parallel branches, the background and the foreground ones; but then you risk synchronization issues on other platforms. We definitely want to avoid examples that work on one platform but break unpredictably elsewhere. – Pierre Lebeaupin Feb 20 '23 at 01:00less
is provided with such a file descriptor, it will never recieve an end of file status… as there is still a writer left: itself. – Pierre Lebeaupin Feb 20 '23 at 01:08sleep 5
until I have improved one of them to be acceptable. – Pierre Lebeaupin Feb 20 '23 at 01:12FIFO
(named pipe), and haveless
open that:(sleep 5; ( echo -e 'foo\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n.' ; sleep 3 ; echo -e 'bar\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n.' ; sleep 10; echo baz ) > fifo ) & less < fifo
As you can see, there is one complication… – Pierre Lebeaupin Feb 20 '23 at 22:02FIFO
(or possibly even just open it for writing) until it has also been opened for reading, or your writer process will get hit with aSIGPIPE
… In some way, that makes sense, though I'd prefer if that signal were raised only in cases where there used to be such a reader, but now no longer is. To avoid theSIGPIPE
, I insert a sleep to delay the opening for and writing to theFIFO
untilless
itself is ready. – Pierre Lebeaupin Feb 20 '23 at 22:02sh -m
rather thansetsid sh
will not work for our purposes (I tried): the former "only" creates a process group, which is not what matters for signal delivery purposes, as the new process group is still part of the session controlled by the terminal. The stronger setsid is needed. – Pierre Lebeaupin Feb 20 '23 at 22:44