Debian Codegraph @

vt/osc.go

raw
1 // Package vt provides a virtual terminal implementation.
2 // SKIP: Fix typecheck errors - function signature mismatches and undefined types
3 package vt
4
5 import (
6 "bytes"
7 "image/color"
8 "io"
9
10 "github.com/charmbracelet/x/ansi"
11 )
12
13 // handleOsc handles an OSC escape sequence.
14 func (e *Emulator) handleOsc(cmd int, data []byte) {
15 e.flushGrapheme() // Flush any pending grapheme before handling OSC sequences.
16 if !e.handlers.handleOsc(cmd, data) {
17 e.logf("unhandled sequence: OSC %q", data)
18 }
19 }
20
21 func (e *Emulator) handleTitle(cmd int, data []byte) {
22 parts := bytes.Split(data, []byte{';'})
23 if len(parts) != 2 {
24 // Invalid, ignore
25 return
26 }
27 switch cmd {
28 case 0: // Set window title and icon name
29 name := string(parts[1])
30 e.iconName, e.title = name, name
31 if e.cb.Title != nil {
32 e.cb.Title(name)
33 }
34 if e.cb.IconName != nil {
35 e.cb.IconName(name)
36 }
37 case 1: // Set icon name
38 name := string(parts[1])
39 e.iconName = name
40 if e.cb.IconName != nil {
41 e.cb.IconName(name)
42 }
43 case 2: // Set window title
44 name := string(parts[1])
45 e.title = name
46 if e.cb.Title != nil {
47 e.cb.Title(name)
48 }
49 }
50 }
51
52 func (e *Emulator) handleDefaultColor(cmd int, data []byte) {
53 if cmd != 10 && cmd != 11 && cmd != 12 &&
54 cmd != 110 && cmd != 111 && cmd != 112 {
55 // Invalid, ignore
56 return
57 }
58
59 parts := bytes.Split(data, []byte{';'})
60 if len(parts) == 0 {
61 // Invalid, ignore
62 return
63 }
64
65 cb := func(c color.Color) {
66 switch cmd {
67 case 10, 110: // Foreground color
68 e.SetForegroundColor(c)
69 case 11, 111: // Background color
70 e.SetBackgroundColor(c)
71 case 12, 112: // Cursor color
72 e.SetCursorColor(c)
73 }
74 }
75
76 switch len(parts) {
77 case 1: // Reset color
78 cb(nil)
79 case 2: // Set/Query color
80 arg := string(parts[1])
81 if arg == "?" {
82 var xrgb ansi.XRGBColor
83 switch cmd {
84 case 10: // Query foreground color
85 xrgb.Color = e.ForegroundColor()
86 if xrgb.Color != nil {
87 io.WriteString(e.pw, ansi.SetForegroundColor(xrgb.String())) //nolint:errcheck,gosec
88 }
89 case 11: // Query background color
90 xrgb.Color = e.BackgroundColor()
91 if xrgb.Color != nil {
92 io.WriteString(e.pw, ansi.SetBackgroundColor(xrgb.String())) //nolint:errcheck,gosec
93 }
94 case 12: // Query cursor color
95 xrgb.Color = e.CursorColor()
96 if xrgb.Color != nil {
97 io.WriteString(e.pw, ansi.SetCursorColor(xrgb.String())) //nolint:errcheck,gosec
98 }
99 }
100 } else if c := ansi.XParseColor(arg); c != nil {
101 cb(c)
102 }
103 }
104 }
105
106 func (e *Emulator) handleWorkingDirectory(cmd int, data []byte) {
107 if cmd != 7 {
108 // Invalid, ignore
109 return
110 }
111
112 // The data is the working directory path.
113 parts := bytes.Split(data, []byte{';'})
114 if len(parts) != 2 {
115 // Invalid, ignore
116 return
117 }
118
119 path := string(parts[1])
120 e.cwd = path
121
122 if e.cb.WorkingDirectory != nil {
123 e.cb.WorkingDirectory(path)
124 }
125 }
126
127 func (e *Emulator) handleHyperlink(cmd int, data []byte) {
128 parts := bytes.Split(data, []byte{';'})
129 if len(parts) != 3 || cmd != 8 {
130 // Invalid, ignore
131 return
132 }
133
134 e.scr.cur.Link.URL = string(parts[1])
135 e.scr.cur.Link.Params = string(parts[2])
136 }