ascii.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package term
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // ASCII list the possible supported ASCII key sequence
  7. var ASCII = []string{
  8. "ctrl-@",
  9. "ctrl-a",
  10. "ctrl-b",
  11. "ctrl-c",
  12. "ctrl-d",
  13. "ctrl-e",
  14. "ctrl-f",
  15. "ctrl-g",
  16. "ctrl-h",
  17. "ctrl-i",
  18. "ctrl-j",
  19. "ctrl-k",
  20. "ctrl-l",
  21. "ctrl-m",
  22. "ctrl-n",
  23. "ctrl-o",
  24. "ctrl-p",
  25. "ctrl-q",
  26. "ctrl-r",
  27. "ctrl-s",
  28. "ctrl-t",
  29. "ctrl-u",
  30. "ctrl-v",
  31. "ctrl-w",
  32. "ctrl-x",
  33. "ctrl-y",
  34. "ctrl-z",
  35. "ctrl-[",
  36. "ctrl-\\",
  37. "ctrl-]",
  38. "ctrl-^",
  39. "ctrl-_",
  40. }
  41. // ToBytes converts a string representing a suite of key-sequence to the corresponding ASCII code.
  42. func ToBytes(keys string) ([]byte, error) {
  43. codes := []byte{}
  44. next:
  45. for _, key := range strings.Split(keys, ",") {
  46. if len(key) != 1 {
  47. for code, ctrl := range ASCII {
  48. if ctrl == key {
  49. codes = append(codes, byte(code))
  50. continue next
  51. }
  52. }
  53. if key == "DEL" {
  54. codes = append(codes, 127)
  55. } else {
  56. return nil, fmt.Errorf("Unknown character: '%s'", key)
  57. }
  58. } else {
  59. codes = append(codes, byte(key[0]))
  60. }
  61. }
  62. return codes, nil
  63. }