1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! CLI output capture for recording terminal sessions.
//!
//! `CliCapture` wraps a `Write` implementation to capture all bytes written
//! to stdout/stderr while forwarding them to the underlying writer. This
//! enables transparent recording without changing calling code.
use ralph_proto::{FrameCapture, TerminalWrite, UxEvent};
use std::io::{self, Write};
use std::time::Instant;
/// A writer that captures all output while forwarding to an inner writer.
///
/// This wrapper implements `std::io::Write` and records every write operation
/// as a `UxEvent::TerminalWrite`. The captured events can be retrieved via
/// the `FrameCapture` trait for session recording.
///
/// # Example
///
/// ```
/// use ralph_core::CliCapture;
/// use ralph_proto::FrameCapture;
/// use std::io::Write;
///
/// let mut output = Vec::new();
/// let mut capture = CliCapture::new(&mut output, true);
///
/// writeln!(capture, "Hello, World!").unwrap();
///
/// // And captured as UX events
/// let events = capture.take_captures();
/// assert_eq!(events.len(), 1);
///
/// // Output was forwarded to the inner writer (checked after capture drops borrow)
/// drop(capture);
/// assert!(String::from_utf8_lossy(&output).contains("Hello"));
/// ```
pub struct CliCapture<W> {
/// The underlying writer to forward output to.
inner: W,
/// Captured UX events.
captures: Vec<UxEvent>,
/// Time when capture started, for calculating offsets.
start_time: Instant,
/// Whether this captures stdout (true) or stderr (false).
is_stdout: bool,
}
impl<W> CliCapture<W> {
/// Creates a new capture wrapper around the given writer.
///
/// # Arguments
///
/// * `inner` - The writer to forward output to
/// * `is_stdout` - `true` if capturing stdout, `false` for stderr
pub fn new(inner: W, is_stdout: bool) -> Self {
Self {
inner,
captures: Vec::new(),
start_time: Instant::now(),
is_stdout,
}
}
/// Creates a capture wrapper with a custom start time.
///
/// This is useful when coordinating multiple captures (stdout + stderr)
/// that should share the same timing baseline.
pub fn with_start_time(inner: W, is_stdout: bool, start_time: Instant) -> Self {
Self {
inner,
captures: Vec::new(),
start_time,
is_stdout,
}
}
/// Returns the current offset in milliseconds since capture started.
#[allow(clippy::cast_possible_truncation)]
fn offset_ms(&self) -> u64 {
// Safe: milliseconds since start won't exceed u64 in practice
self.start_time.elapsed().as_millis() as u64
}
/// Returns a reference to the inner writer.
pub fn inner(&self) -> &W {
&self.inner
}
/// Returns a mutable reference to the inner writer.
pub fn inner_mut(&mut self) -> &mut W {
&mut self.inner
}
/// Consumes the capture and returns the inner writer.
pub fn into_inner(self) -> W {
self.inner
}
}
impl<W: Write> Write for CliCapture<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// Forward to inner writer first
let n = self.inner.write(buf)?;
// Only capture the bytes that were actually written
if n > 0 {
self.captures
.push(UxEvent::TerminalWrite(TerminalWrite::new(
&buf[..n],
self.is_stdout,
self.offset_ms(),
)));
}
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
impl<W: Send + Sync> FrameCapture for CliCapture<W> {
fn take_captures(&mut self) -> Vec<UxEvent> {
std::mem::take(&mut self.captures)
}
fn has_captures(&self) -> bool {
!self.captures.is_empty()
}
}
/// A pair of capture wrappers for stdout and stderr.
///
/// This struct coordinates captures for both streams with a shared start time,
/// ensuring timing offsets are consistent across stdout and stderr events.
pub struct CliCapturePair<Stdout, Stderr> {
/// Capture wrapper for stdout.
pub stdout: CliCapture<Stdout>,
/// Capture wrapper for stderr.
pub stderr: CliCapture<Stderr>,
}
impl<Stdout, Stderr> CliCapturePair<Stdout, Stderr> {
/// Creates a new capture pair with a shared start time.
pub fn new(stdout: Stdout, stderr: Stderr) -> Self {
let start_time = Instant::now();
Self {
stdout: CliCapture::with_start_time(stdout, true, start_time),
stderr: CliCapture::with_start_time(stderr, false, start_time),
}
}
}
impl<Stdout: Send + Sync, Stderr: Send + Sync> CliCapturePair<Stdout, Stderr> {
/// Takes all captured events from both streams, merged in chronological order.
pub fn take_all_captures(&mut self) -> Vec<UxEvent> {
let mut stdout_events = self.stdout.take_captures();
let mut stderr_events = self.stderr.take_captures();
// Merge and sort by offset_ms
stdout_events.append(&mut stderr_events);
stdout_events.sort_by_key(|event| match event {
UxEvent::TerminalWrite(tw) => tw.offset_ms,
UxEvent::TerminalResize(tr) => tr.offset_ms,
UxEvent::TerminalColorMode(cm) => cm.offset_ms,
UxEvent::TuiFrame(tf) => tf.offset_ms,
});
stdout_events
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_capture_write() {
let mut output = Vec::new();
// Scope the capture so we can check output afterward
let events = {
let mut capture = CliCapture::new(&mut output, true);
write!(capture, "Hello").unwrap();
capture.take_captures()
};
// Check output was forwarded
assert_eq!(output, b"Hello");
// Check event was captured
assert_eq!(events.len(), 1);
if let UxEvent::TerminalWrite(tw) = &events[0] {
assert!(tw.stdout);
let decoded = tw.decode_bytes().unwrap();
assert_eq!(decoded, b"Hello");
} else {
panic!("Expected TerminalWrite event");
}
}
#[test]
fn test_capture_multiple_writes() {
let mut output = Vec::new();
let mut capture = CliCapture::new(&mut output, true);
writeln!(capture, "Line 1").unwrap();
writeln!(capture, "Line 2").unwrap();
let events = capture.take_captures();
assert_eq!(events.len(), 2);
// Check offsets are monotonic
if let (UxEvent::TerminalWrite(tw1), UxEvent::TerminalWrite(tw2)) = (&events[0], &events[1])
{
assert!(tw2.offset_ms >= tw1.offset_ms);
}
}
#[test]
fn test_capture_stderr() {
let mut output = Vec::new();
let mut capture = CliCapture::new(&mut output, false);
write!(capture, "Error!").unwrap();
let events = capture.take_captures();
if let UxEvent::TerminalWrite(tw) = &events[0] {
assert!(!tw.stdout); // stderr
}
}
#[test]
fn test_capture_pair() {
let stdout_buf = Vec::new();
let stderr_buf = Vec::new();
let mut pair = CliCapturePair::new(stdout_buf, stderr_buf);
write!(pair.stdout, "out").unwrap();
write!(pair.stderr, "err").unwrap();
let events = pair.take_all_captures();
assert_eq!(events.len(), 2);
}
#[test]
fn test_take_captures_clears_buffer() {
let mut output = Vec::new();
let mut capture = CliCapture::new(&mut output, true);
write!(capture, "test").unwrap();
assert!(capture.has_captures());
let events = capture.take_captures();
assert_eq!(events.len(), 1);
// Buffer should be cleared
assert!(!capture.has_captures());
assert!(capture.take_captures().is_empty());
}
#[test]
fn test_ansi_preservation() {
let mut output = Vec::new();
let mut capture = CliCapture::new(&mut output, true);
// Write ANSI escape sequence for green text
let ansi_text = b"\x1b[32mGreen\x1b[0m";
capture.write_all(ansi_text).unwrap();
let events = capture.take_captures();
if let UxEvent::TerminalWrite(tw) = &events[0] {
let decoded = tw.decode_bytes().unwrap();
assert_eq!(decoded, ansi_text);
}
}
}