Read a line of input, echoing ``*`` per character; on submit, rewrite the line to show ``prompt + _mask_secret(value)`` (prefix 7 + suffix 4). Falls back to plain ``input()`` when stdin/stdout aren't TTYs (tests, pipes) and to ``getpass.getpass`` (no echo at all) on platforms withou
(prompt: str)
| 258 | |
| 259 | |
| 260 | def _masked_input(prompt: str) -> str: |
| 261 | """Read a line of input, echoing ``*`` per character; on submit, rewrite |
| 262 | the line to show ``prompt + _mask_secret(value)`` (prefix 7 + suffix 4). |
| 263 | |
| 264 | Falls back to plain ``input()`` when stdin/stdout aren't TTYs (tests, |
| 265 | pipes) and to ``getpass.getpass`` (no echo at all) on platforms |
| 266 | without ``termios`` (Windows). |
| 267 | """ |
| 268 | import sys |
| 269 | |
| 270 | if not (sys.stdin.isatty() and sys.stdout.isatty()): |
| 271 | return input(prompt) |
| 272 | |
| 273 | try: |
| 274 | import termios |
| 275 | import tty |
| 276 | except ImportError: |
| 277 | import getpass |
| 278 | |
| 279 | return getpass.getpass(prompt) |
| 280 | |
| 281 | sys.stdout.write(prompt) |
| 282 | sys.stdout.flush() |
| 283 | fd = sys.stdin.fileno() |
| 284 | old_attrs = termios.tcgetattr(fd) |
| 285 | chars: list[str] = [] |
| 286 | try: |
| 287 | tty.setraw(fd) |
| 288 | while True: |
| 289 | ch = sys.stdin.read(1) |
| 290 | if ch in ("\r", "\n"): |
| 291 | break |
| 292 | if ch == "\x03": # Ctrl-C |
| 293 | raise KeyboardInterrupt |
| 294 | if ch == "\x04": # Ctrl-D / EOF |
| 295 | if not chars: |
| 296 | raise EOFError |
| 297 | break |
| 298 | if ch in ("\x7f", "\b"): # Backspace / DEL |
| 299 | if chars: |
| 300 | chars.pop() |
| 301 | sys.stdout.write("\b \b") |
| 302 | sys.stdout.flush() |
| 303 | continue |
| 304 | if ch < " ": # Other control chars — ignore |
| 305 | continue |
| 306 | chars.append(ch) |
| 307 | sys.stdout.write("*") |
| 308 | sys.stdout.flush() |
| 309 | finally: |
| 310 | termios.tcsetattr(fd, termios.TCSADRAIN, old_attrs) |
| 311 | value = "".join(chars) |
| 312 | # Rewrite the line: \r → clear → prompt + masked preview + \n. |
| 313 | sys.stdout.write("\r\033[2K" + prompt + _mask_secret(value) + "\n") |
| 314 | sys.stdout.flush() |
| 315 | return value |
| 316 | |
| 317 |