2024-11-12 13:46:38 +01:00
|
|
|
use crate::asciicast::{Event, EventData, Header};
|
2024-01-25 16:01:14 +01:00
|
|
|
|
2025-06-01 09:43:05 +02:00
|
|
|
pub struct RawEncoder;
|
2024-01-25 16:01:14 +01:00
|
|
|
|
2024-10-17 17:31:59 +02:00
|
|
|
impl RawEncoder {
|
2025-06-01 09:43:05 +02:00
|
|
|
pub fn new() -> Self {
|
|
|
|
|
RawEncoder
|
2024-01-25 16:01:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 17:31:59 +02:00
|
|
|
impl super::Encoder for RawEncoder {
|
2024-11-12 13:46:38 +01:00
|
|
|
fn header(&mut self, header: &Header) -> Vec<u8> {
|
2025-06-01 09:43:05 +02:00
|
|
|
format!("\x1b[8;{};{}t", header.term_rows, header.term_cols).into_bytes()
|
2024-01-25 16:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 17:31:59 +02:00
|
|
|
fn event(&mut self, event: Event) -> Vec<u8> {
|
|
|
|
|
if let EventData::Output(data) = event.data {
|
|
|
|
|
data.into_bytes()
|
2024-01-25 16:01:14 +01:00
|
|
|
} else {
|
2024-10-17 17:31:59 +02:00
|
|
|
Vec::new()
|
2024-01-25 16:01:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-17 17:31:59 +02:00
|
|
|
|
2024-11-12 13:46:38 +01:00
|
|
|
fn flush(&mut self) -> Vec<u8> {
|
2024-10-17 17:31:59 +02:00
|
|
|
Vec::new()
|
|
|
|
|
}
|
2024-01-25 16:01:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2024-01-28 20:11:47 +01:00
|
|
|
mod tests {
|
2025-10-17 11:38:57 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2024-01-25 16:01:14 +01:00
|
|
|
use super::RawEncoder;
|
2024-11-12 13:46:38 +01:00
|
|
|
use crate::asciicast::{Event, Header};
|
2024-01-25 16:01:14 +01:00
|
|
|
use crate::encoder::Encoder;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2024-10-17 17:31:59 +02:00
|
|
|
fn encoder() {
|
2025-06-01 09:43:05 +02:00
|
|
|
let mut enc = RawEncoder::new();
|
2024-10-17 17:31:59 +02:00
|
|
|
|
2024-11-12 13:46:38 +01:00
|
|
|
let header = Header {
|
2025-04-24 11:00:14 +02:00
|
|
|
term_cols: 100,
|
|
|
|
|
term_rows: 50,
|
2024-11-12 13:46:38 +01:00
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(enc.header(&header), "\x1b[8;50;100t".as_bytes());
|
2024-01-25 16:01:14 +01:00
|
|
|
|
2024-10-17 17:31:59 +02:00
|
|
|
assert_eq!(
|
2025-10-17 11:38:57 +02:00
|
|
|
enc.event(Event::output(
|
|
|
|
|
Duration::from_micros(0),
|
|
|
|
|
"he\x1b[1mllo\r\n".to_owned()
|
|
|
|
|
)),
|
2024-10-17 17:31:59 +02:00
|
|
|
"he\x1b[1mllo\r\n".as_bytes()
|
|
|
|
|
);
|
2024-01-25 16:01:14 +01:00
|
|
|
|
2024-10-17 17:31:59 +02:00
|
|
|
assert_eq!(
|
2025-10-17 11:38:57 +02:00
|
|
|
enc.event(Event::output(
|
|
|
|
|
Duration::from_micros(1),
|
|
|
|
|
"world\r\n".to_owned()
|
|
|
|
|
)),
|
2024-10-17 17:31:59 +02:00
|
|
|
"world\r\n".as_bytes()
|
|
|
|
|
);
|
2024-02-26 10:55:32 +01:00
|
|
|
|
2025-10-17 11:38:57 +02:00
|
|
|
assert!(enc
|
|
|
|
|
.event(Event::input(Duration::from_micros(2), ".".to_owned()))
|
|
|
|
|
.is_empty());
|
|
|
|
|
assert!(enc
|
|
|
|
|
.event(Event::resize(Duration::from_micros(3), (80, 24)))
|
|
|
|
|
.is_empty());
|
|
|
|
|
assert!(enc
|
|
|
|
|
.event(Event::marker(Duration::from_micros(4), ".".to_owned()))
|
|
|
|
|
.is_empty());
|
2024-11-12 13:46:38 +01:00
|
|
|
assert!(enc.flush().is_empty());
|
2024-01-25 16:01:14 +01:00
|
|
|
}
|
|
|
|
|
}
|