ftui-runtime 0.4.0

Elm-style runtime loop and subscriptions for FrankenTUI.
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#![forbid(unsafe_code)]

//! Asciicast v2 recorder for capturing terminal sessions.
//!
//! This recorder writes newline-delimited JSON (NDJSON) compatible with
//! asciinema-player. The first line is the header object, followed by event
//! arrays of the form `[time, "o", "text"]` for output and `[time, "i", "text"]`
//! for input (optional).
//!
//! # Example
//! ```no_run
//! use ftui_core::terminal_capabilities::TerminalCapabilities;
//! use ftui_runtime::asciicast::{AsciicastRecorder, AsciicastWriter};
//! use ftui_runtime::{ScreenMode, TerminalWriter, UiAnchor};
//! use std::io::Cursor;
//!
//! let recorder = AsciicastRecorder::with_writer(Cursor::new(Vec::new()), 80, 24, 0).unwrap();
//! let output = Cursor::new(Vec::new());
//! let recording_output = AsciicastWriter::new(output, recorder);
//! let caps = TerminalCapabilities::detect();
//! let mut writer = TerminalWriter::new(recording_output, ScreenMode::Inline { ui_height: 10 }, UiAnchor::Bottom, caps);
//! writer.write_log("hello\n").unwrap();
//! ```

use std::fmt::Write as FmtWrite;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};
use web_time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use tracing::{info, trace};

/// Records terminal output in asciicast v2 format.
#[derive(Debug)]
pub struct AsciicastRecorder<W: Write> {
    output: W,
    start: Instant,
    width: u16,
    height: u16,
    event_count: u64,
    path: Option<PathBuf>,
}

impl AsciicastRecorder<BufWriter<File>> {
    /// Create a recorder that writes to a file at `path`.
    pub fn new(path: &Path, width: u16, height: u16) -> io::Result<Self> {
        let file = File::create(path)?;
        let writer = BufWriter::new(file);
        let timestamp = unix_timestamp()?;
        let recorder =
            AsciicastRecorder::build(writer, width, height, timestamp, Some(path.to_path_buf()))?;
        info!(
            path = ?path,
            width = width,
            height = height,
            "Asciicast recording started"
        );
        Ok(recorder)
    }
}

impl<W: Write> AsciicastRecorder<W> {
    /// Create a recorder that writes to the provided writer.
    ///
    /// `timestamp` is seconds since UNIX epoch used in the asciicast header.
    pub fn with_writer(output: W, width: u16, height: u16, timestamp: i64) -> io::Result<Self> {
        let recorder = Self::build(output, width, height, timestamp, None)?;
        info!(
            width = width,
            height = height,
            timestamp = timestamp,
            "Asciicast recording started"
        );
        Ok(recorder)
    }

    /// Record terminal output bytes.
    pub fn record_output(&mut self, data: &[u8]) -> io::Result<()> {
        self.record_event("o", data)
    }

    /// Record terminal input bytes (optional).
    pub fn record_input(&mut self, data: &[u8]) -> io::Result<()> {
        self.record_event("i", data)
    }

    /// Number of events recorded so far.
    #[must_use]
    pub const fn event_count(&self) -> u64 {
        self.event_count
    }

    /// Elapsed duration since recording started.
    #[must_use]
    pub fn duration(&self) -> Duration {
        self.start.elapsed()
    }

    /// Returns the terminal width recorded in the asciicast header.
    #[must_use]
    pub const fn width(&self) -> u16 {
        self.width
    }

    /// Returns the terminal height recorded in the asciicast header.
    #[must_use]
    pub const fn height(&self) -> u16 {
        self.height
    }

    /// Flush output and return the inner writer.
    pub fn finish(mut self) -> io::Result<W> {
        let duration = self.start.elapsed().as_secs_f64();
        self.output.flush()?;
        if let Some(path) = &self.path {
            info!(
                path = ?path,
                duration_secs = duration,
                events = self.event_count,
                "Asciicast recording complete"
            );
        } else {
            info!(
                duration_secs = duration,
                events = self.event_count,
                "Asciicast recording complete"
            );
        }
        Ok(self.output)
    }

    fn record_event(&mut self, kind: &str, data: &[u8]) -> io::Result<()> {
        let time = self.start.elapsed().as_secs_f64();
        let text = String::from_utf8_lossy(data);
        let escaped = escape_json(&text);
        writeln!(self.output, "[{:.6},\"{}\",\"{}\"]", time, kind, escaped)?;
        self.event_count += 1;
        trace!(
            bytes = data.len(),
            elapsed_secs = time,
            kind = kind,
            "Output recorded"
        );
        Ok(())
    }

    fn build(
        mut output: W,
        width: u16,
        height: u16,
        timestamp: i64,
        path: Option<PathBuf>,
    ) -> io::Result<Self> {
        write_header(&mut output, width, height, timestamp)?;
        Ok(Self {
            output,
            start: Instant::now(),
            width,
            height,
            event_count: 0,
            path,
        })
    }
}

/// Writer that mirrors terminal output into an asciicast recorder.
#[derive(Debug)]
pub struct AsciicastWriter<W: Write, R: Write> {
    inner: W,
    recorder: AsciicastRecorder<R>,
}

impl<W: Write, R: Write> AsciicastWriter<W, R> {
    /// Create a new recording writer.
    pub const fn new(inner: W, recorder: AsciicastRecorder<R>) -> Self {
        Self { inner, recorder }
    }

    /// Access the underlying recorder (for input recording).
    pub fn recorder_mut(&mut self) -> &mut AsciicastRecorder<R> {
        &mut self.recorder
    }

    /// Record input bytes.
    pub fn record_input(&mut self, data: &[u8]) -> io::Result<()> {
        self.recorder.record_input(data)
    }

    /// Flush and finish recording, returning the inner writer and recorder output.
    pub fn finish(mut self) -> io::Result<(W, R)> {
        self.inner.flush()?;
        let recorder_output = self.recorder.finish()?;
        Ok((self.inner, recorder_output))
    }
}

impl<W: Write, R: Write> Write for AsciicastWriter<W, R> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let written = self.inner.write(buf)?;
        if written > 0 {
            self.recorder.record_output(&buf[..written])?;
        }
        Ok(written)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()?;
        self.recorder.output.flush()
    }
}

fn write_header<W: Write>(
    output: &mut W,
    width: u16,
    height: u16,
    timestamp: i64,
) -> io::Result<()> {
    writeln!(
        output,
        "{{\"version\":2,\"width\":{},\"height\":{},\"timestamp\":{}}}",
        width, height, timestamp
    )
}

fn unix_timestamp() -> io::Result<i64> {
    let since_epoch = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| io::Error::other("system time before unix epoch"))?;
    Ok(since_epoch.as_secs() as i64)
}

fn escape_json(input: &str) -> String {
    let mut out = String::with_capacity(input.len() + 8);
    for ch in input.chars() {
        match ch {
            '\"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\u{08}' => out.push_str("\\b"),
            '\u{0C}' => out.push_str("\\f"),
            c if c < ' ' => {
                let _ = write!(out, "\\u{:04x}", c as u32);
            }
            _ => out.push(ch),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    fn make_recorder(width: u16, height: u16) -> AsciicastRecorder<Cursor<Vec<u8>>> {
        AsciicastRecorder::with_writer(Cursor::new(Vec::new()), width, height, 0).unwrap()
    }

    fn output_string(recorder: AsciicastRecorder<Cursor<Vec<u8>>>) -> String {
        let cursor = recorder.finish().unwrap();
        String::from_utf8(cursor.into_inner()).unwrap()
    }

    // --- Header tests ---

    #[test]
    fn header_and_output_are_written() {
        let cursor = Cursor::new(Vec::new());
        let mut recorder = AsciicastRecorder::with_writer(cursor, 80, 24, 123).unwrap();
        recorder.record_output(b"hi\n").unwrap();
        let cursor = recorder.finish().unwrap();
        let output = String::from_utf8(cursor.into_inner()).unwrap();
        let mut lines = output.lines();
        assert_eq!(
            lines.next().unwrap(),
            "{\"version\":2,\"width\":80,\"height\":24,\"timestamp\":123}"
        );
        let event = lines.next().unwrap();
        assert!(event.contains("\"o\""));
        assert!(event.contains("hi\\n"));
    }

    #[test]
    fn header_contains_version_2() {
        let recorder = make_recorder(40, 10);
        let output = output_string(recorder);
        let header = output.lines().next().unwrap();
        assert!(header.contains("\"version\":2"));
    }

    #[test]
    fn header_contains_dimensions() {
        let recorder = make_recorder(120, 50);
        let output = output_string(recorder);
        let header = output.lines().next().unwrap();
        assert!(header.contains("\"width\":120"));
        assert!(header.contains("\"height\":50"));
    }

    // --- Event recording tests ---

    #[test]
    fn record_output_creates_output_event() {
        let mut recorder = make_recorder(80, 24);
        recorder.record_output(b"hello").unwrap();
        let output = output_string(recorder);
        let event = output.lines().nth(1).unwrap();
        assert!(event.starts_with('['));
        assert!(event.contains("\"o\""));
        assert!(event.contains("hello"));
    }

    #[test]
    fn record_input_creates_input_event() {
        let mut recorder = make_recorder(80, 24);
        recorder.record_input(b"key").unwrap();
        let output = output_string(recorder);
        let event = output.lines().nth(1).unwrap();
        assert!(event.contains("\"i\""));
        assert!(event.contains("key"));
    }

    #[test]
    fn multiple_events_are_sequential() {
        let mut recorder = make_recorder(80, 24);
        recorder.record_output(b"first").unwrap();
        recorder.record_output(b"second").unwrap();
        recorder.record_input(b"third").unwrap();
        let output = output_string(recorder);
        let lines: Vec<&str> = output.lines().collect();
        // header + 3 events
        assert_eq!(lines.len(), 4);
        assert!(lines[1].contains("first"));
        assert!(lines[2].contains("second"));
        assert!(lines[3].contains("third"));
    }

    #[test]
    fn event_count_tracks_events() {
        let mut recorder = make_recorder(80, 24);
        assert_eq!(recorder.event_count(), 0);
        recorder.record_output(b"a").unwrap();
        assert_eq!(recorder.event_count(), 1);
        recorder.record_input(b"b").unwrap();
        assert_eq!(recorder.event_count(), 2);
    }

    #[test]
    fn accessor_methods_return_dimensions() {
        let recorder = make_recorder(132, 43);
        assert_eq!(recorder.width(), 132);
        assert_eq!(recorder.height(), 43);
    }

    #[test]
    fn duration_is_non_negative() {
        let recorder = make_recorder(80, 24);
        assert!(recorder.duration().as_secs_f64() >= 0.0);
    }

    // --- JSON escaping tests ---

    #[test]
    fn json_escape_controls() {
        let cursor = Cursor::new(Vec::new());
        let mut recorder = AsciicastRecorder::with_writer(cursor, 1, 1, 0).unwrap();
        recorder.record_output(b"\"\\\\\n").unwrap();
        let cursor = recorder.finish().unwrap();
        let output = String::from_utf8(cursor.into_inner()).unwrap();
        let event = output.lines().nth(1).unwrap();
        assert!(event.contains("\\\"\\\\\\\\\\n"));
    }

    #[test]
    fn escape_json_handles_all_special_chars() {
        assert_eq!(escape_json("\""), "\\\"");
        assert_eq!(escape_json("\\"), "\\\\");
        assert_eq!(escape_json("\n"), "\\n");
        assert_eq!(escape_json("\r"), "\\r");
        assert_eq!(escape_json("\t"), "\\t");
        assert_eq!(escape_json("\u{08}"), "\\b");
        assert_eq!(escape_json("\u{0C}"), "\\f");
    }

    #[test]
    fn escape_json_passes_normal_text() {
        assert_eq!(escape_json("hello world"), "hello world");
        assert_eq!(escape_json(""), "");
    }

    #[test]
    fn escape_json_handles_low_control_chars() {
        let result = escape_json("\x01\x02");
        assert!(result.contains("\\u0001"));
        assert!(result.contains("\\u0002"));
    }

    // --- AsciicastWriter tests ---

    #[test]
    fn writer_mirrors_output_to_recorder() {
        let output = Cursor::new(Vec::new());
        let recorder = make_recorder(80, 24);
        let mut writer = AsciicastWriter::new(output, recorder);

        writer.write_all(b"test data").unwrap();
        writer.flush().unwrap();

        let (output, recording) = writer.finish().unwrap();
        let output_str = String::from_utf8(output.into_inner()).unwrap();
        let recording_str = String::from_utf8(recording.into_inner()).unwrap();

        assert_eq!(output_str, "test data");
        assert!(recording_str.contains("test data"));
    }

    #[test]
    fn writer_record_input_works() {
        let output = Cursor::new(Vec::new());
        let recorder = make_recorder(80, 24);
        let mut writer = AsciicastWriter::new(output, recorder);

        writer.record_input(b"key press").unwrap();

        let (_, recording) = writer.finish().unwrap();
        let recording_str = String::from_utf8(recording.into_inner()).unwrap();
        assert!(recording_str.contains("\"i\""));
        assert!(recording_str.contains("key press"));
    }

    #[test]
    fn writer_recorder_mut_accessible() {
        let output = Cursor::new(Vec::new());
        let recorder = make_recorder(80, 24);
        let mut writer = AsciicastWriter::new(output, recorder);

        assert_eq!(writer.recorder_mut().event_count(), 0);
        writer.write_all(b"x").unwrap();
        assert_eq!(writer.recorder_mut().event_count(), 1);
    }

    // --- Empty recording test ---

    #[test]
    fn finish_with_no_events_produces_header_only() {
        let recorder = make_recorder(80, 24);
        let output = output_string(recorder);
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines.len(), 1); // header only
    }
}