File: osc.go

package info (click to toggle)
golang-sourcehut-rockorager-tcell-term 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,216 kB
  • sloc: makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,116 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
package tcellterm

import (
	"strings"
)

func (vt *VT) osc(data string) {
	selector, val, found := cutString(data, ";")
	if !found {
		return
	}
	switch selector {
	case "0", "2":
		ev := &EventTitle{
			EventTerminal: newEventTerminal(vt),
			title:         val,
		}
		vt.postEvent(ev)
	case "8":
		if vt.OSC8 {
			url, id := osc8(val)
			vt.cursor.attrs = vt.cursor.attrs.Url(url)
			vt.cursor.attrs = vt.cursor.attrs.UrlId(id)
		}
	}
}

// parses an osc8 payload into the URL and optional ID
func osc8(val string) (string, string) {
	// OSC 8 ; params ; url ST
	// params: key1=value1:key2=value2
	var id string
	params, url, found := cutString(val, ";")
	if !found {
		return "", ""
	}
	for _, param := range strings.Split(params, ":") {
		key, val, found := cutString(param, "=")
		if !found {
			continue
		}
		switch key {
		case "id":
			id = val
		}
	}
	return url, id
}

// Copied from stdlib to here for go 1.16 compat
func cutString(s string, sep string) (before string, after string, found bool) {
	if i := strings.Index(s, sep); i >= 0 {
		return s[:i], s[i+len(sep):], true
	}
	return s, "", false
}