waveterm/pkg/wshutil/wshrpcio.go
Sylvie Crowe ff5f26709c
WSL Updates for New Architecture (#1756)
This adapts most of the WSL code to follow the new architecture that ssh
uses.

---------

Co-authored-by: sawka <mike@commandline.dev>
2025-01-16 15:54:58 -08:00

52 lines
1.3 KiB
Go

// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package wshutil
import (
"fmt"
"io"
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
)
// special I/O wrappers for wshrpc
// * terminal (wrap with OSC codes)
// * stream (json lines)
// * websocket (json packets)
func AdaptStreamToMsgCh(input io.Reader, output chan []byte) error {
return utilfn.StreamToLines(input, func(line []byte) {
output <- line
})
}
func AdaptOutputChToStream(outputCh chan []byte, output io.Writer) error {
for msg := range outputCh {
if _, err := output.Write(msg); err != nil {
return fmt.Errorf("error writing to output (AdaptOutputChToStream): %w", err)
}
// write trailing newline
if _, err := output.Write([]byte{'\n'}); err != nil {
return fmt.Errorf("error writing trailing newline to output (AdaptOutputChToStream): %w", err)
}
}
return nil
}
func AdaptMsgChToPty(outputCh chan []byte, oscEsc string, output io.Writer) error {
if len(oscEsc) != 5 {
panic("oscEsc must be 5 characters")
}
for msg := range outputCh {
barr, err := EncodeWaveOSCBytes(oscEsc, msg)
if err != nil {
return fmt.Errorf("error encoding osc message (AdaptMsgChToPty): %w", err)
}
if _, err := output.Write(barr); err != nil {
return fmt.Errorf("error writing osc message (AdaptMsgChToPty): %w", err)
}
}
return nil
}