Bombabillo is a non-web client for the terminal, supporting Gopher, Gemini and much more. https://bombadillo.colorfield.space
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.1 KiB

  1. package termios
  2. import (
  3. "os"
  4. "runtime"
  5. "syscall"
  6. "unsafe"
  7. )
  8. type winsize struct {
  9. Row uint16
  10. Col uint16
  11. Xpixel uint16
  12. Ypixel uint16
  13. }
  14. var fd = os.Stdin.Fd()
  15. func ioctl(fd, request, argp uintptr) error {
  16. if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, request, argp); e != 0 {
  17. return e
  18. }
  19. return nil
  20. }
  21. func GetWindowSize() (int, int) {
  22. var value winsize
  23. ioctl(fd, syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&value)))
  24. return int(value.Col), int(value.Row)
  25. }
  26. func getTermios() syscall.Termios {
  27. var value syscall.Termios
  28. err := ioctl(fd, getTermiosIoctl, uintptr(unsafe.Pointer(&value)))
  29. if err != nil {
  30. panic(err)
  31. }
  32. return value
  33. }
  34. func setTermios(termios syscall.Termios) {
  35. err := ioctl(fd, setTermiosIoctl, uintptr(unsafe.Pointer(&termios)))
  36. if err != nil {
  37. panic(err)
  38. }
  39. runtime.KeepAlive(termios)
  40. }
  41. func SetCharMode() {
  42. t := getTermios()
  43. t.Lflag = t.Lflag ^ syscall.ICANON
  44. t.Lflag = t.Lflag ^ syscall.ECHO
  45. setTermios(t)
  46. }
  47. func SetLineMode() {
  48. var t = getTermios()
  49. t.Lflag = t.Lflag | (syscall.ICANON | syscall.ECHO)
  50. setTermios(t)
  51. }