Skip to content

Instantly share code, notes, and snippets.

@butlerallenj
Last active May 2, 2019 00:31
Show Gist options
  • Save butlerallenj/f1c6300f4e0a647fe426246837664b8e to your computer and use it in GitHub Desktop.
Save butlerallenj/f1c6300f4e0a647fe426246837664b8e to your computer and use it in GitHub Desktop.
Paginate Files via STDIN
#!/usr/bin/env python3
import sys
import math
import argparse
parser = argparse.ArgumentParser(
description="Paginate the output of a file or command via STDIN\nExample: cat log.txt | paginate.py -p 2 -l 20\nAuthor:@butlerallenj"
)
parser.add_argument('-l', '--limit', type=int, default=50, help="The per page quantity to print out. Default: 50")
parser.add_argument('-p', '--page', type=int, default=1, help="The page number. Default: 1")
parser.add_argument('-t', '--total', action="store_true", default=False, help="Print total pages")
args = parser.parse_args()
begin = (args.limit * args.page) - args.limit
i = 0
item_count = 0
for line in sys.stdin:
i += 1
if i < begin or item_count >= args.limit:
continue
item_count += 1
if not args.total:
print(line.strip())
if args.total:
total = math.ceil(i / args.limit)
print("total_pages:{total}".format(total=total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment