Skip to main content

Terminal/console pager module in pure Python

Project description

Python module that pages output to the screen, reads keys and console dimensions without executing external utils.

It was meant to be included in standard library http://bugs.python.org/issue8408

Author: anatoly techtonik <techtonik@gmail.com>
License: Public Domain (or MIT if a license is required)

Demo

python -m pager <file>
<command> | pager.py
pager.py < <file>

# run manual tests that are also a demo
python -m pager --test

Status

3.2 (stable)
  • abort pagination if ESC or ‘q’ keys are pressed

  • prompt callback is now able to abort pagination by returning False

3.1 (stable)
  • supports piped input (tested on Windows)

3.0 (stable) - API break in getch() function
  • reverted getch() behaviour changed in 2.0 - now getch() again returns single char

  • new constants with underline for getch() keys

  • new getchars() function that always returns list

2.1 (stable)
  • split getch() into _getch_unix() and _getch_windows() and detect correct flavor at import time (speedup)

2.0 (stable) - API break in getch() function
  • getch() now always returns list of characters (previously it could return single char). This is done to simplify the task of detecting keys from the returned result

1.4 (stable)
  • pager.py <file>

  • Linux: termios comments, docs and preparation for very likely API break in getch() function

1.3 (stable)
  • Windows: Python 3 requires getwch()

  • Windows: fix ENTER LEFT UP RIGHT DOWN ESC key codes

1.2 (stable)
  • add names for ENTER LEFT UP RIGHT DOWN ESC keys

  • manual tests for getch() (fixes bug #4)

  • dumpkey() helper function to get hex dump of a value returned by getch()

1.1 (stable)
  • Python 3 compatibility

  • echo() helper function for unbuffered output (in Py3 output doesn’t appear immediately on screen unless it ends with newline)

1.0 (stable)
  • getch() now returns list of chars for special keys (fixes bug #1 when special key skipped several pages)

  • page() callbacks receive obligatory pagenumber param

  • default page() callback now shows page number

0.2 (stable)
  • do not insert blank line between pages

0.1 (stable)
  • shows content page by page

  • allows to get console/terminal dimensions

  • works on Windows

  • works on Linux

API (output)

..function:: page(content, [pagecallback=prompt])

Output content iterable, calling pagecallback function after each page with page number as a parameter. pagecallback function may return False to terminate pagination.

Default prompt() callback shows page number with ‘Press any key to continue … ‘ prompt, waits for keypress and aborts if ‘q’, ESC or Ctrl-C are pressed.

..function:: echo(msg)

Print msg to the screen without linefeed and flush the output.

..function:: getwidth()

Return width of available window in characters. If detection fails, return value of standard width 80. Coordinate of the last character on a line is -1 from returned value.

..function:: getheight()

Return available window height in characters or 25 if detection fails. Coordinate of the last line is -1 from returned value.

API (input)

..function:: getch()

Wait for keypress, return first char generated as a result.

Arrows and special keys generate sequence of chars. Use getchars function to receive all chars generated or present in buffer.

..function:: getchars()

Wait for keypress. Return list of chars generated as a result. More than one char in result list is returned when arrows and special keys are pressed. Returned sequences differ between platforms, so use constants defined in this module to guess correct keys.

..function:: dumpkey(key)

Helper to convert result of getch (string) or getchars (list) to hex string.

Credits

Danny Yoo for getch()-like unbuffered character reading recipe

References

Excellent tutorials for Win32 Console by Adrian Worley
Console Reference on MSDN
Public Domain Curses library maintained by William McBrine
Ioctl (input/output control) introduction from Wikipedia
Linux Programmer’s Manual - ioctls for terminals and serial lines

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pager-3.2.zip (13.6 kB view details)

Uploaded Source

File details

Details for the file pager-3.2.zip.

File metadata

  • Download URL: pager-3.2.zip
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pager-3.2.zip
Algorithm Hash digest
SHA256 ac33365ad06208246b7410ee7fe2b7277527617f6b27c91ce9dbcf2420d31c07
MD5 be6b19afd171c1777a614f48791f52e1
BLAKE2b-256 fc0084d679270ab295c9bd770bac819e0e41837175626b6fd00377ac7a9afaa9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page