This recipe shows how to create a simple text file pager in Python. It allows you to view text content a page at a time (with a user-definable number of lines per page). Like standard Unix utilities, it can either take a text file name as a command-line argument, or can read the text from its standard input, which can be redirected to come from a file, or to come from a pipe. The recipe is for Windows only, though, since it uses the msvcrt.getch() function, which is Windows-specific. However, the recipe can be modified to work on Unix by using things like tty, curses, termios, cbreak, etc.
More details here:
https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html
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 | '''
tp.py
Purpose: A simple text pager.
Version: 0.1
Platform: Windows-only.
Can be adapted for Unix using tty / termios calls.
Only the use of msvcrt.getch() needs to be changed.
Author: Vasudev Ram
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
'''
import sys
import string
from msvcrt import getch
def pager(in_fil=sys.stdin, lines_per_page=10, quit_key='q'):
assert lines_per_page > 1 and lines_per_page == int(lines_per_page)
assert len(quit_key) == 1 and \
quit_key in (string.ascii_letters + string.digits)
lin_ctr = 0
for lin in in_fil:
sys.stdout.write(lin)
lin_ctr += 1
if lin_ctr >= lines_per_page:
c = getch().lower()
if c == quit_key.lower():
break
else:
lin_ctr = 0
def main():
try:
sa, lsa = sys.argv, len(sys.argv)
if lsa == 1:
pager()
elif lsa == 2:
with open(sa[1], "r") as in_fil:
pager(in_fil)
else:
sys.stderr.write
("Only one input file allowed in this version")
except IOError as ioe:
sys.stderr.write("Caught IOError: {}".format(repr(ioe)))
sys.exit(1)
except Exception as e:
sys.stderr.write("Caught Exception: {}".format(repr(e)))
sys.exit(1)
if __name__ == '__main__':
main()
|
More details here:
https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html
tried to run it in IDE "Thonny" got this error:
Traceback (most recent call last): File "I:\$PC\PROGRMMING\graphic text pager.py", line 57, in <module> mainloop() NameError: name 'mainloop' is not defined
It works fine for me, and why do you need to run a command-line tool like this pager from an IDE? Just adds to the confusion, and more chances of something going wrong. IDE is not necessary or even appropriate for a command-line tool, unless (maybe) you are debugging it. Run the pager from the command-line. There is no variable called mainloop in my program, so it must be from your tool's code - sounds like a Tkinter-based IDE, because it and some other GUI toolkits have a mainloop or similarly named event loop.
@peter: P.S. If you are not used to using the command-line (in whatever OS you use, be it Windows, Linux, MacOS or some other), I recommend you learn how to use it. It will need some time but pay you back many times over in saved time and productivity, particularly if you learn some shell scripting too (shell on Unix / Linux / MacOS), batch files on Windows. Perl or Python or Ruby or similar language on any OS.
ok - my fault .. i-m newbie her thanks for your answer
@peter: Not a problem - I realized from your comments that you were a newbie (all of us were that at some time, no worries) and hence gave the extra advice. Good luck with your learning.