tty.py 879 B

123456789101112131415161718192021222324252627282930313233343536
  1. """Terminal utilities."""
  2. # Author: Steen Lumholt.
  3. from termios import *
  4. __all__ = ["setraw", "setcbreak"]
  5. # Indexes for termios list.
  6. IFLAG = 0
  7. OFLAG = 1
  8. CFLAG = 2
  9. LFLAG = 3
  10. ISPEED = 4
  11. OSPEED = 5
  12. CC = 6
  13. def setraw(fd, when=TCSAFLUSH):
  14. """Put terminal into a raw mode."""
  15. mode = tcgetattr(fd)
  16. mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
  17. mode[OFLAG] = mode[OFLAG] & ~(OPOST)
  18. mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB)
  19. mode[CFLAG] = mode[CFLAG] | CS8
  20. mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG)
  21. mode[CC][VMIN] = 1
  22. mode[CC][VTIME] = 0
  23. tcsetattr(fd, when, mode)
  24. def setcbreak(fd, when=TCSAFLUSH):
  25. """Put terminal into a cbreak mode."""
  26. mode = tcgetattr(fd)
  27. mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON)
  28. mode[CC][VMIN] = 1
  29. mode[CC][VTIME] = 0
  30. tcsetattr(fd, when, mode)