File: write.go

package info (click to toggle)
kitty 0.45.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,476 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (232 lines) | stat: -rw-r--r-- 6,080 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package clipboard

import (
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"slices"
	"strings"

	"github.com/kovidgoyal/kitty/tools/tui/loop"
	"github.com/kovidgoyal/kitty/tools/utils"
)

var _ = fmt.Print

type Input struct {
	src              io.Reader
	arg              string
	ext              string
	is_stream        bool
	mime_type        string
	extra_mime_types []string
}

func is_textual_mime(x string) bool {
	return strings.HasPrefix(x, "text/") || utils.KnownTextualMimes[x]
}

func is_text_plain_mime(x string) bool {
	return x == "text/plain"
}

func (self *Input) has_mime_matching(predicate func(string) bool) bool {
	if predicate(self.mime_type) {
		return true
	}
	return slices.ContainsFunc(self.extra_mime_types, predicate)
}

func write_loop(inputs []*Input, opts *Options) (err error) {
	lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking, loop.NoInBandResizeNotifications)
	if err != nil {
		return err
	}
	var waiting_for_write loop.IdType
	var buf [4096]byte
	aliases, aerr := parse_aliases(opts.Alias)
	if aerr != nil {
		return aerr
	}
	num_text_mimes := 0
	has_text_plain := false
	for _, i := range inputs {
		i.extra_mime_types = aliases[i.mime_type]
		if i.has_mime_matching(is_textual_mime) {
			num_text_mimes++
			if !has_text_plain && i.has_mime_matching(is_text_plain_mime) {
				has_text_plain = true
			}
		}
	}
	if num_text_mimes > 0 && !has_text_plain {
		for _, i := range inputs {
			if i.has_mime_matching(is_textual_mime) {
				i.extra_mime_types = append(i.extra_mime_types, "text/plain")
				break
			}
		}
	}

	make_metadata := func(ptype, mime string) map[string]string {
		ans := map[string]string{"type": ptype}
		if opts.UsePrimary {
			ans["loc"] = "primary"
		}
		if mime != "" {
			ans["mime"] = mime
		}
		if ptype == "write" {
			if opts.Password != "" {
				ans["pw"] = base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(opts.Password))
			}
			if opts.HumanName != "" {
				ans["name"] = base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(opts.HumanName))
			}
		}
		return ans
	}

	lp.OnInitialize = func() (string, error) {
		waiting_for_write = lp.QueueWriteString(encode(make_metadata("write", ""), ""))
		return "", nil
	}

	write_chunk := func() error {
		if len(inputs) == 0 {
			return nil
		}
		i := inputs[0]
		n, err := i.src.Read(buf[:])
		if n > 0 {
			waiting_for_write = lp.QueueWriteString(Encode_bytes(make_metadata("wdata", i.mime_type), buf[:n]))
		}
		if err != nil {
			if errors.Is(err, io.EOF) {
				if len(i.extra_mime_types) > 0 {
					lp.QueueWriteString(encode(make_metadata("walias", i.mime_type), strings.Join(i.extra_mime_types, " ")))
				}
				inputs = inputs[1:]
				if len(inputs) == 0 {
					lp.QueueWriteString(encode(make_metadata("wdata", ""), ""))
					waiting_for_write = 0
				}
				return lp.OnWriteComplete(waiting_for_write, false)
			}
			return fmt.Errorf("Failed to read from %s with error: %w", i.arg, err)
		}
		return nil
	}

	lp.OnWriteComplete = func(msg_id loop.IdType, has_pending_writes bool) error {
		if waiting_for_write == msg_id {
			return write_chunk()
		}
		return nil
	}

	lp.OnEscapeCode = func(etype loop.EscapeCodeType, data []byte) (err error) {
		metadata, _, err := parse_escape_code(etype, data)
		if err != nil {
			return err
		}
		if metadata != nil && metadata["type"] == "write" {
			switch metadata["status"] {
			case "DONE":
				lp.Quit(0)
			case "EIO":
				return fmt.Errorf("Could not write to clipboard an I/O error occurred while the terminal was processing the data")
			case "EINVAL":
				return fmt.Errorf("Could not write to clipboard base64 encoding invalid")
			case "ENOSYS":
				return fmt.Errorf("Could not write to primary selection as the system does not support it")
			case "EPERM":
				return fmt.Errorf("Could not write to clipboard as permission was denied")
			case "EBUSY":
				return fmt.Errorf("Could not write to clipboard, a temporary error occurred, try again later.")
			default:
				return fmt.Errorf("Could not write to clipboard unknowns status returned from terminal: %#v", metadata["status"])
			}
		}
		return
	}

	esc_count := 0
	lp.OnKeyEvent = func(event *loop.KeyEvent) error {
		if event.MatchesPressOrRepeat("ctrl+c") || event.MatchesPressOrRepeat("esc") {
			event.Handled = true
			esc_count++
			if esc_count < 2 {
				key := "Esc"
				if event.MatchesPressOrRepeat("ctrl+c") {
					key = "Ctrl+C"
				}
				lp.QueueWriteString(fmt.Sprintf("Waiting for response from terminal, press %s again to abort. This could cause garbage to be spewed to the screen.\r\n", key))
			} else {
				return fmt.Errorf("Aborted by user!")
			}
		}
		return nil
	}

	err = lp.Run()
	if err != nil {
		return
	}
	ds := lp.DeathSignalName()
	if ds != "" {
		fmt.Println("Killed by signal: ", ds)
		lp.KillIfSignalled()
		return
	}

	return
}

func run_set_loop(opts *Options, args []string) (err error) {
	inputs := make([]*Input, len(args))
	to_process := make([]*Input, len(args))
	defer func() {
		for _, i := range inputs {
			if i != nil && i.src != nil {
				rc, ok := i.src.(io.Closer)
				if ok {
					rc.Close()
				}
			}
		}
	}()

	for i, arg := range args {
		if arg == "/dev/stdin" {
			f, _, err := preread_stdin()
			if err != nil {
				return err
			}
			inputs[i] = &Input{arg: arg, src: f, is_stream: true}
		} else {
			f, err := os.Open(arg)
			if err != nil {
				return fmt.Errorf("Failed to open %s with error: %w", arg, err)
			}
			inputs[i] = &Input{arg: arg, src: f, ext: filepath.Ext(arg)}
		}
		if i < len(opts.Mime) {
			inputs[i].mime_type = opts.Mime[i]
		} else if inputs[i].is_stream {
			inputs[i].mime_type = "text/plain"
		} else if inputs[i].ext != "" {
			inputs[i].mime_type = utils.GuessMimeType(inputs[i].arg)
		}
		if inputs[i].mime_type == "" {
			return fmt.Errorf("Could not guess MIME type for %s use the --mime option to specify a MIME type", arg)
		}
		to_process[i] = inputs[i]
	}
	return write_loop(to_process, opts)
}