Search code examples
python

Creating "Scrollable" Output in Command Line Programs


I have a program that outputs anywhere from 300-1000 lines of data. Rather than have it all output at once, I'd like it to have a manpages-like interface where it will display the first 50 or so lines of input and then the user can press 'f' or 'b' to navigate through the pages. Is there a way to do this in Python?

Note: I want to distribute the program, and I don't want to force users to pipe the output to less/more. Moreover, the output occurs in the middle of the program and is not the only output of the program, so I'm not sure if that would work any way.


Solution

  • You could do something very rudimentary like:

    # pseudocode 
    def display_text(text):
        lines = text.splitlines()
        while lines remaining:
            display next N lines
            wait for key press
    

    To "wait for key press", you could do something like this: http://www.daniweb.com/software-development/python/threads/123777/press-any-key-to-continue