Sometimes we run a command in the terminal, and the output is too large, and we forgot to put "| less" in the end. So I am wondering would it possible to page the output when it is too large in zsh?
I tried to implement this feature by using python and less:
#!/usr/bin/env python3 termHeight = 25 import sys from subprocess import Popen, PIPE p = Popen(['unbuffer'] + sys.argv[1:], stdin=PIPE, stdout=PIPE) lines = [] for count in range(termHeight): line = p.stdout.readline() if not line: break print(line.decode('utf8'), end='') lines += [line] if line: q = Popen(['less', '-Mr'], stdin=PIPE) q.stdin.writelines(lines) while True: line = p.stdout.readline() if not line: break q.stdin.write(line) q.communicate() let's save this python script to p.py. So when we run "python p.py some commands" like "python p.py ls --help", if the output is more than 25 lines, this script will use less to display the output.
The problem is I can not get input from user. Which means this solution does not work with interactive program at all.