1

I have a scripts which generates huge amount of data and dump it to stdout. Using less it is easy to scroll through it:

./myscript | less

If myscript is running and providing data continuously, the latest less can update the view automatically (if the stdout buffer is flushed). This simple Python script shows this case:

import time
import os
import sys

for i in range(1000):
    stream = sys.stdout.buffer
    msg = str(i) + "\n"
    stream.write(msg.encode())
    stream.flush()

    if (i + 0) % 100 == 0:    
        time.sleep(5)
python3 pipe.py | less

What I want to achieve is that the script only provides the next page of data if the user reached the end of the scrolling in less (instead of automatic update which the script above does).

Is it possible to do this? (Note the Python script is just an example, the question is much more about the shell part.)

xhienne
  • 5,738
  • 1
  • 15
  • 34
Tibor Takács
  • 3,535
  • 1
  • 20
  • 23
  • 1
    Maybe relevant: https://unix.stackexchange.com/q/26826/118235 – Benjamin W. Jul 30 '20 at 18:10
  • I don't think there's any way for the program writing to the pipe to know what the user is doing in `less`. There's no feedback mechanism in a pipe. – Barmar Jul 30 '20 at 19:07
  • 1
    I guess you can ask `less` to use a smaller buffer size and have the program drop output instead of block if the pipe is not writable, but you'd be better off e.g. putting the pager functionality into the app itself, if all you want is to update the page with new info whenever the user wants it refreshed – that other guy Jul 30 '20 at 19:16

0 Answers0