aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/ttyplay/frames.rs
blob: 2c70f7d7c3bff9cd6e571531c6abde0e5c615986 (plain) (blame)
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
#[derive(Debug, Clone)]
pub struct Frame {
    screen: vt100::Screen,
    delay: std::time::Duration,
}

impl Frame {
    pub fn new(screen: vt100::Screen, delay: std::time::Duration) -> Self {
        Self { screen, delay }
    }

    pub fn into_screen(self) -> vt100::Screen {
        self.screen
    }

    pub fn delay(&self) -> std::time::Duration {
        self.delay
    }
}

pub struct FrameData {
    frames: Vec<Frame>,
    done_reading: bool,
    new_frame_w: async_std::channel::Sender<Option<usize>>,
    new_frame_r: async_std::channel::Receiver<Option<usize>>,
}

impl FrameData {
    pub fn new() -> Self {
        let (new_frame_w, new_frame_r) = async_std::channel::unbounded();
        Self {
            frames: vec![],
            done_reading: false,
            new_frame_w,
            new_frame_r,
        }
    }

    pub fn get(&self, i: usize) -> Option<&Frame> {
        self.frames.get(i)
    }

    pub fn count(&self) -> usize {
        self.frames.len()
    }

    pub async fn add_frame(&mut self, frame: Frame) {
        self.frames.push(frame);
        self.new_frame_w
            .send(Some(self.frames.len()))
            .await
            .unwrap();
    }

    pub async fn done_reading(&mut self) {
        self.done_reading = true;
        self.new_frame_w.send(None).await.unwrap();
    }

    pub fn wait_for_frame(
        &self,
        i: usize,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = bool> + 'static + Send>,
    > {
        if i < self.frames.len() {
            return Box::pin(std::future::ready(true));
        }
        if self.done_reading {
            return Box::pin(std::future::ready(false));
        }
        let new_frame_r = self.new_frame_r.clone();
        Box::pin(async move {
            while let Some(new_len) = new_frame_r.recv().await.unwrap() {
                if i < new_len {
                    return true;
                }
            }
            false
        })
    }
}