Search code examples
pythonlinuxstdout

How to print to terminal like git log?


I'm writing a simple CLI application using python.
I have a list of records that I want to print in the terminal and I would like to output them just like git log does. So as a partial list you can load more using the down arrow, from which you quit by typing "q" (basically like the output of less, but without opening and closing a file).

How does git log do that?


Solution

  • Device you are looking for is called pager, there exists pipepager function inside pydoc, which is not documented in linked pydoc docs, but using interactive python console you might learn that

    >>> help(pydoc.pipepager)
    Help on function pipepager in module pydoc:
    
    pipepager(text, cmd)
        Page through text by feeding it to another program.
    

    therefore it seems that you should use this as follows

    import pydoc
    pydoc.pipepager("your text here", "less")
    

    with limitation that it does depends on availability of less command.