stty 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stty-0.0.1/LICENSE +14 -0
- stty-0.0.1/PKG-INFO +343 -0
- stty-0.0.1/README.md +302 -0
- stty-0.0.1/pyproject.toml +50 -0
- stty-0.0.1/setup.cfg +4 -0
- stty-0.0.1/src/__init__.py +1 -0
- stty-0.0.1/src/stty.egg-info/PKG-INFO +343 -0
- stty-0.0.1/src/stty.egg-info/SOURCES.txt +10 -0
- stty-0.0.1/src/stty.egg-info/dependency_links.txt +1 -0
- stty-0.0.1/src/stty.egg-info/top_level.txt +3 -0
- stty-0.0.1/src/stty.py +742 -0
- stty-0.0.1/src/tests.py +450 -0
stty-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright (C) 2025 Soumendra Ganguly
|
|
2
|
+
|
|
3
|
+
# This program is free software: you can redistribute it and/or modify
|
|
4
|
+
# it under the terms of the GNU General Public License as published by
|
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
6
|
+
# (at your option) any later version.
|
|
7
|
+
|
|
8
|
+
# This program is distributed in the hope that it will be useful,
|
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
+
# GNU General Public License for more details.
|
|
12
|
+
|
|
13
|
+
# You should have received a copy of the GNU General Public License
|
|
14
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
stty-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stty
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A Python library for manipulating terminal settings in the style of POSIX `stty(1)`.
|
|
5
|
+
Author-email: Soumendra Ganguly <soumendraganguly@gmail.com>
|
|
6
|
+
Maintainer-email: Soumendra Ganguly <soumendraganguly@gmail.com>
|
|
7
|
+
License: # Copyright (C) 2025 Soumendra Ganguly
|
|
8
|
+
|
|
9
|
+
# This program is free software: you can redistribute it and/or modify
|
|
10
|
+
# it under the terms of the GNU General Public License as published by
|
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
12
|
+
# (at your option) any later version.
|
|
13
|
+
|
|
14
|
+
# This program is distributed in the hope that it will be useful,
|
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17
|
+
# GNU General Public License for more details.
|
|
18
|
+
|
|
19
|
+
# You should have received a copy of the GNU General Public License
|
|
20
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
21
|
+
Project-URL: Homepage, https://github.com/8vasu/stty.py
|
|
22
|
+
Project-URL: Issues, https://github.com/8vasu/stty.py/issues
|
|
23
|
+
Keywords: terminal,tty,stty,termios,winsize,posix,terminal-settings,pseudo-terminal,pty,terminal-attributes
|
|
24
|
+
Classifier: Development Status :: 4 - Beta
|
|
25
|
+
Classifier: Intended Audience :: Developers
|
|
26
|
+
Classifier: Topic :: Terminals
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
28
|
+
Classifier: Topic :: System :: Operating System
|
|
29
|
+
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
32
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
33
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
36
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
37
|
+
Requires-Python: >=3.9
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
License-File: LICENSE
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# stty.py
|
|
43
|
+
|
|
44
|
+
A Python library for manipulating terminal settings in the style of POSIX `stty(1)`.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Overview
|
|
49
|
+
|
|
50
|
+
`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.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- Read and modify terminal attributes (iflag, oflag, cflag, lflag, control characters, speeds, window size)
|
|
57
|
+
- Save and load settings to/from JSON files
|
|
58
|
+
- Apply settings to file descriptors or pseudo-terminals
|
|
59
|
+
- Symbolic and string-based access to all settings
|
|
60
|
+
- Emulates many `stty(1)` features and modes (e.g., raw, evenp, oddp, nl, ek)
|
|
61
|
+
- Cross-platform support (where `termios` is available)
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
This is a pure Python library. Place `stty.py` in your project or install via your preferred method.
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Examples
|
|
72
|
+
|
|
73
|
+
### 1. Reading and Printing Terminal Settings
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from stty import Stty
|
|
77
|
+
|
|
78
|
+
# Open terminal and get current settings from stdin (fd=0)
|
|
79
|
+
tty = Stty(fd=0)
|
|
80
|
+
print(tty) # Print all settings in a compact form
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
### 2. Setting Individual Attributes
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from stty import Stty
|
|
89
|
+
|
|
90
|
+
tty = Stty(fd=0)
|
|
91
|
+
|
|
92
|
+
# Turn off echo
|
|
93
|
+
tty.echo = False
|
|
94
|
+
|
|
95
|
+
# Set erase character to Ctrl-H
|
|
96
|
+
tty.erase = "^H"
|
|
97
|
+
|
|
98
|
+
# Set input baud rate to 9600
|
|
99
|
+
tty.ispeed = 9600
|
|
100
|
+
|
|
101
|
+
# Set number of rows in the terminal window (if supported)
|
|
102
|
+
tty.rows = 40
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### 3. Setting Multiple Attributes at Once
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from stty import Stty
|
|
111
|
+
|
|
112
|
+
tty = Stty(fd=0)
|
|
113
|
+
|
|
114
|
+
# Set several attributes in one call
|
|
115
|
+
tty.set(
|
|
116
|
+
echo=False,
|
|
117
|
+
icanon=False,
|
|
118
|
+
erase="^H",
|
|
119
|
+
ispeed=19200,
|
|
120
|
+
ospeed=19200,
|
|
121
|
+
rows=30,
|
|
122
|
+
cols=100
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### 4. Saving and Loading Settings
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from stty import Stty
|
|
132
|
+
|
|
133
|
+
tty = Stty(fd=0)
|
|
134
|
+
|
|
135
|
+
# Save current settings to a file
|
|
136
|
+
tty.save("my_tty_settings.json")
|
|
137
|
+
|
|
138
|
+
# Later, restore settings from the file
|
|
139
|
+
tty2 = Stty(path="my_tty_settings.json")
|
|
140
|
+
tty2.tofd(0) # Apply to stdin
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### 5. Using Raw Mode
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from stty import Stty
|
|
149
|
+
|
|
150
|
+
tty = Stty(fd=0)
|
|
151
|
+
tty.raw() # Set raw mode
|
|
152
|
+
tty.tofd(0) # Apply to stdin
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### 6. Working with Pseudo-terminals
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from stty import Stty
|
|
161
|
+
|
|
162
|
+
tty = Stty(fd=0)
|
|
163
|
+
m, s, sname = tty.openpty() # Open a new pty pair and apply settings to slave
|
|
164
|
+
print(f"Master fd: {m}, Slave fd: {s}, Slave name: {sname}")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
### 7. Setting Control Characters
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from stty import Stty
|
|
173
|
+
|
|
174
|
+
tty = Stty(fd=0)
|
|
175
|
+
|
|
176
|
+
# Set interrupt character to Ctrl-C
|
|
177
|
+
tty.intr = "^C"
|
|
178
|
+
|
|
179
|
+
# Set end-of-file character to Ctrl-D
|
|
180
|
+
tty.eof = "^D"
|
|
181
|
+
|
|
182
|
+
# Set suspend character to DEL
|
|
183
|
+
tty.susp = "^?"
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
### 8. Querying Settings as a Dictionary
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from stty import Stty
|
|
192
|
+
|
|
193
|
+
tty = Stty(fd=0)
|
|
194
|
+
settings = tty.get()
|
|
195
|
+
print(settings["echo"]) # True or False
|
|
196
|
+
print(settings["erase"]) # e.g., '^H'
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
### 9. Using Symbolic Constants
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
from stty import Stty, cs8, tab0
|
|
205
|
+
|
|
206
|
+
tty = Stty(fd=0)
|
|
207
|
+
|
|
208
|
+
# Set character size to 8 bits using symbolic constant
|
|
209
|
+
tty.csize = cs8
|
|
210
|
+
|
|
211
|
+
# Set tab delay to tab0
|
|
212
|
+
tty.tabdly = tab0
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
### 10. Fork new process with pseudo-terminal
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
x = stty.Stty(0)
|
|
221
|
+
x.intr = "^p"
|
|
222
|
+
pid, m, sname = x.forkpty()
|
|
223
|
+
|
|
224
|
+
if pid == 0: # Child process
|
|
225
|
+
with open("out", "w") as f:
|
|
226
|
+
f.write(str(stty.Stty(0)))
|
|
227
|
+
else: # Parent process
|
|
228
|
+
print(sname)
|
|
229
|
+
print("")
|
|
230
|
+
s = os.open(sname, os.O_RDWR)
|
|
231
|
+
print("Parent:", stty.Stty(s))
|
|
232
|
+
os.close(s)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## API Reference
|
|
238
|
+
|
|
239
|
+
### Classes
|
|
240
|
+
|
|
241
|
+
#### `Stty`
|
|
242
|
+
|
|
243
|
+
Manipulate terminal settings in the style of `stty(1)`.
|
|
244
|
+
|
|
245
|
+
**Constructor:**
|
|
246
|
+
```python
|
|
247
|
+
Stty(fd: int = None, path: str = None, **opts)
|
|
248
|
+
```
|
|
249
|
+
- `fd`: File descriptor to read settings from.
|
|
250
|
+
- `path`: Path to JSON file to load settings from.
|
|
251
|
+
- `**opts`: Any supported terminal attribute as a keyword argument.
|
|
252
|
+
|
|
253
|
+
**Methods:**
|
|
254
|
+
|
|
255
|
+
- `get() -> dict`
|
|
256
|
+
Return a dictionary of all current settings.
|
|
257
|
+
|
|
258
|
+
- `set(**opts)`
|
|
259
|
+
Set multiple attributes at once.
|
|
260
|
+
|
|
261
|
+
- `save(path: str = None)`
|
|
262
|
+
Save settings to a JSON file, or return a deep copy if no path is given.
|
|
263
|
+
|
|
264
|
+
- `load(path: str)`
|
|
265
|
+
Load settings from a JSON file.
|
|
266
|
+
|
|
267
|
+
- `fromfd(fd: int)`
|
|
268
|
+
Load settings from a file descriptor.
|
|
269
|
+
|
|
270
|
+
- `tofd(fd: int, when=TCSANOW, apply_termios=True, apply_winsize=True)`
|
|
271
|
+
Apply settings to a file descriptor.
|
|
272
|
+
|
|
273
|
+
- `evenp(plus=True)`
|
|
274
|
+
Set/unset even parity mode.
|
|
275
|
+
|
|
276
|
+
- `oddp(plus=True)`
|
|
277
|
+
Set/unset odd parity mode.
|
|
278
|
+
|
|
279
|
+
- `raw()`
|
|
280
|
+
Set terminal to raw mode.
|
|
281
|
+
|
|
282
|
+
- `nl(plus=True)`
|
|
283
|
+
Set/unset nl combination mode.
|
|
284
|
+
|
|
285
|
+
- `ek()`
|
|
286
|
+
Set ek combination mode.
|
|
287
|
+
|
|
288
|
+
- `openpty(apply_termios=True, apply_winsize=True)`
|
|
289
|
+
Open a new pseudo-terminal pair and apply settings.
|
|
290
|
+
|
|
291
|
+
- `forkpty(apply_termios=True, apply_winsize=True)`
|
|
292
|
+
Fork a new process with a pseudo-terminal and apply settings.
|
|
293
|
+
|
|
294
|
+
**Attribute Access:**
|
|
295
|
+
|
|
296
|
+
- All terminal attributes (e.g., `echo`, `icanon`, `erase`, `ispeed`, `rows`, etc.) are accessible as properties.
|
|
297
|
+
- Setting an attribute updates the internal state and validates the value.
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
### Constants
|
|
302
|
+
|
|
303
|
+
- `TCSANOW`, `TCSADRAIN`, `TCSAFLUSH`: the values accepted by the `when` named argument of `Stty.tofd()`. Compare with `termios.tcsetattr()`.
|
|
304
|
+
- Symbolic constants for masks and values (e.g., `cs8`, `tab0`, etc.) are available as module attributes.
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
### Data
|
|
309
|
+
|
|
310
|
+
- `settings`: A dictionary describing all available Stty attributes and their possible values on the current platform.
|
|
311
|
+
|
|
312
|
+
---
|
|
313
|
+
|
|
314
|
+
### Functions
|
|
315
|
+
|
|
316
|
+
- `settings_help_str`: Return help string about all available Stty attributes and their possible values on the current platform.
|
|
317
|
+
- `settings_help`: Print help message about all available Stty attributes and their possible values on the current platform.
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
## Compatibility
|
|
322
|
+
|
|
323
|
+
- Requires Python 3.x and a POSIX-like system with the `termios` module.
|
|
324
|
+
- Some features depend on platform support (e.g., window size).
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
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.
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
## Author
|
|
335
|
+
|
|
336
|
+
Soumendra Ganguly, 2025
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
## See Also
|
|
341
|
+
|
|
342
|
+
- [stty(1) manpage](https://man7.org/linux/man-pages/man1/stty.1.html)
|
|
343
|
+
- [Python termios documentation](https://docs.python.org/3/library/termios.html)
|
stty-0.0.1/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# stty.py
|
|
2
|
+
|
|
3
|
+
A Python library for manipulating terminal settings in the style of POSIX `stty(1)`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
`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.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Read and modify terminal attributes (iflag, oflag, cflag, lflag, control characters, speeds, window size)
|
|
16
|
+
- Save and load settings to/from JSON files
|
|
17
|
+
- Apply settings to file descriptors or pseudo-terminals
|
|
18
|
+
- Symbolic and string-based access to all settings
|
|
19
|
+
- Emulates many `stty(1)` features and modes (e.g., raw, evenp, oddp, nl, ek)
|
|
20
|
+
- Cross-platform support (where `termios` is available)
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
This is a pure Python library. Place `stty.py` in your project or install via your preferred method.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Examples
|
|
31
|
+
|
|
32
|
+
### 1. Reading and Printing Terminal Settings
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from stty import Stty
|
|
36
|
+
|
|
37
|
+
# Open terminal and get current settings from stdin (fd=0)
|
|
38
|
+
tty = Stty(fd=0)
|
|
39
|
+
print(tty) # Print all settings in a compact form
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
### 2. Setting Individual Attributes
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from stty import Stty
|
|
48
|
+
|
|
49
|
+
tty = Stty(fd=0)
|
|
50
|
+
|
|
51
|
+
# Turn off echo
|
|
52
|
+
tty.echo = False
|
|
53
|
+
|
|
54
|
+
# Set erase character to Ctrl-H
|
|
55
|
+
tty.erase = "^H"
|
|
56
|
+
|
|
57
|
+
# Set input baud rate to 9600
|
|
58
|
+
tty.ispeed = 9600
|
|
59
|
+
|
|
60
|
+
# Set number of rows in the terminal window (if supported)
|
|
61
|
+
tty.rows = 40
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
### 3. Setting Multiple Attributes at Once
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from stty import Stty
|
|
70
|
+
|
|
71
|
+
tty = Stty(fd=0)
|
|
72
|
+
|
|
73
|
+
# Set several attributes in one call
|
|
74
|
+
tty.set(
|
|
75
|
+
echo=False,
|
|
76
|
+
icanon=False,
|
|
77
|
+
erase="^H",
|
|
78
|
+
ispeed=19200,
|
|
79
|
+
ospeed=19200,
|
|
80
|
+
rows=30,
|
|
81
|
+
cols=100
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
### 4. Saving and Loading Settings
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from stty import Stty
|
|
91
|
+
|
|
92
|
+
tty = Stty(fd=0)
|
|
93
|
+
|
|
94
|
+
# Save current settings to a file
|
|
95
|
+
tty.save("my_tty_settings.json")
|
|
96
|
+
|
|
97
|
+
# Later, restore settings from the file
|
|
98
|
+
tty2 = Stty(path="my_tty_settings.json")
|
|
99
|
+
tty2.tofd(0) # Apply to stdin
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### 5. Using Raw Mode
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from stty import Stty
|
|
108
|
+
|
|
109
|
+
tty = Stty(fd=0)
|
|
110
|
+
tty.raw() # Set raw mode
|
|
111
|
+
tty.tofd(0) # Apply to stdin
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### 6. Working with Pseudo-terminals
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from stty import Stty
|
|
120
|
+
|
|
121
|
+
tty = Stty(fd=0)
|
|
122
|
+
m, s, sname = tty.openpty() # Open a new pty pair and apply settings to slave
|
|
123
|
+
print(f"Master fd: {m}, Slave fd: {s}, Slave name: {sname}")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### 7. Setting Control Characters
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from stty import Stty
|
|
132
|
+
|
|
133
|
+
tty = Stty(fd=0)
|
|
134
|
+
|
|
135
|
+
# Set interrupt character to Ctrl-C
|
|
136
|
+
tty.intr = "^C"
|
|
137
|
+
|
|
138
|
+
# Set end-of-file character to Ctrl-D
|
|
139
|
+
tty.eof = "^D"
|
|
140
|
+
|
|
141
|
+
# Set suspend character to DEL
|
|
142
|
+
tty.susp = "^?"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 8. Querying Settings as a Dictionary
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from stty import Stty
|
|
151
|
+
|
|
152
|
+
tty = Stty(fd=0)
|
|
153
|
+
settings = tty.get()
|
|
154
|
+
print(settings["echo"]) # True or False
|
|
155
|
+
print(settings["erase"]) # e.g., '^H'
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
### 9. Using Symbolic Constants
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from stty import Stty, cs8, tab0
|
|
164
|
+
|
|
165
|
+
tty = Stty(fd=0)
|
|
166
|
+
|
|
167
|
+
# Set character size to 8 bits using symbolic constant
|
|
168
|
+
tty.csize = cs8
|
|
169
|
+
|
|
170
|
+
# Set tab delay to tab0
|
|
171
|
+
tty.tabdly = tab0
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
### 10. Fork new process with pseudo-terminal
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
x = stty.Stty(0)
|
|
180
|
+
x.intr = "^p"
|
|
181
|
+
pid, m, sname = x.forkpty()
|
|
182
|
+
|
|
183
|
+
if pid == 0: # Child process
|
|
184
|
+
with open("out", "w") as f:
|
|
185
|
+
f.write(str(stty.Stty(0)))
|
|
186
|
+
else: # Parent process
|
|
187
|
+
print(sname)
|
|
188
|
+
print("")
|
|
189
|
+
s = os.open(sname, os.O_RDWR)
|
|
190
|
+
print("Parent:", stty.Stty(s))
|
|
191
|
+
os.close(s)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## API Reference
|
|
197
|
+
|
|
198
|
+
### Classes
|
|
199
|
+
|
|
200
|
+
#### `Stty`
|
|
201
|
+
|
|
202
|
+
Manipulate terminal settings in the style of `stty(1)`.
|
|
203
|
+
|
|
204
|
+
**Constructor:**
|
|
205
|
+
```python
|
|
206
|
+
Stty(fd: int = None, path: str = None, **opts)
|
|
207
|
+
```
|
|
208
|
+
- `fd`: File descriptor to read settings from.
|
|
209
|
+
- `path`: Path to JSON file to load settings from.
|
|
210
|
+
- `**opts`: Any supported terminal attribute as a keyword argument.
|
|
211
|
+
|
|
212
|
+
**Methods:**
|
|
213
|
+
|
|
214
|
+
- `get() -> dict`
|
|
215
|
+
Return a dictionary of all current settings.
|
|
216
|
+
|
|
217
|
+
- `set(**opts)`
|
|
218
|
+
Set multiple attributes at once.
|
|
219
|
+
|
|
220
|
+
- `save(path: str = None)`
|
|
221
|
+
Save settings to a JSON file, or return a deep copy if no path is given.
|
|
222
|
+
|
|
223
|
+
- `load(path: str)`
|
|
224
|
+
Load settings from a JSON file.
|
|
225
|
+
|
|
226
|
+
- `fromfd(fd: int)`
|
|
227
|
+
Load settings from a file descriptor.
|
|
228
|
+
|
|
229
|
+
- `tofd(fd: int, when=TCSANOW, apply_termios=True, apply_winsize=True)`
|
|
230
|
+
Apply settings to a file descriptor.
|
|
231
|
+
|
|
232
|
+
- `evenp(plus=True)`
|
|
233
|
+
Set/unset even parity mode.
|
|
234
|
+
|
|
235
|
+
- `oddp(plus=True)`
|
|
236
|
+
Set/unset odd parity mode.
|
|
237
|
+
|
|
238
|
+
- `raw()`
|
|
239
|
+
Set terminal to raw mode.
|
|
240
|
+
|
|
241
|
+
- `nl(plus=True)`
|
|
242
|
+
Set/unset nl combination mode.
|
|
243
|
+
|
|
244
|
+
- `ek()`
|
|
245
|
+
Set ek combination mode.
|
|
246
|
+
|
|
247
|
+
- `openpty(apply_termios=True, apply_winsize=True)`
|
|
248
|
+
Open a new pseudo-terminal pair and apply settings.
|
|
249
|
+
|
|
250
|
+
- `forkpty(apply_termios=True, apply_winsize=True)`
|
|
251
|
+
Fork a new process with a pseudo-terminal and apply settings.
|
|
252
|
+
|
|
253
|
+
**Attribute Access:**
|
|
254
|
+
|
|
255
|
+
- All terminal attributes (e.g., `echo`, `icanon`, `erase`, `ispeed`, `rows`, etc.) are accessible as properties.
|
|
256
|
+
- Setting an attribute updates the internal state and validates the value.
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
### Constants
|
|
261
|
+
|
|
262
|
+
- `TCSANOW`, `TCSADRAIN`, `TCSAFLUSH`: the values accepted by the `when` named argument of `Stty.tofd()`. Compare with `termios.tcsetattr()`.
|
|
263
|
+
- Symbolic constants for masks and values (e.g., `cs8`, `tab0`, etc.) are available as module attributes.
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
### Data
|
|
268
|
+
|
|
269
|
+
- `settings`: A dictionary describing all available Stty attributes and their possible values on the current platform.
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
### Functions
|
|
274
|
+
|
|
275
|
+
- `settings_help_str`: Return help string about all available Stty attributes and their possible values on the current platform.
|
|
276
|
+
- `settings_help`: Print help message about all available Stty attributes and their possible values on the current platform.
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## Compatibility
|
|
281
|
+
|
|
282
|
+
- Requires Python 3.x and a POSIX-like system with the `termios` module.
|
|
283
|
+
- Some features depend on platform support (e.g., window size).
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
287
|
+
## License
|
|
288
|
+
|
|
289
|
+
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.
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Author
|
|
294
|
+
|
|
295
|
+
Soumendra Ganguly, 2025
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## See Also
|
|
300
|
+
|
|
301
|
+
- [stty(1) manpage](https://man7.org/linux/man-pages/man1/stty.1.html)
|
|
302
|
+
- [Python termios documentation](https://docs.python.org/3/library/termios.html)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "stty"
|
|
8
|
+
version = "0.0.1"
|
|
9
|
+
description = "A Python library for manipulating terminal settings in the style of POSIX `stty(1)`."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
license = { file = "LICENSE" }
|
|
13
|
+
keywords = [
|
|
14
|
+
"terminal",
|
|
15
|
+
"tty",
|
|
16
|
+
"stty",
|
|
17
|
+
"termios",
|
|
18
|
+
"winsize",
|
|
19
|
+
"posix",
|
|
20
|
+
"terminal-settings",
|
|
21
|
+
"pseudo-terminal",
|
|
22
|
+
"pty",
|
|
23
|
+
"terminal-attributes"
|
|
24
|
+
]
|
|
25
|
+
authors = [{ name = "Soumendra Ganguly", email = "soumendraganguly@gmail.com" }]
|
|
26
|
+
maintainers = [
|
|
27
|
+
{ name = "Soumendra Ganguly", email = "soumendraganguly@gmail.com" },
|
|
28
|
+
]
|
|
29
|
+
classifiers = [
|
|
30
|
+
"Development Status :: 4 - Beta",
|
|
31
|
+
|
|
32
|
+
"Intended Audience :: Developers",
|
|
33
|
+
"Topic :: Terminals",
|
|
34
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
35
|
+
"Topic :: System :: Operating System",
|
|
36
|
+
|
|
37
|
+
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
|
38
|
+
|
|
39
|
+
"Programming Language :: Python :: 3",
|
|
40
|
+
"Programming Language :: Python :: 3.9",
|
|
41
|
+
"Programming Language :: Python :: 3.10",
|
|
42
|
+
"Programming Language :: Python :: 3.11",
|
|
43
|
+
"Programming Language :: Python :: 3.12",
|
|
44
|
+
"Programming Language :: Python :: 3.13",
|
|
45
|
+
"Programming Language :: Python :: 3 :: Only"
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[project.urls]
|
|
49
|
+
Homepage = "https://github.com/8vasu/stty.py"
|
|
50
|
+
Issues = "https://github.com/8vasu/stty.py/issues"
|
stty-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .stty import *
|