| 1 |
package vt |
| 2 |
|
| 3 |
import ( |
| 4 |
"image/color" |
| 5 |
"sync" |
| 6 |
|
| 7 |
uv "github.com/charmbracelet/ultraviolet" |
| 8 |
) |
| 9 |
|
| 10 |
// SafeEmulator is a wrapper around an Emulator that adds concurrency safety. |
| 11 |
type SafeEmulator struct { |
| 12 |
*Emulator |
| 13 |
mu sync.RWMutex |
| 14 |
} |
| 15 |
|
| 16 |
// NewSafeEmulator creates a new SafeEmulator instance. |
| 17 |
func NewSafeEmulator(w, h int) *SafeEmulator { |
| 18 |
return &SafeEmulator{ |
| 19 |
Emulator: NewEmulator(w, h), |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// Write writes data to the emulator in a concurrency-safe manner. |
| 24 |
func (se *SafeEmulator) Write(data []byte) (int, error) { |
| 25 |
se.mu.Lock() |
| 26 |
defer se.mu.Unlock() |
| 27 |
return se.Emulator.Write(data) |
| 28 |
} |
| 29 |
|
| 30 |
// Read reads data from the emulator in a concurrency-safe manner. |
| 31 |
func (se *SafeEmulator) Read(p []byte) (int, error) { |
| 32 |
return se.Emulator.Read(p) |
| 33 |
} |
| 34 |
|
| 35 |
// Resize resizes the emulator in a concurrency-safe manner. |
| 36 |
func (se *SafeEmulator) Resize(w, h int) { |
| 37 |
se.mu.Lock() |
| 38 |
defer se.mu.Unlock() |
| 39 |
se.Emulator.Resize(w, h) |
| 40 |
} |
| 41 |
|
| 42 |
// Render renders the emulator's current state in a concurrency-safe manner. |
| 43 |
func (se *SafeEmulator) Render() string { |
| 44 |
se.mu.RLock() |
| 45 |
defer se.mu.RUnlock() |
| 46 |
return se.Emulator.Render() |
| 47 |
} |
| 48 |
|
| 49 |
// SetCell sets a cell in the emulator in a concurrency-safe manner. |
| 50 |
func (se *SafeEmulator) SetCell(x, y int, cell *uv.Cell) { |
| 51 |
se.mu.Lock() |
| 52 |
defer se.mu.Unlock() |
| 53 |
se.Emulator.SetCell(x, y, cell) |
| 54 |
} |
| 55 |
|
| 56 |
// CellAt retrieves a cell from the emulator in a concurrency-safe manner. |
| 57 |
func (se *SafeEmulator) CellAt(x, y int) *uv.Cell { |
| 58 |
se.mu.RLock() |
| 59 |
defer se.mu.RUnlock() |
| 60 |
return se.Emulator.CellAt(x, y) |
| 61 |
} |
| 62 |
|
| 63 |
// SendKey sends a key event to the emulator in a concurrency-safe manner. |
| 64 |
func (se *SafeEmulator) SendKey(key uv.KeyEvent) { |
| 65 |
se.mu.Lock() |
| 66 |
defer se.mu.Unlock() |
| 67 |
se.Emulator.SendKey(key) |
| 68 |
} |
| 69 |
|
| 70 |
// SendMouse sends a mouse event to the emulator in a concurrency-safe manner. |
| 71 |
func (se *SafeEmulator) SendMouse(mouse uv.MouseEvent) { |
| 72 |
se.mu.Lock() |
| 73 |
defer se.mu.Unlock() |
| 74 |
se.Emulator.SendMouse(mouse) |
| 75 |
} |
| 76 |
|
| 77 |
// SendText sends text input to the emulator in a concurrency-safe manner. |
| 78 |
func (se *SafeEmulator) SendText(text string) { |
| 79 |
se.mu.Lock() |
| 80 |
defer se.mu.Unlock() |
| 81 |
se.Emulator.SendText(text) |
| 82 |
} |
| 83 |
|
| 84 |
// Paste pastes text into the emulator in a concurrency-safe manner. |
| 85 |
func (se *SafeEmulator) Paste(text string) { |
| 86 |
se.mu.Lock() |
| 87 |
defer se.mu.Unlock() |
| 88 |
se.Emulator.Paste(text) |
| 89 |
} |
| 90 |
|
| 91 |
// SetForegroundColor sets the foreground color in a concurrency-safe manner. |
| 92 |
func (se *SafeEmulator) SetForegroundColor(color color.Color) { |
| 93 |
se.mu.Lock() |
| 94 |
defer se.mu.Unlock() |
| 95 |
se.Emulator.SetForegroundColor(color) |
| 96 |
} |
| 97 |
|
| 98 |
// SetBackgroundColor sets the background color in a concurrency-safe manner. |
| 99 |
func (se *SafeEmulator) SetBackgroundColor(color color.Color) { |
| 100 |
se.mu.Lock() |
| 101 |
defer se.mu.Unlock() |
| 102 |
se.Emulator.SetBackgroundColor(color) |
| 103 |
} |
| 104 |
|
| 105 |
// SetCursorColor sets the cursor color in a concurrency-safe manner. |
| 106 |
func (se *SafeEmulator) SetCursorColor(color color.Color) { |
| 107 |
se.mu.Lock() |
| 108 |
defer se.mu.Unlock() |
| 109 |
se.Emulator.SetCursorColor(color) |
| 110 |
} |
| 111 |
|
| 112 |
// SetIndexedColor sets an indexed color in a concurrency-safe manner. |
| 113 |
func (se *SafeEmulator) SetIndexedColor(index int, color color.Color) { |
| 114 |
se.mu.Lock() |
| 115 |
defer se.mu.Unlock() |
| 116 |
se.Emulator.SetIndexedColor(index, color) |
| 117 |
} |
| 118 |
|
| 119 |
// IndexedColor retrieves an indexed color in a concurrency-safe manner. |
| 120 |
func (se *SafeEmulator) IndexedColor(index int) color.Color { |
| 121 |
se.mu.RLock() |
| 122 |
defer se.mu.RUnlock() |
| 123 |
return se.Emulator.IndexedColor(index) |
| 124 |
} |
| 125 |
|
| 126 |
// Touched returns the touched lines in a concurrency-safe manner. |
| 127 |
func (se *SafeEmulator) Touched() []*uv.LineData { |
| 128 |
se.mu.RLock() |
| 129 |
defer se.mu.RUnlock() |
| 130 |
return se.Emulator.Touched() |
| 131 |
} |
| 132 |
|
| 133 |
// Height returns the height of the emulator in a concurrency-safe manner. |
| 134 |
func (se *SafeEmulator) Height() int { |
| 135 |
se.mu.RLock() |
| 136 |
defer se.mu.RUnlock() |
| 137 |
return se.Emulator.Height() |
| 138 |
} |
| 139 |
|
| 140 |
// Width returns the width of the emulator in a concurrency-safe manner. |
| 141 |
func (se *SafeEmulator) Width() int { |
| 142 |
se.mu.RLock() |
| 143 |
defer se.mu.RUnlock() |
| 144 |
return se.Emulator.Width() |
| 145 |
} |
| 146 |
|
| 147 |
// ForegroundColor returns the foreground color in a concurrency-safe manner. |
| 148 |
func (se *SafeEmulator) ForegroundColor() color.Color { |
| 149 |
se.mu.RLock() |
| 150 |
defer se.mu.RUnlock() |
| 151 |
return se.Emulator.ForegroundColor() |
| 152 |
} |
| 153 |
|
| 154 |
// BackgroundColor returns the background color in a concurrency-safe manner. |
| 155 |
func (se *SafeEmulator) BackgroundColor() color.Color { |
| 156 |
se.mu.RLock() |
| 157 |
defer se.mu.RUnlock() |
| 158 |
return se.Emulator.BackgroundColor() |
| 159 |
} |
| 160 |
|
| 161 |
// CursorColor returns the cursor color in a concurrency-safe manner. |
| 162 |
func (se *SafeEmulator) CursorColor() color.Color { |
| 163 |
se.mu.RLock() |
| 164 |
defer se.mu.RUnlock() |
| 165 |
return se.Emulator.CursorColor() |
| 166 |
} |
| 167 |
|
| 168 |
// CursorPosition returns the cursor position in a concurrency-safe manner. |
| 169 |
func (se *SafeEmulator) CursorPosition() uv.Position { |
| 170 |
se.mu.RLock() |
| 171 |
defer se.mu.RUnlock() |
| 172 |
return se.Emulator.CursorPosition() |
| 173 |
} |
| 174 |
|
| 175 |
// Draw draws the emulator's content onto a given surface in a concurrency-safe manner. |
| 176 |
func (se *SafeEmulator) Draw(s uv.Screen, a uv.Rectangle) { |
| 177 |
se.mu.RLock() |
| 178 |
defer se.mu.RUnlock() |
| 179 |
se.Emulator.Draw(s, a) |
| 180 |
} |