-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstty.py
70 lines (62 loc) · 2.04 KB
/
stty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: [email protected]
Date of creation: 20-11-2024
Last update: 20-11-2024
Version: 1.0
Description: The stty command from GNU coreutils in Python3.
Example of use: python3 stty.py
'''
import sys
import termios
import tty
import fcntl
import os
class STTYCommand:
def __init__(self, fd=sys.stdin.fileno()):
self.fd = fd
def get_terminal_attributes(self):
"""Get current terminal attributes"""
try:
attrs = termios.tcgetattr(self.fd)
return attrs
except termios.error as e:
print(f"Error getting terminal attributes: {e}", file=sys.stderr)
return None
def display_speed(self):
"""Display input and output baud rates"""
try:
ispeed = termios.tcgetattr(self.fd)[2]
ospeed = termios.tcgetattr(self.fd)[3]
print(f"Input speed: {ispeed}")
print(f"Output speed: {ospeed}")
except termios.error as e:
print(f"Error getting speed: {e}", file=sys.stderr)
def display_line_settings(self):
"""Display basic line settings"""
try:
attrs = self.get_terminal_attributes()
if attrs:
print(f"Control modes: {attrs[2]}")
print(f"Local modes: {attrs[3]}")
print(f"Input modes: {attrs[0]}")
print(f"Output modes: {attrs[1]}")
except Exception as e:
print(f"Error displaying settings: {e}", file=sys.stderr)
def run(self, args):
"""Main method to handle different stty operations"""
if len(args) == 0:
# Default: display current settings
self.display_line_settings()
self.display_speed()
elif args[0] == 'speed':
self.display_speed()
else:
print(f"Unsupported operation: {' '.join(args)}", file=sys.stderr)
sys.exit(1)
def main():
stty = STTYCommand()
stty.run(sys.argv[1:])
if __name__ == "__main__":
main()