Skip to main content

Functions related to terminals.

Latest release 20260912: New ttysizepx(fd) function returning the tty size in characters and pixels.

Short summary:

  • modify_termios: Apply mode changes to a tty. Return the previous tty modes as from termios.tcgetattr or None if the changes could not be applied. If strict, raise an exception instead of returning None.

  • setupterm: Run curses.setupterm, needed to be able to use the status line. Uses a global flag to avoid doing this twice.

  • stack_termios: Context manager to apply and restore changes to a tty. Yield the previous tty modes as from termios.tcgetattr or None if the changes could not be applied. If strict, raise an exception instead of yielding None.

  • status: Write a message to the terminal's status line.

  • statusline: Update the status line.

  • statusline_bs: Return a byte string to update the status line.

  • ttysize: Return a (rows, columns) tuple for the specified file descriptor.

  • ttysizepx: Return a (rows,columns,widthpx,heightpx) tuple for the specified file descriptor being the terminal character rows and columns and pixel width and height respectively.

  • WinSize: WinSize(rows, columns).

  • WinSizePX: WinSizePX(rows, columns, widthpx, heightpx).

Functions

modify_termios(fd=0, set_modes=None, clear_modes=None, strict=False)

Apply mode changes to a tty. Return the previous tty modes as from termios.tcgetattr or None if the changes could not be applied. If strict, raise an exception instead of returning None.

Parameters:

  • fd: optional tty file descriptor, default 0.
  • set_modes: an optional mapping of attribute name to new value for values to set
  • clear_modes: an optional mapping of attribute name to new value for values to clear
  • strict: optional flag, default False; if true, raise exceptions from failed tcgetattr and tcsetattr calls otherwise issue a warning if the errno is not ENOTTY and proceed. This aims to provide ease of use in batch mode by default while providing a mode to fail overtly if required.

The attribute names are from iflag, oflag, cflag, lflag, ispeed, ospeed, cc, corresponding to the list entries defined by the termios.tcgetattr call.

For set_modes, the attributes ispeed, ospeed and cc are applied directly; the other attributes are binary ORed into the existing modes.

For clear_modes, the attributes ispeed, ospeed and cc cannot be cleared; the other attributes are binary removed from the existing modes.

For example, to turn off the terminal echo during some operation:

old_modes = apply_termios(clear_modes={'lflag': termios.ECHO}):
    ... do something with tty echo disabled ...
if old_modes:
    termios.tcsetattr(fd, termios.TCSANOW, old_modes)

setupterm(*args)

Run curses.setupterm, needed to be able to use the status line. Uses a global flag to avoid doing this twice.

stack_termios(fd=0, set_modes=None, clear_modes=None, strict=False)

Context manager to apply and restore changes to a tty. Yield the previous tty modes as from termios.tcgetattr or None if the changes could not be applied. If strict, raise an exception instead of yielding None.

Parameters:

  • fd: optional tty file descriptor, default 0.
  • set_modes: an optional mapping of attribute name to new value for values to set
  • clear_modes: an optional mapping of attribute name to new value for values to clear
  • strict: optional flag, default False; if true, raise exceptions from failed tcgetattr and tcsetattr calls otherwise issue a warning if the errno is not ENOTTY and proceed. This aims to provide ease of use in batch mode by default while providing a mode to fail overtly if required.

The attribute names are from iflag, oflag, cflag, lflag, ispeed, ospeed, cc, corresponding to the list entries defined by the termios.tcgetattr call.

For set_modes, the attributes ispeed, ospeed and cc are applied directly; the other attributes are binary ORed into the existing modes.

For clear_modes, the attributes ispeed, ospeed and cc cannot be cleared; the other attributes are binary removed from the existing modes.

For example, to turn off the terminal echo during some operation:

with stack_termios(clear_modes={'lflag': termios.ECHO}):
    ... do something with tty echo disabled ...

status(msg, *args, **kwargs)

Write a message to the terminal's status line.

Parameters:

  • msg: message string
  • args: if not empty, the message is %-formatted with args
  • file: optional keyword argument specifying the output file. Default: sys.stderr.

Hack: if there is no status line use the xterm title bar sequence :-(

statusline(text, fd=None, reverse=False, xpos=None, ypos=None)

Update the status line.

statusline_bs(text, reverse=False, xpos=None, ypos=None)

Return a byte string to update the status line.

ttysize(fd)

Return a (rows, columns) tuple for the specified file descriptor.

If the window size cannot be determined, None will be returned for either or both of rows and columns.

This function relies on the UNIX stty command.

ttysizepx(fd)

Return a (rows,columns,widthpx,heightpx) tuple for the specified file descriptor being the terminal character rows and columns and pixel width and height respectively.

This function relies on the fcntl.ioctl using termios.TIOCGWINSZ.

Classes

class WinSize(builtins.tuple)

WinSize(rows, columns)

WinSize.__match_args__

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

WinSize.__replace__(self, /, **kwds)

Return a new WinSize object replacing specified fields with new values

WinSize.__slots__

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

WinSize.columns

Alias for field number 1

WinSize.rows

Alias for field number 0

class WinSizePX(builtins.tuple)

WinSizePX(rows, columns, widthpx, heightpx)

WinSizePX.__match_args__

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

WinSizePX.__replace__(self, /, **kwds)

Return a new WinSizePX object replacing specified fields with new values

WinSizePX.__slots__

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

WinSizePX.columns

Alias for field number 1

WinSizePX.heightpx

Alias for field number 3

WinSizePX.rows

Alias for field number 0

WinSizePX.widthpx

Alias for field number 2

Release Log

Release 20260912: New ttysizepx(fd) function returning the tty size in characters and pixels.

Release 20210316:

  • ttysize: discard the Popen object earlier.
  • ttysize: close Popen.stdout after use. seems to leak.

Release 20201102: New modify_termios and stack_termios to apply (and restore) termios modes.

Release 20200521:

  • New status() function dragged in from cs.logutils, which uses cs.upd for status() -- needs some refactoring to match with the other functions in cs.tty -- text vs bytes, stdout vs stderr, etc.
  • Get warning() from cs.gimmicks.

Release 20190101: Small bugfix for setupterm.

Release 20170903: add statusline and statusline_s functions; ttysize: support BSD stty output format

Release 20160828: Use "install_requires" instead of "requires" in DISTINFO, add PyPI category.

Release 20150116: Initial PyPI release.

Release files for cs-tty 20260912

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-tty 20260912
File Size Uploaded
cs_tty-20260912.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cs-tty 20260912
File Interpreter ABI Platform
cs_tty-20260912-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.0 kB

Release files / cs_tty-20260912.tar.gz

Download URL cs_tty-20260912.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d3f3ee11e462da52b944548f3a783c0780181591dac11f1d29a1e4c634580478
BLAKE2b-256 checksum
How to use checksums
2f4dcb979d57cc4da40d973d7a7d4c6f5f5eb27e2a0a3acee3c2599309f138b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / cs_tty-20260912-py2.py3-none-any.whl

Download URL cs_tty-20260912-py2.py3-none-any.whl
Size 6.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3b955ccd8ab11d80b8de0b30fed9eefdcb0fd6a736763c285b1001a5dec05767
BLAKE2b-256 checksum
How to use checksums
b3e33d86f281ca6898184c9f68629205992d355fe09620f832a62a88492c932e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release history Release notifications | RSS feed

This release

20260912 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page