A Python library for manipulating terminal settings in the style of POSIX `stty(1)`.
Project description
stty.py
A Python library for manipulating terminal settings in the style of POSIX stty(1).
Overview
stty provides a high-level, Pythonic interface to terminal I/O settings, including control characters, baud rates, and window size, using the termios and related modules. It allows you to get, set, save, and restore terminal attributes, and to apply them to file descriptors or pseudo-terminals.
Features
- Read and modify terminal attributes (iflag, oflag, cflag, lflag, control characters, speeds, window size)
- Save and load settings to/from JSON files
- Apply settings to file descriptors or pseudo-terminals
- Symbolic and string-based access to all settings
- Emulates many
stty(1)features and modes (e.g., raw, evenp, oddp, nl, ek) - Cross-platform support (where
termiosis available)
Installation
This is a pure Python library. Place stty.py in your project or install via your preferred method.
Examples
1. Reading and Printing Terminal Settings
from stty import Stty
# Open terminal and get current settings from stdin (fd=0)
tty = Stty(fd=0)
print(tty) # Print all settings in a compact form
2. Setting Individual Attributes
from stty import Stty
tty = Stty(fd=0)
# Turn off echo
tty.echo = False
# Set erase character to Ctrl-H
tty.erase = "^H"
# Set input baud rate to 9600
tty.ispeed = 9600
# Set number of rows in the terminal window (if supported)
tty.rows = 40
3. Setting Multiple Attributes at Once
from stty import Stty
tty = Stty(fd=0)
# Set several attributes in one call
tty.set(
echo=False,
icanon=False,
erase="^H",
ispeed=19200,
ospeed=19200,
rows=30,
cols=100
)
4. Saving and Loading Settings
from stty import Stty
tty = Stty(fd=0)
# Save current settings to a file
tty.save("my_tty_settings.json")
# Later, restore settings from the file
tty2 = Stty(path="my_tty_settings.json")
tty2.tofd(0) # Apply to stdin
5. Using Raw Mode
from stty import Stty
tty = Stty(fd=0)
tty.raw() # Set raw mode
tty.tofd(0) # Apply to stdin
6. Working with Pseudo-terminals
from stty import Stty
tty = Stty(fd=0)
m, s, sname = tty.openpty() # Open a new pty pair and apply settings to slave
print(f"Master fd: {m}, Slave fd: {s}, Slave name: {sname}")
7. Setting Control Characters
from stty import Stty
tty = Stty(fd=0)
# Set interrupt character to Ctrl-C
tty.intr = "^C"
# Set end-of-file character to Ctrl-D
tty.eof = "^D"
# Set suspend character to DEL
tty.susp = "^?"
8. Querying Settings as a Dictionary
from stty import Stty
tty = Stty(fd=0)
settings = tty.get()
print(settings["echo"]) # True or False
print(settings["erase"]) # e.g., '^H'
9. Using Symbolic Constants
from stty import Stty, cs8, tab0
tty = Stty(fd=0)
# Set character size to 8 bits using symbolic constant
tty.csize = cs8
# Set tab delay to tab0
tty.tabdly = tab0
10. Fork new process with pseudo-terminal
x = stty.Stty(0)
x.intr = "^p"
pid, m, sname = x.forkpty()
if pid == 0: # Child process
with open("out", "w") as f:
f.write(str(stty.Stty(0)))
else: # Parent process
print(sname)
print("")
s = os.open(sname, os.O_RDWR)
print("Parent:", stty.Stty(s))
os.close(s)
API Reference
Classes
Stty
Manipulate terminal settings in the style of stty(1).
Constructor:
Stty(fd: int = None, path: str = None, **opts)
fd: File descriptor to read settings from.path: Path to JSON file to load settings from.**opts: Any supported terminal attribute as a keyword argument.
Methods:
-
get() -> dictReturn a dictionary of all current settings. -
set(**opts)Set multiple attributes at once. -
save(path: str = None)Save settings to a JSON file, or return a deep copy if no path is given. -
load(path: str)Load settings from a JSON file. -
fromfd(fd: int)Load settings from a file descriptor. -
tofd(fd: int, when=TCSANOW, apply_termios=True, apply_winsize=True)Apply settings to a file descriptor. -
evenp(plus=True)Set/unset even parity mode. -
oddp(plus=True)Set/unset odd parity mode. -
raw()Set terminal to raw mode. -
nl(plus=True)Set/unset nl combination mode. -
ek()Set ek combination mode. -
openpty(apply_termios=True, apply_winsize=True)Open a new pseudo-terminal pair and apply settings. -
forkpty(apply_termios=True, apply_winsize=True)Fork a new process with a pseudo-terminal and apply settings.
Attribute Access:
- All terminal attributes (e.g.,
echo,icanon,erase,ispeed,rows, etc.) are accessible as properties. - Setting an attribute updates the internal state and validates the value.
Constants
TCSANOW,TCSADRAIN,TCSAFLUSH: the values accepted by thewhennamed argument ofStty.tofd(). Compare withtermios.tcsetattr().- Symbolic constants for masks and values (e.g.,
cs8,tab0, etc.) are available as module attributes.
Data
settings: A dictionary describing all available Stty attributes and their possible values on the current platform.
Functions
settings_help_str: Return help string about all available Stty attributes and their possible values on the current platform.settings_help: Print help message about all available Stty attributes and their possible values on the current platform.
Compatibility
- Requires Python 3.x and a POSIX-like system with the
termiosmodule. - Some features depend on platform support (e.g., window size).
License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Author
Soumendra Ganguly, 2025
See Also
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stty-0.0.1.tar.gz.
File metadata
- Download URL: stty-0.0.1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9ba6dbed2f3fb8b52b23e6a5273016b5f0051625f74343dc01041667574ea5e
|
|
| MD5 |
824dec386cea301dad5d7ca38fca8437
|
|
| BLAKE2b-256 |
760370bea2f525777ae10b3e32722a8a1d877986dd9c78542f4752ad6b2e444c
|
Provenance
The following attestation bundles were made for stty-0.0.1.tar.gz:
Publisher:
python-publish.yml on 8vasu/stty.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stty-0.0.1.tar.gz -
Subject digest:
c9ba6dbed2f3fb8b52b23e6a5273016b5f0051625f74343dc01041667574ea5e - Sigstore transparency entry: 428773829
- Sigstore integration time:
-
Permalink:
8vasu/stty.py@fece76ac066e4a8f23f4efad41c7f2c96cb732a4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/8vasu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@fece76ac066e4a8f23f4efad41c7f2c96cb732a4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file stty-0.0.1-py3-none-any.whl.
File metadata
- Download URL: stty-0.0.1-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1bfab9e785c63d2609152bf35dc9f71288a786eb3331e9aae78afdf1fd73d70
|
|
| MD5 |
024adf76aed72e7b89526226bc24e10f
|
|
| BLAKE2b-256 |
6325f27e31c6cc43bb35fd6e5025862435339c9d9d04506a45d3ebf0e96efe18
|
Provenance
The following attestation bundles were made for stty-0.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on 8vasu/stty.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stty-0.0.1-py3-none-any.whl -
Subject digest:
a1bfab9e785c63d2609152bf35dc9f71288a786eb3331e9aae78afdf1fd73d70 - Sigstore transparency entry: 428773844
- Sigstore integration time:
-
Permalink:
8vasu/stty.py@fece76ac066e4a8f23f4efad41c7f2c96cb732a4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/8vasu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@fece76ac066e4a8f23f4efad41c7f2c96cb732a4 -
Trigger Event:
release
-
Statement type: