termcap.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package input
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "strings"
  6. )
  7. // CapabilityEvent represents a Termcap/Terminfo response event. Termcap
  8. // responses are generated by the terminal in response to RequestTermcap
  9. // (XTGETTCAP) requests.
  10. //
  11. // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
  12. type CapabilityEvent string
  13. func parseTermcap(data []byte) CapabilityEvent {
  14. // XTGETTCAP
  15. if len(data) == 0 {
  16. return CapabilityEvent("")
  17. }
  18. var tc strings.Builder
  19. split := bytes.Split(data, []byte{';'})
  20. for _, s := range split {
  21. parts := bytes.SplitN(s, []byte{'='}, 2)
  22. if len(parts) == 0 {
  23. return CapabilityEvent("")
  24. }
  25. name, err := hex.DecodeString(string(parts[0]))
  26. if err != nil || len(name) == 0 {
  27. continue
  28. }
  29. var value []byte
  30. if len(parts) > 1 {
  31. value, err = hex.DecodeString(string(parts[1]))
  32. if err != nil {
  33. continue
  34. }
  35. }
  36. if tc.Len() > 0 {
  37. tc.WriteByte(';')
  38. }
  39. tc.WriteString(string(name))
  40. if len(value) > 0 {
  41. tc.WriteByte('=')
  42. tc.WriteString(string(value))
  43. }
  44. }
  45. return CapabilityEvent(tc.String())
  46. }