Extract control characters from terminal settings. Returns a dict mapping control char names to their str values. Falls back to POSIX defaults if termios is not available or if the control character is not supported by termios.
(fd)
| 42 | |
| 43 | |
| 44 | def _get_terminal_ctrl_chars(fd): |
| 45 | """Extract control characters from terminal settings. |
| 46 | |
| 47 | Returns a dict mapping control char names to their str values. |
| 48 | |
| 49 | Falls back to POSIX defaults if termios is not available |
| 50 | or if the control character is not supported by termios. |
| 51 | """ |
| 52 | ctrl = dict(_POSIX_CTRL_CHARS) |
| 53 | try: |
| 54 | old = termios.tcgetattr(fd) |
| 55 | cc = old[6] # Index 6 is the control characters array |
| 56 | except (termios.error, OSError): |
| 57 | return ctrl |
| 58 | |
| 59 | # Use defaults for Backspace (BS) and Ctrl+A/E/K (SOH/ENQ/VT) |
| 60 | # as they are not in the termios control characters array. |
| 61 | for name in ('ERASE', 'KILL', 'WERASE', 'LNEXT', 'EOF', 'INTR'): |
| 62 | cap = getattr(termios, f'V{name}') |
| 63 | if cap < len(cc): |
| 64 | ctrl[name] = cc[cap].decode('latin-1') |
| 65 | return ctrl |
| 66 | |
| 67 | |
| 68 | def unix_getpass(prompt='Password: ', stream=None, *, echo_char=None): |
no test coverage detected
searching dependent graphs…