2024-05-18 20:09:27 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package wshutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
// these should both be 5 characters
|
2024-05-18 20:09:27 +02:00
|
|
|
const WaveOSC = "23198"
|
2024-06-17 18:58:28 +02:00
|
|
|
const WaveServerOSC = "23199"
|
|
|
|
const WaveOSCPrefixLen = 5 + 3 // \x1b] + WaveOSC + ; + \x07
|
|
|
|
|
2024-05-18 20:09:27 +02:00
|
|
|
const WaveOSCPrefix = "\x1b]" + WaveOSC + ";"
|
2024-06-17 18:58:28 +02:00
|
|
|
const WaveServerOSCPrefix = "\x1b]" + WaveServerOSC + ";"
|
2024-06-14 23:43:47 +02:00
|
|
|
|
2024-05-18 20:09:27 +02:00
|
|
|
const HexChars = "0123456789ABCDEF"
|
|
|
|
const BEL = 0x07
|
2024-06-13 23:41:28 +02:00
|
|
|
const ST = 0x9c
|
|
|
|
const ESC = 0x1b
|
2024-05-18 20:09:27 +02:00
|
|
|
|
|
|
|
// OSC escape types
|
|
|
|
// OSC 23198 ; (JSON | base64-JSON) ST
|
|
|
|
// JSON = must escape all ASCII control characters ([\x00-\x1F\x7F])
|
|
|
|
// we can tell the difference between JSON and base64-JSON by the first character: '{' or not
|
|
|
|
|
2024-06-14 23:43:47 +02:00
|
|
|
// for responses (terminal -> program), we'll use OSC 23199
|
|
|
|
// same json format
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
func copyOscPrefix(dst []byte, oscNum string) {
|
|
|
|
dst[0] = ESC
|
|
|
|
dst[1] = ']'
|
|
|
|
copy(dst[2:], oscNum)
|
|
|
|
dst[len(oscNum)+2] = ';'
|
|
|
|
}
|
|
|
|
|
|
|
|
func oscPrefixLen(oscNum string) int {
|
|
|
|
return 3 + len(oscNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeOscPrefix(oscNum string) []byte {
|
|
|
|
output := make([]byte, oscPrefixLen(oscNum))
|
|
|
|
copyOscPrefix(output, oscNum)
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
|
|
|
|
if len(oscNum) != 5 {
|
|
|
|
panic("oscNum must be 5 characters")
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|
|
|
|
hasControlChars := false
|
|
|
|
for _, b := range barr {
|
|
|
|
if b < 0x20 || b == 0x7F {
|
|
|
|
hasControlChars = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasControlChars {
|
|
|
|
// If no control characters, directly construct the output
|
|
|
|
// \x1b] (2) + WaveOSC + ; (1) + message + \x07 (1)
|
2024-06-17 18:58:28 +02:00
|
|
|
output := make([]byte, oscPrefixLen(oscNum)+len(barr)+1)
|
|
|
|
copyOscPrefix(output, oscNum)
|
|
|
|
copy(output[oscPrefixLen(oscNum):], barr)
|
2024-05-18 20:09:27 +02:00
|
|
|
output[len(output)-1] = BEL
|
2024-07-18 00:24:43 +02:00
|
|
|
return output
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2024-06-17 18:58:28 +02:00
|
|
|
buf.Write(makeOscPrefix(oscNum))
|
2024-05-18 20:09:27 +02:00
|
|
|
escSeq := [6]byte{'\\', 'u', '0', '0', '0', '0'}
|
|
|
|
for _, b := range barr {
|
|
|
|
if b < 0x20 || b == 0x7f {
|
|
|
|
escSeq[4] = HexChars[b>>4]
|
|
|
|
escSeq[5] = HexChars[b&0x0f]
|
|
|
|
buf.Write(escSeq[:])
|
|
|
|
} else {
|
|
|
|
buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteByte(BEL)
|
2024-07-18 00:24:43 +02:00
|
|
|
return buf.Bytes()
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:24:43 +02:00
|
|
|
func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
|
|
|
|
if msg == nil {
|
|
|
|
return nil, fmt.Errorf("nil message")
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
barr, err := json.Marshal(msg)
|
2024-05-18 20:09:27 +02:00
|
|
|
if err != nil {
|
2024-07-18 00:24:43 +02:00
|
|
|
return nil, fmt.Errorf("error marshalling message to json: %w", err)
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|
2024-07-18 00:24:43 +02:00
|
|
|
return EncodeWaveOSCBytes(oscNum, barr), nil
|
2024-05-18 20:09:27 +02:00
|
|
|
}
|