-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpage
executable file
·81 lines (71 loc) · 2.07 KB
/
page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /usr/bin/env python3
import sys, getopt, os, pickle
# TODO add option for --next and --previous, storing the current page in an environment variable.
def printUsage():
usage = """
Paging tool by Rohitt Vashishtha
Usage: pipeinput | page --size=<pagesize> --num=<pagenum>
Description: Use this tool to print a long file a few lines at a time
and run mre commands manually after each cycle. Makes dealing with large
amount of data easier for mere humans. Have fun. :)
License: "THE BEER-WARE LICENSE" (Revision 42): Rohitt Vashishtha wrote
this file. As long as you retain this notice you can do whatever you
want with this stuff. If we meet some day, and you think this stuff is
worth it, you can buy me a beer in return.
- Rohitt Vashishtha
"""
print(usage)
def getDefaultParams():
params = {}
params['size']=10
params['num']=1
try:
file = open('~/Documents/scripts/params.page', 'r')
params['num'] = int(file.readline())
except FileNotFoundError:
pass
except ValueError:
pass
return params
def setParams(argv):
params = getDefaultParams()
try:
opts, args = getopt.getopt(argv,"hs:n:",["size=","num=","next","prev"])
except getopt.GetoptError:
print ('Incorrect usage. Use page -h to print usage.')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printUsage()
sys.exit(0)
elif opt in ("-n", "--num"):
params['num'] = int(arg)
elif opt in ("-s", "--size"):
params['size'] = int(arg)
elif opt in ("--next"):
params['num'] += 1
elif opt in ("--prev"):
params['num'] -= 1
try:
file = open('~/Documents/scripts/params.page', 'w+')
file.write(str(params['num']))
file.close()
except FileNotFoundError:
pass
print(params)
return params
def printPage(params):
linestoskip = params['size']*(params['num']-1)
linestoprint = params['size']
for line in sys.stdin:
if linestoskip > 0:
linestoskip -= 1
# print("> ",line)
if linestoskip == 0 and linestoprint > 0:
sys.stdout.write(line)
# print(line)
linestoprint -= 1
if linestoprint == 0:
sys.exit(0)
if __name__ == "__main__":
printPage(setParams(sys.argv[1:]))