send_text.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package at
  3. import (
  4. "encoding/base64"
  5. "errors"
  6. "io"
  7. "kitty/tools/tty"
  8. "kitty/tools/tui/loop"
  9. "kitty/tools/utils"
  10. "kitty/tools/utils/shlex"
  11. "os"
  12. "strings"
  13. )
  14. var end_reading_from_stdin = errors.New("end reading from STDIN")
  15. var waiting_on_stdin = errors.New("wait for key events from STDIN")
  16. func make_file_gen(f *os.File) func(*rc_io_data) (bool, error) {
  17. chunk := make([]byte, 2048)
  18. file_gen := func(io_data *rc_io_data) (bool, error) {
  19. n, err := f.Read(chunk)
  20. if err != nil && !errors.Is(err, io.EOF) {
  21. return false, err
  22. }
  23. set_payload_data(io_data, "base64:"+base64.StdEncoding.EncodeToString(chunk[:n]))
  24. return n == 0 || errors.Is(err, io.EOF), nil
  25. }
  26. return file_gen
  27. }
  28. func parse_send_text(io_data *rc_io_data, args []string) error {
  29. generators := make([]func(io_data *rc_io_data) (bool, error), 0, 1)
  30. if len(args) > 0 {
  31. for i, arg := range args {
  32. args[i] = shlex.ExpandANSICEscapes(arg)
  33. }
  34. text := strings.Join(args, " ")
  35. text_gen := func(io_data *rc_io_data) (bool, error) {
  36. limit := len(text)
  37. if limit > 2048 {
  38. limit = 2048
  39. }
  40. set_payload_data(io_data, "base64:"+base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(text[:limit])))
  41. text = text[limit:]
  42. return len(text) == 0, nil
  43. }
  44. generators = append(generators, text_gen)
  45. }
  46. if options_send_text.FromFile != "" {
  47. f, err := os.Open(options_send_text.FromFile)
  48. if err != nil {
  49. return err
  50. }
  51. generators = append(generators, make_file_gen(f))
  52. }
  53. if options_send_text.Stdin {
  54. if tty.IsTerminal(os.Stdin.Fd()) {
  55. pending_key_events := make([]string, 0, 1)
  56. io_data.on_key_event = func(lp *loop.Loop, ke *loop.KeyEvent) error {
  57. ke.Handled = true
  58. if ke.MatchesPressOrRepeat("ctrl+d") {
  59. return end_reading_from_stdin
  60. }
  61. bs := "kitty-key:" + base64.StdEncoding.EncodeToString([]byte(ke.AsCSI()))
  62. pending_key_events = append(pending_key_events, bs)
  63. if ke.Text != "" {
  64. lp.QueueWriteString(ke.Text)
  65. } else if ke.MatchesPressOrRepeat("backspace") {
  66. lp.QueueWriteString("\x08\x1b[P")
  67. }
  68. return nil
  69. }
  70. key_gen := func(io_data *rc_io_data) (bool, error) {
  71. if len(pending_key_events) > 0 {
  72. payload := io_data.rc.Payload.(send_text_json_type)
  73. payload.Exclude_active = true
  74. io_data.rc.Payload = payload
  75. set_payload_data(io_data, pending_key_events[0])
  76. pending_key_events = pending_key_events[1:]
  77. return false, nil
  78. }
  79. return false, waiting_on_stdin
  80. }
  81. generators = append(generators, key_gen)
  82. } else {
  83. generators = append(generators, make_file_gen(os.Stdin))
  84. }
  85. }
  86. io_data.multiple_payload_generator = func(io_data *rc_io_data) (bool, error) {
  87. if len(generators) == 0 {
  88. set_payload_data(io_data, "text:")
  89. return true, nil
  90. }
  91. finished, err := generators[0](io_data)
  92. if finished {
  93. generators = generators[1:]
  94. finished = len(generators) == 0
  95. }
  96. return finished, err
  97. }
  98. return nil
  99. }