feat: complete Go port with go-te terminal emulator
Full Go implementation under go/ replacing Python pyte with go-te: - HTTP server with WebSocket protocol, SSE, screenshot SVG rendering - PTY terminal sessions and Docker exec sessions - Docker watcher (label-based container discovery + event stream) - CPU stats collection with sparkline SVG rendering - Session manager with TwoWayMap routing and replay buffers - C1 normalization, DA filtering, identity generation, theme palettes Audit fixes for 9 concurrency/correctness issues: - HTTP transport leak: shared client pool for Docker socket calls - WebSocket concurrent writes: all writes routed through send channel - Closed channel panic: atomic.Bool guard on enqueueWSData - GetFirstRunningSession: use UnsafeForward under SessionManager lock - NewSession TOCTOU: re-check routeKey after re-acquiring lock - waitErr data race: protect with mutex in both session types - Replay buffer fragmentation: copy to new slice on eviction - go-te dirty tracking: check screen.Dirty before incrementing counter - Identity modulo bias: rejection sampling for uniform distribution All Go tests pass (including -race). Python baseline unchanged (397 tests). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package terminalstate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rcarmo/go-te/pkg/te"
|
||||
)
|
||||
|
||||
var ansi16Names = [...]string{
|
||||
"black",
|
||||
"red",
|
||||
"green",
|
||||
"yellow",
|
||||
"blue",
|
||||
"magenta",
|
||||
"cyan",
|
||||
"white",
|
||||
"brightblack",
|
||||
"brightred",
|
||||
"brightgreen",
|
||||
"brightyellow",
|
||||
"brightblue",
|
||||
"brightmagenta",
|
||||
"brightcyan",
|
||||
"brightwhite",
|
||||
}
|
||||
|
||||
type Cell struct {
|
||||
Data string `json:"data"`
|
||||
FG string `json:"fg"`
|
||||
BG string `json:"bg"`
|
||||
Bold bool `json:"bold"`
|
||||
Italics bool `json:"italics"`
|
||||
Underscore bool `json:"underscore"`
|
||||
Reverse bool `json:"reverse"`
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Buffer [][]Cell `json:"buffer"`
|
||||
HasChanges bool `json:"has_changes"`
|
||||
}
|
||||
|
||||
type Tracker struct {
|
||||
mu sync.Mutex
|
||||
screen *te.DiffScreen
|
||||
stream *te.ByteStream
|
||||
changeCounter uint64
|
||||
lastSnapshotCounter uint64
|
||||
}
|
||||
|
||||
func NewTracker(width, height int) *Tracker {
|
||||
screen := te.NewDiffScreen(width, height)
|
||||
return &Tracker{
|
||||
screen: screen,
|
||||
stream: te.NewByteStream(screen, false),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tracker) Feed(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if err := t.stream.Feed(data); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(t.screen.Dirty) > 0 {
|
||||
t.changeCounter++
|
||||
// Clear dirty set so subsequent feeds detect new changes
|
||||
for k := range t.screen.Dirty {
|
||||
delete(t.screen.Dirty, k)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Tracker) Resize(width, height int) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if width == t.screen.Columns && height == t.screen.Lines {
|
||||
return
|
||||
}
|
||||
t.screen.Resize(height, width)
|
||||
t.changeCounter++
|
||||
}
|
||||
|
||||
func (t *Tracker) Snapshot() Snapshot {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
snapshot := Snapshot{
|
||||
Width: t.screen.Columns,
|
||||
Height: t.screen.Lines,
|
||||
HasChanges: t.changeCounter > t.lastSnapshotCounter,
|
||||
Buffer: make([][]Cell, t.screen.Lines),
|
||||
}
|
||||
t.lastSnapshotCounter = t.changeCounter
|
||||
|
||||
for row := 0; row < t.screen.Lines; row++ {
|
||||
line := make([]Cell, t.screen.Columns)
|
||||
for col := 0; col < t.screen.Columns; col++ {
|
||||
raw := t.screen.Buffer[row][col]
|
||||
data := raw.Data
|
||||
if data == "" {
|
||||
data = " "
|
||||
}
|
||||
line[col] = Cell{
|
||||
Data: data,
|
||||
FG: colorToString(raw.Attr.Fg),
|
||||
BG: colorToString(raw.Attr.Bg),
|
||||
Bold: raw.Attr.Bold,
|
||||
Italics: raw.Attr.Italics,
|
||||
Underscore: raw.Attr.Underline,
|
||||
Reverse: raw.Attr.Reverse,
|
||||
}
|
||||
}
|
||||
snapshot.Buffer[row] = line
|
||||
}
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func colorToString(color te.Color) string {
|
||||
if color.Name != "" {
|
||||
name := strings.ToLower(strings.TrimPrefix(color.Name, "#"))
|
||||
if len(name) == 6 {
|
||||
if _, err := strconv.ParseUint(name, 16, 32); err == nil {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return name
|
||||
}
|
||||
switch color.Mode {
|
||||
case te.ColorDefault:
|
||||
return "default"
|
||||
case te.ColorANSI16:
|
||||
if int(color.Index) < len(ansi16Names) {
|
||||
return ansi16Names[color.Index]
|
||||
}
|
||||
return "default"
|
||||
case te.ColorANSI256:
|
||||
return fmt.Sprintf("%d", color.Index)
|
||||
case te.ColorTrueColor:
|
||||
return "default"
|
||||
default:
|
||||
return "default"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package terminalstate
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTrackerSnapshotChangeTracking(t *testing.T) {
|
||||
tracker := NewTracker(10, 3)
|
||||
if err := tracker.Feed([]byte("hi")); err != nil {
|
||||
t.Fatalf("Feed() error = %v", err)
|
||||
}
|
||||
|
||||
snapshot := tracker.Snapshot()
|
||||
if !snapshot.HasChanges {
|
||||
t.Fatalf("expected first snapshot to report changes")
|
||||
}
|
||||
if got := snapshot.Buffer[0][0].Data; got != "h" {
|
||||
t.Fatalf("expected first cell to be h, got %q", got)
|
||||
}
|
||||
if got := snapshot.Buffer[0][1].Data; got != "i" {
|
||||
t.Fatalf("expected second cell to be i, got %q", got)
|
||||
}
|
||||
|
||||
again := tracker.Snapshot()
|
||||
if again.HasChanges {
|
||||
t.Fatalf("expected second snapshot without new input to report no changes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackerAnsiStyles(t *testing.T) {
|
||||
tracker := NewTracker(10, 3)
|
||||
if err := tracker.Feed([]byte("\x1b[31;1mA\x1b[0m")); err != nil {
|
||||
t.Fatalf("Feed() error = %v", err)
|
||||
}
|
||||
snapshot := tracker.Snapshot()
|
||||
cell := snapshot.Buffer[0][0]
|
||||
if !cell.Bold {
|
||||
t.Fatalf("expected bold attribute to be true")
|
||||
}
|
||||
if cell.FG != "red" {
|
||||
t.Fatalf("expected red foreground, got %q", cell.FG)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackerResize(t *testing.T) {
|
||||
tracker := NewTracker(10, 3)
|
||||
tracker.Resize(20, 4)
|
||||
snapshot := tracker.Snapshot()
|
||||
if snapshot.Width != 20 || snapshot.Height != 4 {
|
||||
t.Fatalf("unexpected dimensions: got %dx%d", snapshot.Width, snapshot.Height)
|
||||
}
|
||||
if !snapshot.HasChanges {
|
||||
t.Fatalf("expected resize to mark snapshot as changed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user