control_codes.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Control character sets for terminal handling.
  3. This module provides the control character sets used by the width() function to handle terminal
  4. control characters.
  5. """
  6. # Illegal C0/C1 control characters.
  7. # These raise ValueError in 'strict' mode.
  8. ILLEGAL_CTRL = frozenset(
  9. chr(c) for c in (
  10. list(range(0x01, 0x07)) + # SOH, STX, ETX (^C), EOT (^D), ENQ, ACK
  11. list(range(0x10, 0x1b)) + # DLE through SUB (^Z)
  12. list(range(0x1c, 0x20)) + # FS, GS, RS, US
  13. [0x7f] + # DEL
  14. list(range(0x80, 0xa0)) # C1 control characters
  15. )
  16. )
  17. # Vertical movement control characters.
  18. # These raise ValueError in 'strict' mode (indeterminate horizontal position).
  19. VERTICAL_CTRL = frozenset({
  20. '\x0a', # LF (line feed)
  21. '\x0b', # VT (vertical tab)
  22. '\x0c', # FF (form feed)
  23. })
  24. # Horizontal movement control characters.
  25. # These affect cursor position and are tracked in 'strict' and 'parse' modes.
  26. HORIZONTAL_CTRL = frozenset({
  27. '\x08', # BS (backspace) - cursor left 1
  28. '\x09', # HT (horizontal tab) - advance to next tab stop
  29. '\x0d', # CR (carriage return) - cursor to column 0
  30. })
  31. # Terminal-valid zero-width control characters.
  32. # These are allowed in all modes (zero-width, no movement).
  33. ZERO_WIDTH_CTRL = frozenset({
  34. '\x00', # NUL
  35. '\x07', # BEL (bell)
  36. '\x0e', # SO (shift out)
  37. '\x0f', # SI (shift in)
  38. })
  39. # All control characters that need special handling (not regular printable).
  40. ALL_CTRL = ILLEGAL_CTRL | VERTICAL_CTRL | HORIZONTAL_CTRL | ZERO_WIDTH_CTRL | {'\x1b'}