Refactor Recorder and Writer traits

This commit is contained in:
Marcin Kulik
2024-01-11 11:09:10 +01:00
parent 8479035dc4
commit f6e18c36ad
5 changed files with 65 additions and 60 deletions

View File

@@ -1,4 +1,3 @@
use crate::format;
use crate::format::{asciicast, raw};
use crate::locale;
use crate::pty;
@@ -89,7 +88,7 @@ impl Cli {
.truncate(overwrite)
.open(&self.filename)?;
let writer: Box<dyn format::Writer + Send> = if self.raw {
let writer: Box<dyn recorder::EventWriter + Send> = if self.raw {
Box::new(raw::Writer::new(file))
} else {
let time_offset = if append {
@@ -105,10 +104,12 @@ impl Cli {
writer,
append,
self.stdin,
self.idle_time_limit,
self.command.clone(),
self.title,
capture_env(&self.env),
recorder::Metadata {
idle_time_limit: self.idle_time_limit,
command: self.command.clone(),
title: self.title,
env: capture_env(&self.env),
},
);
let exec_command = build_exec_command(self.command);

View File

@@ -1,20 +1,2 @@
pub mod asciicast;
pub mod raw;
use std::{collections::HashMap, io};
pub trait Writer {
fn header(&mut self, header: &Header) -> io::Result<()>;
fn output(&mut self, time: u64, data: &[u8]) -> io::Result<()>;
fn input(&mut self, time: u64, data: &[u8]) -> io::Result<()>;
fn resize(&mut self, time: u64, size: (u16, u16)) -> io::Result<()>;
}
pub struct Header {
pub cols: u16,
pub rows: u16,
pub timestamp: Option<u64>,
pub idle_time_limit: Option<f64>,
pub command: Option<String>,
pub title: Option<String>,
pub env: HashMap<String, String>,
}

View File

@@ -1,3 +1,4 @@
use crate::recorder;
use anyhow::Result;
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
@@ -63,12 +64,16 @@ where
}
}
impl<W> super::Writer for Writer<W>
impl<W> recorder::EventWriter for Writer<W>
where
W: Write,
{
fn header(&mut self, header: &super::Header) -> io::Result<()> {
self.write_header(&header.into())
fn start(&mut self, header: &recorder::Header, append: bool) -> io::Result<()> {
if append {
Ok(())
} else {
self.write_header(&header.into())
}
}
fn output(&mut self, time: u64, data: &[u8]) -> io::Result<()> {
@@ -267,7 +272,7 @@ fn format_time(time: u64) -> String {
format!("{}.{:0>6}", time / 1_000_000, time % 1_000_000)
}
impl From<&Header> for super::Header {
impl From<&Header> for recorder::Header {
fn from(header: &Header) -> Self {
Self {
cols: header.width,
@@ -281,8 +286,8 @@ impl From<&Header> for super::Header {
}
}
impl From<&super::Header> for Header {
fn from(header: &super::Header) -> Self {
impl From<&recorder::Header> for Header {
fn from(header: &recorder::Header) -> Self {
Self {
width: header.cols,
height: header.rows,

View File

@@ -1,3 +1,4 @@
use crate::recorder;
use std::io::{self, Write};
pub struct Writer<W> {
@@ -10,9 +11,13 @@ impl<W> Writer<W> {
}
}
impl<W: Write> super::Writer for Writer<W> {
fn header(&mut self, header: &super::Header) -> io::Result<()> {
write!(self.writer, "\x1b[8;{};{}t", header.rows, header.cols)
impl<W: Write> recorder::EventWriter for Writer<W> {
fn start(&mut self, header: &recorder::Header, append: bool) -> io::Result<()> {
if append {
Ok(())
} else {
write!(self.writer, "\x1b[8;{};{}t", header.rows, header.cols)
}
}
fn output(&mut self, _time: u64, data: &[u8]) -> io::Result<()> {

View File

@@ -1,4 +1,3 @@
use crate::format;
use crate::pty;
use std::collections::HashMap;
use std::io;
@@ -7,19 +6,40 @@ use std::thread;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
pub struct Recorder {
writer: Option<Box<dyn format::Writer + Send>>,
writer: Option<Box<dyn EventWriter + Send>>,
start_time: Instant,
append: bool,
record_input: bool,
idle_time_limit: Option<f64>,
command: Option<String>,
title: Option<String>,
env: HashMap<String, String>,
metadata: Metadata,
sender: mpsc::Sender<Message>,
receiver: Option<mpsc::Receiver<Message>>,
handle: Option<JoinHandle>,
}
pub trait EventWriter {
fn start(&mut self, header: &Header, append: bool) -> io::Result<()>;
fn output(&mut self, time: u64, data: &[u8]) -> io::Result<()>;
fn input(&mut self, time: u64, data: &[u8]) -> io::Result<()>;
fn resize(&mut self, time: u64, size: (u16, u16)) -> io::Result<()>;
}
pub struct Header {
pub cols: u16,
pub rows: u16,
pub timestamp: Option<u64>,
pub idle_time_limit: Option<f64>,
pub command: Option<String>,
pub title: Option<String>,
pub env: HashMap<String, String>,
}
pub struct Metadata {
pub idle_time_limit: Option<f64>,
pub command: Option<String>,
pub title: Option<String>,
pub env: HashMap<String, String>,
}
enum Message {
Output(u64, Vec<u8>),
Input(u64, Vec<u8>),
@@ -30,13 +50,10 @@ struct JoinHandle(Option<thread::JoinHandle<()>>);
impl Recorder {
pub fn new(
writer: Box<dyn format::Writer + Send>,
writer: Box<dyn EventWriter + Send>,
append: bool,
record_input: bool,
idle_time_limit: Option<f64>,
command: Option<String>,
title: Option<String>,
env: HashMap<String, String>,
metadata: Metadata,
) -> Self {
let (sender, receiver) = mpsc::channel();
@@ -45,10 +62,7 @@ impl Recorder {
start_time: Instant::now(),
append,
record_input,
idle_time_limit,
command,
title,
env,
metadata,
sender,
receiver: Some(receiver),
handle: None,
@@ -70,19 +84,17 @@ impl pty::Recorder for Recorder {
let mut writer = self.writer.take().unwrap();
let receiver = self.receiver.take().unwrap();
if !self.append {
let header = format::Header {
cols: size.0,
rows: size.1,
timestamp: Some(timestamp),
idle_time_limit: self.idle_time_limit,
command: self.command.clone(),
title: self.title.clone(),
env: self.env.clone(),
};
let header = Header {
cols: size.0,
rows: size.1,
timestamp: Some(timestamp),
idle_time_limit: self.metadata.idle_time_limit,
command: self.metadata.command.clone(),
title: self.metadata.title.clone(),
env: self.metadata.env.clone(),
};
writer.header(&header)?;
}
writer.start(&header, self.append)?;
let handle = thread::spawn(move || {
for msg in receiver {