File: main.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 (352 lines) | stat: -rw-r--r-- 8,992 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package notify

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"image"
	"io"
	"os"
	"slices"
	"strconv"
	"strings"
	"time"

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

var _ = fmt.Print

const ESC_CODE_PREFIX = "\x1b]99;"
const ESC_CODE_SUFFIX = "\x1b\\"
const CHUNK_SIZE = 4096

func b64encode(x string) string {
	return base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(x))
}

func check_id_valid(x string) bool {
	pat := utils.MustCompile(`[^a-zA-Z0-9_+.-]`)
	return pat.ReplaceAllString(x, "") == x
}

type parsed_data struct {
	opts                    *Options
	wait_till_closed        bool
	expire_time             time.Duration
	title, body, identifier string
	image_data              []byte
	initial_msg             string
}

func (p *parsed_data) create_metadata() string {
	ans := []string{}
	if p.opts.AppName != "" {
		ans = append(ans, "f="+b64encode(p.opts.AppName))
	}
	switch p.opts.Urgency {
	case "low":
		ans = append(ans, "u=0")
	case "critical":
		ans = append(ans, "u=2")
	}
	if p.expire_time >= 0 {
		ans = append(ans, "w="+strconv.FormatInt(p.expire_time.Milliseconds(), 10))
	}
	if p.opts.Type != "" {
		ans = append(ans, "t="+b64encode(p.opts.Type))
	}
	if p.wait_till_closed {
		ans = append(ans, "c=1:a=report")
	}
	for _, x := range p.opts.Icon {
		ans = append(ans, "n="+b64encode(x))
	}
	if p.opts.IconCacheId != "" {
		ans = append(ans, "g="+p.opts.IconCacheId)
	}
	if p.opts.SoundName != "system" {
		ans = append(ans, "s="+b64encode(p.opts.SoundName))
	}
	m := strings.Join(ans, ":")
	if m != "" {
		m = ":" + m
	}
	return m
}

var debugprintln = tty.DebugPrintln

func (p *parsed_data) generate_chunks(callback func(string)) {
	prefix := ESC_CODE_PREFIX + "i=" + p.identifier
	write_chunk := func(middle string) {
		callback(prefix + middle + ESC_CODE_SUFFIX)
	}

	add_payload := func(payload_type, payload string) {
		if payload == "" {
			return
		}
		p := utils.IfElse(payload_type == "title", "", ":p="+payload_type)
		payload = b64encode(payload)
		for len(payload) > 0 {
			chunk := payload[:min(CHUNK_SIZE, len(payload))]
			payload = utils.IfElse(len(payload) > len(chunk), payload[len(chunk):], "")
			write_chunk(":d=0:e=1" + p + ";" + chunk)
		}
	}
	metadata := p.create_metadata()
	write_chunk(":d=0" + metadata + ";")
	add_payload("title", p.title)
	add_payload("body", p.body)
	if len(p.image_data) > 0 {
		add_payload("icon", utils.UnsafeBytesToString(p.image_data))
	}
	if len(p.opts.Button) > 0 {
		add_payload("buttons", strings.Join(p.opts.Button, "\u2028"))
	}
	write_chunk(";")
}

func (p *parsed_data) run_loop() (err error) {
	lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking, loop.NoInBandResizeNotifications)
	if err != nil {
		return err
	}
	activated := -1
	prefix := ESC_CODE_PREFIX + "i=" + p.identifier

	poll_for_close := func() {
		lp.AddTimer(time.Millisecond*50, false, func(_ loop.IdType) error {
			lp.QueueWriteString(prefix + ":p=alive;" + ESC_CODE_SUFFIX)
			return nil
		})
	}
	lp.OnInitialize = func() (string, error) {
		if p.initial_msg != "" {
			return p.initial_msg, nil
		}
		p.generate_chunks(func(x string) { lp.QueueWriteString(x) })
		return "", nil
	}
	lp.OnEscapeCode = func(ect loop.EscapeCodeType, data []byte) error {
		if ect == loop.OSC && bytes.HasPrefix(data, []byte(ESC_CODE_PREFIX[2:])) {
			raw := utils.UnsafeBytesToString(data[len(ESC_CODE_PREFIX[2:]):])
			metadata, payload, _ := strings.Cut(raw, ";")
			sent_identifier, payload_type := "", ""
			for x := range strings.SplitSeq(metadata, ":") {
				key, val, _ := strings.Cut(x, "=")
				switch key {
				case "i":
					sent_identifier = val
				case "p":
					payload_type = val
				}
			}
			if sent_identifier == p.identifier {
				switch payload_type {
				case "close":
					if payload == "untracked" {
						poll_for_close()
					} else {
						lp.Quit(0)
					}
				case "alive":
					live_ids := strings.Split(payload, ",")
					if slices.Contains(live_ids, p.identifier) {
						poll_for_close()
					} else {
						lp.Quit(0)
					}
				case "":
					if activated, err = strconv.Atoi(utils.IfElse(payload == "", "0", payload)); err != nil {
						return fmt.Errorf("Got invalid activation response from terminal: %#v", payload)
					}
				}
			}
		}
		return nil
	}
	close_requested := 0
	lp.OnKeyEvent = func(event *loop.KeyEvent) error {
		if event.MatchesPressOrRepeat("ctrl+c") || event.MatchesPressOrRepeat("esc") {
			event.Handled = true
			switch close_requested {
			case 0:
				lp.QueueWriteString(prefix + ":p=close;" + ESC_CODE_SUFFIX)
				lp.Println("Closing notification, please wait...")
				close_requested++
			case 1:
				key := "Esc"
				if event.MatchesPressOrRepeat("ctrl+c") {
					key = "Ctrl+C"
				}
				lp.Println(fmt.Sprintf("Waiting for response from terminal, press the %s key again to abort. Note that this might result in garbage being printed to the terminal.", key))
				close_requested++
			default:
				return fmt.Errorf("Aborted by user!")
			}
		}
		return nil
	}

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

func random_ident() (string, error) {
	return utils.HumanUUID4()
}

func parse_duration(x string) (ans time.Duration, err error) {
	switch x {
	case "never":
		return 0, nil
	case "":
		return -1, nil
	}
	trailer := x[len(x)-1]
	multipler := time.Second
	switch trailer {
	case 's':
		x = x[:len(x)-1]
	case 'm':
		x = x[:len(x)-1]
		multipler = time.Minute
	case 'h':
		x = x[:len(x)-1]
		multipler = time.Hour
	case 'd':
		x = x[:len(x)-1]
		multipler = time.Hour * 24
	}
	val, err := strconv.ParseFloat(x, 64)
	if err != nil {
		return ans, err
	}
	ans = time.Duration(float64(multipler) * val)
	return
}

func (p *parsed_data) load_image_data() (err error) {
	if p.opts.IconPath == "" {
		return nil
	}
	f, err := os.Open(p.opts.IconPath)
	if err != nil {
		return err
	}
	defer f.Close()
	_, imgfmt, err := image.DecodeConfig(f)
	if _, err = f.Seek(0, io.SeekStart); err != nil {
		return err
	}
	if err == nil && imgfmt != "" && strings.Contains("jpeg jpg gif png", strings.ToLower(imgfmt)) {
		p.image_data, err = io.ReadAll(f)
		return
	}
	return fmt.Errorf("The icon must be in PNG, JPEG or GIF formats")
}

func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
	if len(args) == 0 {
		return 1, fmt.Errorf("Must specify a TITLE for the notification")
	}
	var p parsed_data
	p.opts = opts
	p.title = args[0]
	if len(args) > 1 {
		p.body = strings.Join(args[1:], " ")
	}
	ident := opts.Identifier
	if ident == "" {
		if ident, err = random_ident(); err != nil {
			return 1, fmt.Errorf("Failed to generate a random identifier with error: %w", err)
		}
	}
	bad_ident := func(which string) error {
		return fmt.Errorf("Invalid identifier: %s must be only English letters, numbers, hyphens and underscores.", which)
	}
	if !check_id_valid(ident) {
		return 1, bad_ident(ident)
	}
	p.identifier = ident
	if !check_id_valid(opts.IconCacheId) {
		return 1, bad_ident(opts.IconCacheId)
	}
	if len(p.title) == 0 {
		if ident == "" {
			return 1, fmt.Errorf("Must specify a non-empty TITLE for the notification or specify an identifier to close a notification.")
		}
		msg := ESC_CODE_PREFIX + "i=" + ident + ":p=close;" + ESC_CODE_SUFFIX
		if opts.OnlyPrintEscapeCode {
			_, err = os.Stdout.WriteString(msg)
		} else if p.wait_till_closed {
			p.initial_msg = msg
			err = p.run_loop()
		} else {
			var term *tty.Term
			if term, err = tty.OpenControllingTerm(); err != nil {
				return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", err)
			}
			if _, err = term.WriteString(msg); err != nil {
				term.RestoreAndClose()
				return 1, err
			}
			term.RestoreAndClose()
		}
	}
	if p.expire_time, err = parse_duration(opts.ExpireAfter); err != nil {
		return 1, fmt.Errorf("Invalid expire time: %s with error: %w", opts.ExpireAfter, err)
	}
	p.wait_till_closed = opts.WaitTillClosed
	if err = p.load_image_data(); err != nil {
		return 1, fmt.Errorf("Failed to load image data from %s with error %w", opts.IconPath, err)
	}
	if opts.OnlyPrintEscapeCode {
		p.generate_chunks(func(x string) {
			if err == nil {
				_, err = os.Stdout.WriteString(x)
			}
		})
	} else {
		if opts.PrintIdentifier {
			fmt.Println(ident)
		}
		if p.wait_till_closed {
			err = p.run_loop()
		} else {
			var term *tty.Term
			if term, err = tty.OpenControllingTerm(); err != nil {
				return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", err)
			}
			p.generate_chunks(func(x string) {
				if err == nil {
					_, err = term.WriteString(x)
				}
			})
			term.RestoreAndClose()
		}

	}
	if err != nil {
		rc = 1
	}
	return
}

func EntryPoint(parent *cli.Command) {
	create_cmd(parent, main)
}