Skip to content

Instantly share code, notes, and snippets.

@gesquive
Last active January 17, 2021 19:53
Show Gist options
  • Save gesquive/249ab1f5ab71d90ef57c to your computer and use it in GitHub Desktop.
Save gesquive/249ab1f5ab71d90ef57c to your computer and use it in GitHub Desktop.
Use less to page output
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