Last active
January 17, 2021 19:53
-
-
Save gesquive/249ab1f5ab71d90ef57c to your computer and use it in GitHub Desktop.
Use less to page output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import os | |
def output_to_pager(text): | |
try: | |
# args stolen fron git source, see `man less` | |
pager = subprocess.Popen(['less', '-F', '-R', '-S', '-X', '-K'], | |
stdin=subprocess.PIPE, | |
stdout=sys.stdout) | |
for line in text: | |
pager.stdin.write("{}{}".format(line, os.linesep)) | |
pager.stdin.close() | |
pager.wait() | |
except KeyboardInterrupt: | |
pass | |
# let less handle this, -K will exit cleanly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment