[Tutor] Scrolling through output in shell

ALAN GAULD alan.gauld at btinternet.com
Mon Nov 17 20:27:43 CET 2008

Forwarding to the group.
Please use Reply All when responding.

----- Original Message ----
From: Mike Hoy <mhoy06 at gmail.com>
To: Alan Gauld <alan.gauld at btinternet.com>

> I'm writing a program that reads a text file onto the screen. The text
> file is too large to read so I want to be able to scroll through it
> with the arrow key or something like that. I am not using GUI. 

In that case you need to write some screen handling code to 
do the paging. Ideally that means working out how many lines 
the screen can display then displaying the data in chunks of 
that size. Basic screen handling can be as simple as hit return
to contnue. But if you want to page up as well then you need a 
bit more. Here is some pseudo (ie. incomplete and untested) code:

data = myfile.readlines()
size = 25 ## get real screen length here!
top = 0

while True:
      for line in data[top:top+size]:
           print line
      key = raw_input("N for Next page, P for Previous page, Q to Quit")
      if key in 'Nn': top += size
      elif key in 'Pp': top -= size
      elif key in 'Qq': break
   except IndexError: break

Obviously you can make the control keys as sophisticated as you want!


Alan G.

More information about the Tutor mailing list